如何在不更改其子元素的情况下更改元素 innerText
How do you change an element innerText without changing its children
我有一个 html 元素,例如:
<div id="el1">Change only me<div>but not me</div></div>
但我只想更改第一个文本并让子 div 保持原样
document.getElementById("el1").innerText = "changed!"
<div id="el1">Change only me<div>but not me</div></div>
试试这个:
document.getElementById("el1").childNodes[0].textContent = "changed";
<div id="el1">Change only me<div>but not me</div></div>
我有一个 html 元素,例如:
<div id="el1">Change only me<div>but not me</div></div>
但我只想更改第一个文本并让子 div 保持原样
document.getElementById("el1").innerText = "changed!"
<div id="el1">Change only me<div>but not me</div></div>
试试这个:
document.getElementById("el1").childNodes[0].textContent = "changed";
<div id="el1">Change only me<div>but not me</div></div>