我的 button
中有下一个结构
<button>
Name
<svg>
<button>
名字是动态变化的。如果没有 <svg>
,我怎样才能得到按钮的内容?
如果我使用 innerHTML
我得到了所有的结构
最佳答案
如果这是按钮的结构,您可以使用 .firstChild
获取包含文本的文本节点,然后访问其 .textContent
。
也就是说,
const button = document.getElementById('button'); // or however you reference the button.
console.log(button.firstChild.textContent);
关于javascript - 取标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56887095/