I have a div id=”X” within that i have multiple p , what i want is that jquery select the first p and from that p take the first 25 words and give them the Class excerpt
$("p:first").addClass("excerpt");
This isn’t a problem but how to limit the words ?
You have to go into the text node inside the
pelement and split up the text data, then put a new wrapper element around those words, and give that wrapper a class. You can’t add a class directly to text itself.jQuery doesn’t really give you much in the way of tools for manipulating text nodes so you will have to do this using some plain DOM too, eg:
This requires that the text in the element be a single direct text node. If you’ve got a situation where there are more elements inside the text, eg:
then the question becomes much trickier, as you can’t wrap a
<span>around just part of an element.