I’m trying to add a child element to another element based on a given start offset and length. Example:
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
With a given start offset of 6 characters and a length of 20 characters, I want to inner wrap the parent element (p) with a span element.
<p>Lorem <span>ipsum dolor sit amet</span>,
consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
ut laoreet dolore magna aliquam erat volutpat.
Any ideas?
You have a text node, which you can split using
splitText: http://jsfiddle.net/VNY2k/.If you have a text node
nodeand split indexn, thennode.splitText(n)will modifynodeto only containnode‘s text up to that index, and return a new text node with the rest of the text.Note that you can’t edit the text node itself to contain
<span>. A text node really only contains text, and an element (e.g.<span>) is a node per se. As such you need to split the text nodes.