js DOM节点 childNodes 和 children
作者:
发光驴子 | 来源:发表于
2017-09-20 23:31 被阅读0次 <!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>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function ()
{
var oUl=document.getElementById('ul1');
for(var i=0; i< oUl.childNodes.length;i++){
// alert(oUl.childNodes[i].nodeType);
if(oUl.childNodes[i].nodeType==1){
oUl.childNodes[i].style.background='red';
}
}
var oUl2=document.getElementById("ul2");
for(var i=0; i<oUl2.children.length;i++){
// document.children.length 就是 document.childNodes 的兼容版
alert(oUl.children.length);
oUl.children.style.background='red';
}
//DOM 获取父节点,没有兼容性问题,不做实例
// document.parentNode
// 获取元素在页面上的实际位置的父级
// document.offsetParent
}
</script>
</head>
<body>
<ul id="ul1">
<li>11sdfsdfsd1</li>
<li>222</li>
<li>333</li>
</ul>
<ul id="ul2">
<li>qwe</li>
<li>qwe</li>
<li>qwe</li>
</ul>
</body>
</html>
本文标题:js DOM节点 childNodes 和 children
本文链接:https://www.haomeiwen.com/subject/yhpasxtx.html
网友评论