通过DOM进行递归搜索以去除innerText

时间:2019-02-08 12:11:15

标签: javascript dom

我需要递归搜索DIV层次结构并获取innerText。我只能通过ID来识别父对象,但这是唯一一致的信息。我知道该元素的子元素中有一个div,其中包含文本。对所有孩子以及孩子的孩子递归执行此操作的最佳方法是什么?

这是我得到的记录:

Found text: .rLshyf,.BmP5tf{padding-top:56px;padding-bottom:56px}
Found text: .w1C3Le,.BmP5tf,.G5NbBd{padding-left:112px;padding-right:112px;}
Found text: .G5NbBd{padding-bottom:56px}
Found text: .ROYx{display:flex;justify-content:space-between}
Found text: .SkmBxc{width:988px;margin-right:68px}
Found text: .EURV7d{font-weight:500;margin-top:-8px}
Found text: .j8epzd{height:192px;margin-top:16px}
Found text: .UYT3jb{width:100%;height:2px;margin-top:24px;margin-bottom:24px;background-color:#303030}
Found text: .z24g9c{margin-top:32px}
Found text: .GsVE9b{float:right}
Found text: .MUxGbd{font-size:28px;font-family:Roboto,sans-serif;line-height:40px;padding-top:2px;margin-bottom:-2px}
Found text: .MUxGbd.ITUZi{font-size:40px;line-height:48px}
Found text: .lyLwlc{color:#202124}
Found text: .aLF0Z{text-overflow:ellipsis;white-space:nowrap;overflow:hidden}
Found text: .MUxGbd.v0nnCb{font-size:32px;line-height:48px;padding-top:6px;margin-bottom:-6px;}
Found text: .lEBKkf{display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden}
Found text: .L6lOSd{color:rgba(255,255,255,.5) !important}
Found text: .L6lOSd svg{fill:rgba(255,255,255,.5);height:24px;width:24px;vertical-align:middle;margin-left:8px}
Found text: .L6lOSd:hover{background-color:#303030;color:rgba(255,255,255,1) !important}
Found text: .L6lOSd:hover svg{fill:rgba(255,255,255,1)}
Found text: .L6lOSd:focus{background-color:#303030;color:rgba(255,255,255,1) !important}
Found text: .L6lOSd:focus svg{fill:rgba(255,255,255,1)}
Found text: .MUxGbd.gbj1yb{font-size:24px;line-height:32px;padding-top:0px;margin-bottom:-8px}
Found text: .WZ8Tjf{color:#70757A}
Found text: .WZ8Tjf a:visited{color:#acacac}
Found text: Is iPhone 8 waterproof?
Found text: The iPhone 8 and 8 Plus is not waterproof — no smartphone is. However, as you stated yourself, they do have an IP67 rating for dust and water-resistance.
Found text: Source: damage - Does the iPhone 8 have any sort of water resistance or ...
Found text: window.onUrlClick=function(a){var b=a.getAttribute("data-url");a=a.getAttribute("data-follow-up-query");b?window.parent.postMessage({url:{href:b}},"*"):a&&window.parent.postMessage({query:{queryText:a}},"*")};
Found text: const ENTER_KEYCODE = 13;const ENTER = 'Enter';

我不明白为什么要得到所有内部样式文本。我该如何获取可显示的文本内容?

3 个答案:

答案 0 :(得分:1)

处理递归的最佳方法是创建一个递归函数,因此您可以从父级到树级进行递归。

function recurseEl(element) {
  if(element.childElementCount === 0) {
    element.textContent = '';
  } else {
    Array.from(element.children).forEach(child => {
      recurseEl(child);
    });
  }
}

function removeInnerText(elements) {
  recurseEl(element);
}

// Usage
const parentElement = document.getElementById('parent');
removeInnerText(parentElement);

这将在树中委派相同的递归函数,前提是树中仍然有一个子节点。当它碰到树上的叶子时,它将运行替换功能。

答案 1 :(得分:0)

以下是删除元素中所有文本节点的方法:

function removeTextNodes(element){
    let i = 0;
    while (i<element.childNodes.length){
        let node = element.childNodes[i];
        if (node.nodeType==3){
            element.removeChild(node);
        }else if(node.nodeType==1){
            removeTextNodes(node);
            i++;
        }
    }
}

答案 2 :(得分:0)

如果您打算记录文本节点的内容:

function logInnerText(elem) {
  if (elem.nodeType === Node.TEXT_NODE && elem.nodeValue.trim())
    console.log(elem.parentNode.nodeName + ' contains ' + elem.nodeValue.trim())
  elem.childNodes.length && elem.childNodes.forEach(el => logInnerText(el))
}

logInnerText(document.getElementById('someId'))