不属性节点在DOM中计为子节点吗?
我的页面就像这5个身体中的孩子:文字> h1>文字> div>文字> // 5个孩子结束 但是,属性数量不计算在内吗? childNodes属性不应返回所有类型节点的数组吗?使用文字>属性>文字> h1> attribut>文字> div> attribut>文字> p //等......
我有这个页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DOM testing</title>
<script src="scripts/domTesting3.js" type="text/javascript" charset="utf-8"></script>
</head>
<body bgcolor="#cccccc">
<h1 id="test">Test</h1>
<div id="content">
<p>Examining the DOM2 Core and DOM2 HTML Recommendations</p>
<h2>Browsers</h2>
<ul id="browserList">
<li id="chromeListItem">
<a href="http://www.google.com/chrome/ "title="Get Chrome" id="chrome">Chrome</a>
</li>
<li id="firefoxListItem">
<a href="http://www.getfirefox.com/"title="Get Firefox" id="firefox">Firefox 5.0</a>
</li>
<li>
<a href="http://www.microsoft.com/windows/ie/downloads/" title="Get Microsoft Internet Explorer" id="msie">Microsoft Internet Explorer 9</a>
</li>
<li>
<a href="http://www.apple.com/macosx/features/safari/" title="Check out Safari" id="safari">Safari</a>
</li>
<li>
<a href="http://www.opera.com/download/" title="Get Opera" id="opera">Opera 9</a>
</li>
</ul>
</div>
</body>
</html>
使用此脚本:
function domTest() {
var le = document.body.childNodes.length;
alert(le);//alerts 5 children where are attribut nodes?
for(i = 0; i < le; i++) {
alert(document.body.childNodes[i].nodeType);
alert(document.body.childNodes[i].nodeName);
alert(document.body.childNodes[i].nodeValue);
}
}
window.onload = domTest;
答案 0 :(得分:0)
Attr对象继承Node接口,但由于它们实际上不是它们描述的元素的子节点,因此DOM不认为它们是文档树的一部分。因此,Node属性parentNode,previousSibling和nextSibling对Attr对象具有空值