javascript - 通过 ClassName 获取 <span> 中不带字符串的元素

标签 javascript html

我正在尝试从一段 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/

相关文章:

javascript - tinymce - 编辑器初始化后更新插件属性

javascript - 在javascript中重新排序字符串

javascript - 在 jQuery 中定位上一类时遇到问题

javascript - 如何强制将 SVG 包含在 div 中?

html - 未定义 FormView 上的命名空间前缀 asp - 我的页面指令是什么?

html - 如何将用户的输入从 .html 文件获取到 .c 文件?

javascript - 如何使用jquery获取div元素的html内容

javascript - 菜单图标动画 jQuery

html - Bootstrap 4 Grid - 使元素彼此靠近

html - 有效的字体系列名称