How to only change the text in a DOM element without replacing any child elements

前端 未结 7 1299
一个人的身影
一个人的身影 2021-01-17 17:58

Hi I have a simple html structure

Title text inner text

What I want is to replace only the te

7条回答
  •  没有蜡笔的小新
    2021-01-17 18:40

    $("h1").each(function() {
        var textNode = document.createTextNode("new text");
        this.replaceChild(textNode, this.firstChild);
    });
    

    DEMO: http://jsfiddle.net/FvbJa/

提交回复
热议问题