zoukankan      html  css  js  c++  java
  • js中获取页面元素节点的几种方式

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <!--
    使ie以IE8的模式运行
    -->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" >
    <script type="text/javascript">

    /**
    * 得到页面元素节点的几种方式
    */

    //通过id选择器去得到
    function getDom01() {
    //获取一个
    var div = document.getElementById("box1");
    console.log(div);
    }

    //通过标签名得到元素
    function getDom02(){
    //会得到所有的 与名字相同的标签
    var divs = document.getElementsByTagName("div");
    console.log(divs[0]);
    }
    //根据名字得到
    function getDom03(){
    //只能得到 原生就有name属性的元素 不能是自定义的
    var div = document.getElementsByName("box2");
    document.getElementsByClassName("box1");
    console.log(div[0]);
    }
    //IE7 以及以前的版本 不支持
    function getDom04(){
    var div = document.querySelector("#box1");
    console.log(div);
    }
    //IE7 以及以前的版本 不支持
    function getDom05(){
    var span = document.querySelectorAll(".box > span");
    console.log(span.length);
    }
    </script>
    <style type="text/css">
    .box{
    border: 1px solid red;
    450px;
    height: 100px;
    }

    .box span{
    border-left: 1px solid green;
    border-right: 1px solid green;
    margin: 0 10px;
    padding: 0 5px;
    }
    </style>
    </head>
    <body>
    <input type="button" onclick="getDom01()" value="getDom01"/>
    <input type="button" onclick="getDom02()" value="getDom02"/>
    <input type="button" onclick="getDom03()" value="getDom03" name="box2"/>
    <input type="button" onclick="getDom04()" value="getDom04" name="box2"/>
    <input type="button" onclick="getDom05()" value="getDom05" name="box2"/>
    <hr/>
    <div class="box" id="box1" name="box2">
    <span>this is a span in div</span>
    <span>this is a span in div</span>
    <span>this is a span in div</span>
    <span>this is a span in div</span>
    </div>
    </body>
    </html>
  • 相关阅读:
    2018.8.20 Python之路---常用模块
    2018.8.16 正则表达式
    2018.8.15 python中的冒泡法排序
    2018.8.15 python 中的sorted()、filter()、map()函数
    2018.8.14 python中的内置函数(68个)
    2018.8.13 python中生成器和生成器表达式
    2018.8.10 python中的迭代器
    2018.8.9 python中的动态传参与命名空间
    python测试开发django(1)--开始Hello World!
    UPC-5120 Open-Pit Mining(最大权闭合子图)
  • 原文地址:https://www.cnblogs.com/hwgok/p/5734077.html
Copyright © 2011-2022 走看看