JavaScript中的nodeName nodeType nodeValue区别

在JavaScript中,存在有nodeName 、nodeType、 nodeValue这三个属性,今天我们来了解下JavaScript中的nodeName 、nodeType 、nodeValue区别

nodeName

nodeName 属性含有某个节点的名称。

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

注释:nodeName 所包含的 XML 元素的标签名称永远是大写的.


nodeValue

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

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

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

nodeType 属性可返回节点的类型。

最重要的节点类型是:
元素类型 节点类型
元素element 1
属性attr 2
文本text 3
注释comments 8
文档document 9

HTML文件:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DOM标准</title>
<script type="text/javascript" src="test.js"></js>
</head>
<body>
<h1 id="h1">An HTML Document</h1>
<p id="p1">This is a <i>W3C HTML DOM</i> document.</p>
<p><input id="btnDemo1" type="button" value="取H1 Element节点值"></p>
<p><input id="btnDemo2" type="button" value="取H1 Element节点文本"></p>
<p><input id="btnDemo3" type="button" value="取Document Element节点文本"></p>
<p><input type="button" alt="这是个演示按钮" title="演示按钮提示标题" name="btnShowAttr" id="btnShowAttr" value="按钮节点演示" /></p>
</body>
</html>

 

JS:

function showElement(){
var element=document.getElementById("h1");//h1是一个<h1>标签
alert('nodetype:'+element.nodeType);//nodeType=1
alert('nodeName:'+element.nodeName);
alert('nodeValue:'+element.nodeValue); //null
alert('element:'+element);
}

function showText(){
var element=document.getElementById("h1");
var text=element.childNodes[0];
alert('nodeType:'+text.nodeType);  //nodeType=3
alert('nodeValue:'+text.nodeValue);  //文本节点的nodeValue是其文本内容
text.nodeValue=text.nodeValue+"abc"; //文本内容添加修改删除等等。
alert('nodeName:'+text.nodeName);
alert(text.data);   //data同样是其内容,这个属性下同样可以增删改。
}

function showDocument(){
alert('nodeType:'+document.nodeType);  //9
alert('nodeName:'+document.nodeName);
alert(document);
}

function showAttr(){
var btnShowAttr=document.getElementById("btnShowAttr"); //演示按钮,有很多属性
var attrs=btnShowAttr.attributes;
for(var i=0;i<attrs.length ;i++){
var attr=attrs[i];
alert('nodeType:'+attr.nodeType); //attribute 的nodeType=2
alert('attr:'+attr);
alert('attr.name:'+attr.name+'='+attr.value);
}
}

function demo(){
var btnDemo1=document.getElementById("btnDemo1");
btnDemo1.onclick=showElement; //按钮1取节点nodetype值
var btnDemo2=document.getElementById("btnDemo2");
btnDemo2.onclick=showText;
var btnDemo3=document.getElementById("btnDemo3");
btnDemo3.onclick=showDocument;
var btnShowAttr=document.getElementById("btnShowAttr");
btnShowAttr.onclick=showAttr;
}
window.onload=demo;

  

1. nodeName属性: 节点的名字。

如果节点是元素节点,那么返回这个元素的名字。此时,相当于tagName属性。
比如:
<p>aaaa</p>  : 则返回 p ;
如果是属性节点,nodeName将返回这个属性的名字。
如果是文本节点,nodeName将返回一个#text的字符串。

另外我要说的是: nodeName属性是一个只读属性,不能进行设置.(写)
它返回 大写字母的值。

 

2. nodeType属性 : 返回一个整数,代表这个节点的类型。
我们常用的3中类型:
nodeType == 1  : 元素节点
nodeType == 2  : 属性节点
nodeType == 3  : 文本节点
如果想记住的话,我们可以这么去记:
比如:

<p  title="cssrain" >test</p>   从前往后读: 你会发现先是元素节点(1),然后是属性节点(2),最后是文本节点(3),这样你就很容易记住了 nodeType分别代表什么类型了。(我总结的一点小技巧, ^_^。)

nodeType属性经常跟 if 配合使用,以确保不会在错误的节点类型上 执行错误的操作。
比如:
function cs_demo(mynode){
      if(mynode.nodeType == 1){
              mynode.setAttribute("title","demo");
        }
}
代码解释: 先检查mynode的nodeType属性,以确保它所代表的节点确实是 一个元素节点。
和nodeName属性一样,他也是只读属性,不能进行设置.(写)。

 

3. nodeValue属性 : 返回一个字符串,这个节点的值。
如果节点是元素节点,那么返回null;(注意下)
如果是属性节点,nodeValue将返回这个属性的值。
如果是文本节点,nodeValue将返回这个文本节点的内容。
比如:
<div id="c">aaaaaaaaaaaaaaaa</div>

<SCRIPT LANGUAGE="JavaScript">
 var c= document.getElementById("c");
 alert(  c.nodeValue  );//返回null

 nodeValue是一个可以读、写的属性。 但它不能设置元素节点的值。
再看看下面的例子:
<div id="c">aaaaaaaaaaaaaaaa</div>

 var c= document.getElementById("c");
  c.nodeValue =" dddddddddddd"; //不能设置
  //alert( c.firstChild.nodeValue ) //元素节点 包括属性节点和文本节点。
  c.firstChild.nodeValue =  "test"//能设置

当然我们为了确保能正确运行:可以加一段代码:
<div id="c">aaaaaaaaaaaaaaaa</div>

var c= document.getElementById("c");
  c.nodeValue =" dddddddddddd"; //不能设置
  //alert( c.firstChild.nodeValue )
  if( c.firstChild.nodeType==3 ){ //判断是不是 文本节点
  c.firstChild.nodeValue =  "test"//能设置
  }

//可以看出,如果要设置元素节点,不能直接设置,而必须先使用firstChild或者lastChild等 然后设置nodeValue.
nodeValue一般只用来设置 文本节点的值。如果要刷新属性节点的值,一般使用setAttribute().

 

你可能感兴趣的:(JavaScript,html,C++,c,C#)