这是一个例子
<p id="myid">
<font>
<font>000</font>
</font>
</p>
我想要000
我想是这样的
document.getElementById("#myid").innerHTML
最佳答案
- 使用
getElementById()
时不需要#
- 您需要使用
innerText
而不是innerHTML
。 - 确保您的代码在
DOM
加载完成后运行。
代码:
document.addEventListener("DOMContentLoaded", function() {
alert(document.getElementById('myid').innerText);
});
<p id="myid">
<font>
<font>000</font>
</font>
</p>
关于javascript - 基于类元素取其他元素中某个元素的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31427829/