<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>
高洛峰2017-04-10 17:10:59
alert(a[0].nodeValue);
改成alert(a[0].childNodes[0].nodeValue);
只有文本节点的nodeValue
才有值,a[0]
是一个p节点
,它的子节点才是一个文本节点
迷茫2017-04-10 17:10:59
节点值啊。具体看看这个nodeValue不止可以获取文本节点的还可以获取属性节点值等等http://developer.51cto.com/art/201009/224912.htm