码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript 中childNodes和children的区别

时间:2014-06-02 05:39:42      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

bubuko.com,布布扣
<!DOCTYPE html>
<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">
        //childNodes  我很少用,感觉用的不是很方便
        //window.onload = function () {
        //    var oUl = document.getElementById("ul");
        //    for (var i = 0; oUl.childNodes.length; i++) {
        //        //nodeType=1,元素节点
        //        //nodeType=3,文本节点
        //        if (oUl.childNodes[i].nodeType == 1) {
        //            oUl.childNodes[i].style.background="red";
        //        }
        //    }
        //}

        window.onload = function () {
            var oU = document.getElementById("ul1");
            for (var i = 0; i < oU.children.length; i++) {
                //children  只包括元素节点,不包括文本节点
                oU.children[i].style.background = "red";
            }
        }
    </script>
</head>
<body>
    <ul id="ul1">
        <li></li>
        <li></li>
    </ul>
</body>
</html>
bubuko.com,布布扣
//children  只包括元素节点,不包括文本节点  childNodes既包括元素节点,又包括文本节点

可以通过alert(
oU.children.leght);和alert(OUl.childNodes.length);进行比较

Javascript 中childNodes和children的区别,布布扣,bubuko.com

Javascript 中childNodes和children的区别

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/alphafly/p/3763762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!