Javascript 使用标签内的标题保留内部文本

Javascript 使用标签内的标题保留内部文本,javascript,Javascript,有这样一个标签: <h1> Title</h1> 问题中的该命令返回一个列表,因此索引第一个元素并获取内部文本,如: document.querySelectorAll("h1")[0].innerText 因此,您想要使用DOM获得H1的“标题”?您可以将节点列表转换为数组,并按文本内容进行筛选[…document.queryselectoral('H1')]。筛选(item=>item.textContent.includes('Title')

有这样一个标签:

<h1> Title</h1>

问题中的该命令返回一个列表,因此索引第一个元素并获取内部文本,如:

document.querySelectorAll("h1")[0].innerText

因此,您想要使用DOM获得H1的“标题”?您可以将节点列表转换为数组,并按文本内容进行筛选
[…document.queryselectoral('H1')]。筛选(item=>item.textContent.includes('Title'))
@keidakida是的,就是这样,然后您就可以执行此
document.queryselectoral(“H1”)[0]。innerText
@YuryTarabanko谢谢您
document.querySelectorAll("h1")[0].innerText