开发者

Replace text at top-level of node without breaking formatting of children

开发者 https://www.devze.com 2023-04-09 08:58 出处:网络
If I have the following html code: <div id=\"element\"> Qwerty Foo Bar <span style=\"color: red;\">This text should/will never be changed by the script.</span>

If I have the following html code:

<div id="element">
Qwerty Foo Bar 
<span style="color: red;">This text should/will never be changed by the script.</span>
</div>

And I want to change "Foo" to "baz", I can do the following:

var element = document.getElementById('element');
element.innerText = element.innerText.replace('Foo', 'baz');

However this will destroy the formatting of the red text.

How can you do this?

I don't need cross-browser support, only support for chrome and I don't want to use a js framework. jsFiddle开发者_JAVA技巧: http://jsfiddle.net/cLzJD/3/


You can iterate over the children and only modify text nodes:

var children = element.childNodes,
    child;

for(var i = children.length; i--; ) {
    child = children[i];
    if(child.nodeType === 3) {
        child.nodeValue = child.nodeValue.replace('Foo', 'baz');
    }
}

DEMO

Notes:

  • If you want to replace all occurrences of Foo, you have to use a regular expression: replace(/Foo/g, 'baz').

  • The advantage of this approach is that event handlers bound through JavaScript will stay intact. If you don't need this, innerHTML will work as well.


Although @Felix Kling's solution is the best approach, in your special case you could use .innerHTML instead of .innerText.

element.innerHtml = element.innerHtml.replace('Foo', 'baz');

.replace() will only replace the first occurrence, so if you're sure there is no HTML content before your text, you can use it. Otherwise it could break your HTML.


You are losing the formatting because you're using innerText (which will return the contents with all the HTML stripped out). Just use innerHTML instead: http://jsfiddle.net/cLzJD/4/ (I've also changed the ids to be unique).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号