I've got a paragraph: <p>I want to put a ding in the universe.</p>
I want to change the attributes of the characters in the text e.g. to change its color, so after this the paragraph should look like this:
<p>
<span class="color">
I
</span> <span class="color">
w
</span><span class="color">
a
</span><span class="color">
n
</span><span class="color">
t
</span>...
</p>
How to realize it in jQuery?
Some combination of String.split() and jQuery.wrap() will probably do the trick.
Something like this: (untested, just whipped this up quickly)
Since characters are not DOM-elements you need to build a new DOM:
Beware of that this method might not be fast, and you are emptying the paragraph (
<p />
) so every information that is not text will be removed, for example links (<a />
) will not be preserved. Result:I don't see the point of wrapping individually with the spans that have the same class, but here you go:
EDIT: If you wanted to assign different classes, you could do something like this:
Example: http://jsfiddle.net/LnD6W/1/
CSS
A little slow with response but I have a working example: http://jsfiddle.net/YgzhK/