<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
window.onload=function(){
var a=document.getElementsByTagName('p');
alert(a[0].nodeValue);//这里为什么弹出的事null啊?它不是获取文本内容吗
}
</script>
</head>
<body>
<p>aaa</p>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
alert(a[0].nodeValue);改成
alert(a[0].childNodes[0].nodeValue);只有文本节点的
nodeValue才有值,a[0]是一个p节点,它的子节点才是一个文本节点节点值啊。具体看看这个nodeValue不止可以获取文本节点的还可以获取属性节点值等等http://developer.51cto.com/art/201009/224912.htm
获取节点的值,上面的@hellnpeter已经给了你个网址,你可以去看看