search

Home  >  Q&A  >  body text

javascript - 求大神看看为什么nodeValue显示NULL,明明有内容啊??

</head>
  <body>
    <h1>What to buy</h1>
    <p title="a gentle reminder">Don't <em>qwqw</em>forget to buy this stuff.</p>
    <p>This is just a test</p>
    
    <script>
    var paras = document.getElementsByTagName("p");
    alert(paras[0].childNodes[1].nodeValue);//为什么显示NULL??应该显示qwqw啊??
    </script>
  </body>
PHPzPHPz2902 days ago215

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-10 17:03:58

    只有文本节点的nodeValue才会返回真正的文本

    paras[0].childNodes[1].childNodes[0].nodeValue

    reply
    0
  • 高洛峰

    高洛峰2017-04-10 17:03:58

    paras[0].childNodes[1]取得<em>qwqw</em>
    内部文字为文本节点,因此还需取一层
    paras[0].childNodes[1].childNodes[0].nodeValue

    reply
    0
  • Cancelreply