obj1[i].nodeName显示正常,修改obj0.childNodes[i]的背景也正常,
可是obj0.childNodes[i]总为空,而obj0.childNodes[i].nodeValue总为null,难道不能获得对象的值??
代码:
<!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>无标题文档</title>
<script type="text/javascript">
aa=function(){
var obj0=document.getElementById("a22");
var obj1=obj0.childNodes;
for(i=0;i<obj1.length;i++)
{
alert(obj1[i].nodeName)
obj1[i].onmouseover=function(){this.style.background="#aacc00"}
alert(obj1[i])
alert(obj1[i].nodeValue)
}
}
window.onload=aa;
</script>
</head>
<body>
<div id="a22">
<a title="11">
<input type="text" value="打开窗口">ff
</a>
<a title="11">
<input type="text" value="打开窗口">rr
</a>
</div>
</body>
</html>
4 个解决方案
alert(obj1[i].innerText);
Syntax
HTML N/A
Scripting object.nodeValue [ = sValue ]
Possible Values
sValue Variant that specifies or receives the node value or null.
The property is read/write. The property has no default value.
Expressions can be used in place of the preceding value(s), as of Microsoft® Internet Explorer 5. For more information, see About Dynamic Properties.
Remarks
If the object is a TextNode, the nodeValue property returns a string representing the text contained by the node.
Expressions cannot be used to change the nodeValue of a TextNode object.
If the object is an attribute object retrieved from the attributes collection, the nodeValue property returns the value of the attribute if it has been specified, or null otherwise.
If the object is an element, the nodeValue returns null. Use the nodeName property to determine the element name.
看下以上描述就明白了
<!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=GBK" />
<title>无标题文档</title>
<script type="text/javascript">
aa=function(){
var obj0=document.getElementById("a22");
var obj1=obj0.childNodes;
//alert("length ==== " + obj1.length);
for(i=0;i<obj1.length;i++)
{
//alert(obj1[i].nodeName)
//obj1[i].onmouseover=function(){this.style.background="#aacc00"}
//for(var a in obj1[i])
//{
// alert(a);
//alert(obj1[i].nodeValue)
//}
alert(obj1[i].innerHTML);
}
}
window.onload=aa;
</script>
</head>
<body>
<div id="a22">
<a title="11" id = "a1">
<input type="text" value="打开窗口">ff
</a>
<a title="11" id = "a2">
<input type="text" value="打开窗口">rr
</a>
</div>
</body>
</html>