我正在尝试更改以下 h5 标记中的文本:
<h5 id="status"><img id="status-icon" src="some image">Change Text</h5>
我正在使用:
var heading = document.getElementById("status")
heading.textContent = "Resolved"
上面替换了 img 标签。
如何只更改 h5 的文本?
最佳答案
您可以设置heading
的.firstElementChild.nextSibling
的.textContent
var heading = document.getElementById("status");
heading.firstElementChild.nextSibling.textContent = "Resolved";
<h5 id="status"><img id="status-icon" src="#" alt="status-icon">Change Text</h5>
或使用.querySelector()
和.nextSibling
var heading = document.getElementById("status");
heading.querySelector("img").nextSibling.textContent = "Resolved";
<h5 id="status"><img id="status-icon" src="#" alt="status-icon">Change Text</h5>
关于javascript - 如何更改嵌套标签中第一个标签的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46274978/