我正在尝试从一段 HTML 代码(不是我的)中获取字符串:这就是我需要的
<div class="hlFld-Abstract"><h2 id="section-1"><span>Abstract</span></h2><div class="abstractSection abstractInFull"><p xmlns:mml="http://www.w3.org/1998/Math/MathML">THIS is what I need</p> </div></div><h2><span>Keywords</span>
为此,我编写了以下内容:
document.getElementById("section-1").document.getElementsByClassName("abstractSection abstractInFull")[0].textContent
它几乎完成了这项工作,但它还包括我不需要的▪ Abstract
。如何仅获取字符串这就是我需要的
?
最佳答案
您可以使用 querySelector
来实现:
console.log(document.querySelector('div.abstractSection.abstractInFull p').textContent)
<div class="hlFld-Abstract"><h2 id="section-1"><span>Abstract</span></h2><div class="abstractSection abstractInFull"><p xmlns:mml="http://www.w3.org/1998/Math/MathML">THIS is what I need</p> </div></div><h2><span>Keywords</span>
关于javascript - 通过 ClassName 获取 <span> 中不带字符串的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41775055/