Woo*_*old 3 html javascript parent-child innertext
我有一个 html 元素,例如:
<div id="el1">Change only me<div>but not me</div></div>
Run Code Online (Sandbox Code Playgroud)
但我只想更改第一个文本并保留子 div 不变
<div id="el1">Change only me<div>but not me</div></div>
Run Code Online (Sandbox Code Playgroud)
document.getElementById("el1").innerText = "changed!"Run Code Online (Sandbox Code Playgroud)
Maj*_*awi 12
尝试这个:
document.getElementById("el1").childNodes[0].textContent = "changed";Run Code Online (Sandbox Code Playgroud)
<div id="el1">Change only me<div>but not me</div></div>Run Code Online (Sandbox Code Playgroud)