zoukankan      html  css  js  c++  java
  • 怎样获取当前节点及其所有后代节点的文本值

    Node.prototype.textContent

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <div id="test">hello<span>world</span></div>
        <script>
            var test = document.getElementById('test');
            console.log(test.nodeValue);
            console.log(test.firstChild.nodeValue);
            console.log(test.textContent);
            console.log(test.firstChild.textContent);
        </script>
    </body>
    </html>

    nodeValue 只获取当前节点的文本值, 如果节点类型不是文本 /属性 / 注释, 则会返回null. 

    textContent 获取当前节点及其所有后代节点的文本值, 没有获取到, 则返回一个空字符串.

    文档节点(document) 和 文档类型节点(doctype) 的textConent是一个空字符串, 如果想获取document的文本值(即整个网页文本) 可以通过 document.documentElement.textContent 获取

  • 相关阅读:
    一意孤行
    叶子书签
    漫舞
    男朋友
    ubuntu自动关机命令,ubuntu 无法关机解决方法
    情人节
    生死由天
    春暖花开
    android开发学习中的问题:error: device not found解决办法
    暗恋
  • 原文地址:https://www.cnblogs.com/aisowe/p/11504332.html
Copyright © 2011-2022 走看看