简体   繁体   English

Vanilla JS:查找仅包含文本的所有DOM元素

[英]Vanilla JS: Find all the DOM elements that just contain text

I want to get all the DOM elements in an HTML that doesn't contain any node, but text only. 我想在HTML中获取所有DOM元素,该元素不包含任何节点,而仅包含文本。

I've got this code right now: 我现在有以下代码:

var elements = document.querySelectorAll("body *");
for(var i = 0; i < elements.length; i++) {
    if(!elements[i].hasChildNodes()) {
        console.log(elements[i])
    }
}

This prints of course elements that have absolutely no content (and curiously enough, iframes). 这会打印出绝对没有内容的课程元素(而且奇怪的是,iframe)。 Texts are accounted as a child node, so the .childNodes.length equals 1, but I don't know how to distinguish the nodes from the text. 文本被视为子节点,因此.childNodes.length等于1,但是我不知道如何从文本中区分出这些节点。 typeof the first node is always object, sadly. typeof第一节点总是对象,可悲。

How to distinguish the texts from the nodes? 如何区分文本和节点?

You can check for elements that have no .firstElementChild , which means it will only have text (or other invisible stuff). 您可以检查没有.firstElementChild元素,这意味着它将仅包含文本(或其他不可见的东西)。

 var elements = document.querySelectorAll("body *"); for (var i = 0; i < elements.length; i++) { if (!elements[i].firstElementChild) { console.log(elements[i].nodeName) } } 
 <p> text and elements <span>text only</span> </p> <div>text only</div> 

The script that the stack snippet is included because it also only has text. 包括堆栈片段的script ,因为它也只有文本。 You can filter out scripts if needed. 您可以根据需要过滤出脚本。 This will also include elements that can not have content, like <input> . 这还将包括不能包含内容的元素,例如<input>

Basically you are looking for leaf nodes of DOM with something inside the textContent property of the leaf node. 基本上,您正在寻找DOM的叶子节点,其中的叶子节点的textContent属性内包含某些内容。

Let's traverse DOM and work out our little logic on leaf nodes. 让我们遍历DOM并在叶节点上得出我们的小逻辑。

const nodeQueue = [ document.querySelector('html') ];    
const textOnlyNodes = [];
const textRegEx = /\w+/gi;

function traverseDOM () {
  let currentNode = nodeQueue.shift();

  // Our Leaf node
  if (!currentNode.childElementCount && textRegEx.test(currentNode.textContent)) {
    textOnlyNodes.push(currentNode);
    return;
  }

  // Nodes with child nodes
  nodeQueue.push(...currentNode.children);
  traverseDOM();
}

childElementCount property make sure that the node is the leaf node and the RegEx test on textContent property is just my understanding of what a text implies in general. childElementCount属性确保该节点是叶节点和财产的textContent正则表达式测试只是我一个什么样的文本一般意味着理解。 You can anytime tune the expression to make it a btter fit for your use case. 您可以随时调整表达式以使其更适合您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM