求大神看看为什么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>
阅读 3.5k
2 个回答

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

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

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题