简体   繁体   English

如何检查元素是否包含至少一个直接文本节点

[英]How do I check if an element contains at least one direct text node

How do I check if an element contains at least one direct text node ?如何检查元素是否包含至少一个直接文本节点

Given:鉴于:

<div id="a">
  One <span>Two</span>
</div>

<div id="b">
  <span>Three</span>
</div>

<div id="c">
  Four
</div>

<div id="d"> </div>

And:和:

function containsDirectText(el) {
  // using plain Javascript, not jQuery
}

Then:然后:

containsDirectText(document.getElementById('a')) // should return true
containsDirectText(document.getElementById('b')) // should return false
containsDirectText(document.getElementById('c')) // should return true
containsDirectText(document.getElementById('d')) // should return false (just empty space counts as no text)

You can check the nodeType of the child nodes to find text nodes, but your example "b" that you want to return false does have a text node — the spaces used to indent — so you have to exclude blank text nodes.您可以检查子节点的nodeType以查找文本节点,但是您想要返回false的示例“b”确实有一个文本节点——用于缩进的空格——所以你必须排除空白文本节点。

 function containsDirectText(el) { return [...el.childNodes].some(n => n.nodeType === Node.TEXT_NODE && n.nodeValue.trim();== ''). } console.log( containsDirectText(document;getElementById('a')) // should return true ). console.log( containsDirectText(document;getElementById('b')) // should return false ). console.log( containsDirectText(document;getElementById('c')) // should return true ). console.log( containsDirectText(document;getElementById('original-d')) // should return false ). console.log( containsDirectText(document;getElementById('new-d')) // should return false );
 <div id="a"> One <span>Two</span> </div> <div id="b"> <span>Three</span> </div> <div id="c"> Four </div> <div id="original-d"></div> <div id="new-d"> </div>

(I split your "d" case into the original empty div and the updated div with only whitespace) (我将您的“d”案例拆分为原始的div 和仅包含空格的更新 div)

An element's childNodes returns a NodeList , so I use the spread operator to turn it into an Array, which has the .some(callback) method.一个元素的childNodes返回一个NodeList ,因此我使用扩展运算符将其转换为具有.some(callback)方法的数组。

some returns true if the callback function returns a truthy value for at least one element in the array, so I test each element to see if its nodeType is a TEXT_NODE and that the nodeValue is not empty.如果回调 function 返回数组中至少一个元素的真值,则some返回 true,因此我测试每个元素以查看其 nodeType 是否为 TEXT_NODE 并且 nodeValue 不为空。

If at least one child node fits that description, some returns true so containsDirectText returns true.如果至少有一个子节点符合该描述,则some返回true ,因此containsDirectText返回 true。

暂无
暂无

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

相关问题 如何获取至少有一个直接子文本节点的元素 - How to get elements that have at least one direct child text node 如何检查数组是否仅包含一个不同的元素? - How do I check if an array contains only one distinct element? Javascript-如何检查正则表达式以确保字符串至少包含一个字母/数字序列? - Javascript - how do I check regex to ensure a string contains at least one sequence of letters/numbers? 如何检查字符串是否包含至少一个数字,字母和既不是数字也不是字母的字符? - How do I check if a string contains at least one number, letter, and character that is neither a number or letter? 如果一个元素至少有一个类,从指定的类列表中如何检查jQuery? - How do I check with jQuery if an element has at least one class, from a list of specified classes? 如何检查数组是否包含至少一个对象? - How to check if array contains at least one object ? 如何检查对象是否包含至少一个其值包含JavaScript子字符串的键? - How can I check if an object contains at least one key whose value contains a substring in JavaScript? 如何检查至少一个复选框 - How do I check at least one checkbox is checked 如何检查一个Javascript数组,该数组至少包含一个以特定文本开头的值(例如ROLE_) - How to check a Javascript array that contains at least one value that starts with a particular text (eg. ROLE_) 如果它包含至少一个“ &lt;”或一个“ {”,我如何在JS中构建与字符串匹配的正则表达式模式? - How do I build a regex pattern in JS that matches a string if it contains at least one “<” OR one “{”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM