子节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script type="text/javascript">
window.onload=function(){
var ou=document.getElementById(‘ul1‘)
// console.log(ou.childNodes.length)
// childNodes包括文本节点、元素节点
// alert(ou.childNodes.length)
// alert(ou.children.length) --- 常用
for (var i = ou.childNodes.length - 1; i >= 0; i--) {
// nodeType==3 -> 文本节点
// nodeType==1 -> 元素节点
// alert(ou.childNodes[i].nodeType)
if(ou.childNodes[i].nodeType==1){
ou.childNodes[i].style.background=‘red‘
}
}
}
</script>
<body>
<!-- dom基础 -->
<ul id="ul1">
<li>我是元素节点</li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
我是文本节点
</body>
</html>原文:http://f1yinsky.blog.51cto.com/12568071/1939095