日期:2014-05-16 浏览次数:20374 次
<div id="aaa" aaa="bbb"></div>
<script type="text/javascript">
var attr = document.getElementById('aaa').attributes;
for(var i = 0 ; i < attr.length ; ++i){
alert(attr[i].ownerElement.id);
}
</script>
<div id="foo" class="xxx"></div>hello<p id="foo2"></p><!-- comment -->
<script>
//元素节点 nodeType = 1
console.log(document.getElementById('foo').nodeType);
//属性节点 nodeType = 2
console.log(document.getElementById('foo').attributes['id'].nodeType);
//文本节点 nodeType = 3
console.log(document.getElementById('foo').nextSibling.nodeType);
//注释节点 nodeType = 8
console.log(document.getElementById('foo2').nextSibling.nodeType);
//文档节点 nodeType = 9
console.log(document.nodeType);
//fragment节点 nodeType = 11
console.log((document.createDocumentFragment()).nodeType);
</script>