首页 > 其他 > 详细

DOM中元素节点、属性节点、文本节点

时间:2015-03-02 18:31:50      阅读:123      评论:0      收藏:0      [点我收藏+]

DOM中有12中节点,但最常用到的是元素节点,属性节点,文本节点。

元素节点的节点类型(nodeType)是1;

属性节点的节点类型(nodeType)是2;

文本节点的节点类型(nodeType)是3。

元素节点的 nodeName 是标签名称
属性节点的 nodeName 是属性名称
文本节点的 nodeName 永远是 #text

对于文本节点,nodeValue 属性包含文本。

对于属性节点,nodeValue 属性包含属性值。

nodeValue 属性对于文档节点和元素节点是不可用的。

nodeValue就是用来得到“文本元素的值”的,即只适用于“文本节点”;

看下例子

<html>
<head>
<script type="text/javascript">
function getNodeValue()
{
var nv=document.getElementById("td1").firstChild.nodeValue;
var nn=document.getElementById("tr1").nodeValue;
var nv1=document.getElementById("tr1").firstChild.nodeValue;
console.log("nv="+nv);//nv=Silence
console.log("nn="+nn);//nn=null
console.log("nv1="+nv1);//nv1=
console.log(document.getElementById("tr1").firstChild);//<TextNode textContent="\n ">
console.log(document.getElementById("td1").firstChild);//<TextNode textContent="Silence">
}
</script>
</head>
<body>
<table>
  <tr id="tr1">
    <td id="td1" >Silence</td>
    <td>bean</td>
    <td>happy</td>
  </tr>
</table>
<input type="button" value="按钮"  onclick="getNodeValue()"/>
</body>
</html>

 

 

DOM中元素节点、属性节点、文本节点

原文:http://www.cnblogs.com/MissBean/p/4309371.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!