我有一个 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)