简体   繁体   English

JavaScript 获取不包括子项的 textContent

[英]JavaScript get textContent excluding children

First, I'm creating a library for JavaScript and I can not use jQuery.首先,我正在为 JavaScript 创建一个库,但我不能使用 jQuery。 I'm trying to get the text content of an HTML element without the text contents of its children.我正在尝试获取 HTML 元素的文本内容,而没有其子元素的文本内容。

Both attributes innerText and textContent don't give me what needed, please help.两个属性 innerText 和 textContent 都没有给我需要的东西,请帮忙。

You can solve using DOM API as childNodes and nodeType.您可以使用 DOM API 作为 childNodes 和 nodeType 来解决。

var elChildNode = document.querySelector("#hello").childNodes;
var sChildTextSum = "";

elChildNode.forEach(function(value){
    if(value.nodeType === Node.TEXT_NODE) { 
        console.log("Current textNode value is : ", value.nodeValue.trim());
        sChildTextSum += value.nodeValue;
    }
}); 

console.log("All text value of firstChild : ", sChildTextSum);

I created a sample code as above.我如上所述创建了一个示例代码。

https://jsfiddle.net/nigayo/p7t9bdc3/ https://jsfiddle.net/nigayo/p7t9bdc3/

To get Author's Name from the following element, excluding <span>... :要从以下元素中获取Author's Name ,不包括<span>...

<div class="details__instructor">
    Author's Name<span ng-show="job_title">, Entrepreneur</span>
</div>

use childNodes[0] .使用childNodes[0] For example:例如:

document.querySelector('div.details__instructor').childNodes[0].textContent

Using only JavaScript (you specified you cannot use jQuery), and given that you have provided and know the id for the parent element:仅使用 JavaScript(您指定不能使用 jQuery),并且假设您已经提供并知道父元素的id

document.getElementById('parent_element_id').childNodes[0].nodeValue;

You can also use .trim() to remove any trailing space characters left behind from the removal of any child element text:您还可以使用.trim()删除在删除任何子元素文本时留下的任何尾随空格字符:

document.getElementById('parent_element_id').childNodes[0].nodeValue.trim();
var mydiv = getElementByID("id");
function Get_text(element) {
    var selected = element.cloneNode(true);
    var text;
    while (selected.firstChild) {
      if (selected.firstChild.nodeType == 3) text = selected.firstChild.nodeValue;
      selected.removeChild(selected.firstChild);
    }
    return text;
  }
Get_text(mydiv);

I know many good solutions here exist, but none of them actually achieved what I needed (get the textContent of a single node, none of its children), so sharing this for future searchers.我知道这里有很多好的解决方案,但它们都没有真正实现我所需要的(获取单个节点的 textContent,没有它的子节点),因此为未来的搜索者分享这个。

var html = document.getElementsByTagName("*");

for (var i = 0; i < html.length; i++) {
    var el = html[i];
    for (var j = 0; j < el.children.length; j++) {
        var child = el.children[j],
            childTextContent = child.innerHTML;
        // Remove all children tags, leaving only the actual text of the node.
        childTextContent = childTextContent.replace(/\<.*\>.*\<\/.*\>/gmi, "");
        console.log(childTextContent);
        // Now you can do any type of text matching (regex) on the result.
    }
});

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

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