zoukankan      html  css  js  c++  java
  • Javascript 中childNodes和children的区别

    <!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>
    //children  只包括元素节点,不包括文本节点  childNodes既包括元素节点,又包括文本节点

    可以通过alert(
    oU.children.leght);和alert(OUl.childNodes.length);进行比较
  • 相关阅读:
    多线程GCD
    根据UITouch 自定义手势
    KVC在数据解析中的应用
    oc 文件读写操作
    oc 字典应用实例-城市查询省份
    oc 字典应用实例-成绩科目排序
    oc 数组应用实例-验证码
    协议 protocol
    概念杂记
    OC 类的继承 方法重载重写
  • 原文地址:https://www.cnblogs.com/alphafly/p/3763762.html
Copyright © 2011-2022 走看看