childNodes,nodeType, nodeValue, firstChild, lastChild

<html>
<head>学习js</head>
<body>
<p id="test_p">2233232</p>
<script>
window.onload = showBodyCount;
function showBodyCount() {
var element_body = document.getElementsByTagName('body');//返回的是对象数组
// alert(element_body[0].childNodes); //element_body[0].childNodes 返回的也是对象数组
// alert(element_body[0].childNodes.length);
var textNode = document.getElementById('test_p');
alert(textNode.childNodes[0].nodeValue); //nodeValue 专门获取文本节点的属性值 为什么不能用textNode.nodeValue 因为p不是文本节点 是p下面的文本是文本节点,所以会使用到childNode
alert(textNode.firstChild.nodeValue); //同上等价
alert(textNode.childNodes[textNode.childNodes.length - 1].nodeValue); //最后一个子节点 因为这边p标签只有一个子节点,所以结果都是一样的,但是效果不一样
alert(textNode.lastChild.nodeValue); //这个与上面是等价的 肯定是喜欢这种方式吧
//nodeValue除了有读取值,还有设置值的功能
alert(textNode.firstChild.nodeValue = 'i Modified you'); //文本几点的内容被修改了
}
</script>
</body>
</html>

posted on 2015-04-02 09:26  西施先森  阅读(131)  评论(0)    收藏  举报

导航