I want to replace a specific line in (text) with all descendant elements of this element.
innerHTML cannot be used because this sequence can be displayed in attributes. I tried using XPath, but it seems that the interface is essentially read-only. Since this is limited to one element, functions like document.getElementsByTagName cannot be used either.
Can anyone suggest any way to do this? Any jQuery or pure DOM method is acceptable.
Edit:
Some of the answers point to the problem I was trying to work with: changing the text directly on the element will delete all the non-text child nodes.
Thus, the problem basically boils down to how to efficiently select all Text nodes in the tree. In XPath, you can easily do this as //text() , but the current XPath interface does not allow you to modify these text nodes.
One way to do this is recursion, as shown in Bergi's answer . Another way is to use the jQuery find('*') selector, but it's a bit expensive. Still waiting to see if there are better solutions.
billc.cn
source share