我有一个html元素,像。
<div id="el1">Change only me<div>but not me</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>