zoukankan      html  css  js  c++  java
  • HTML5的selector选择DOM元素API.

    在html5新增的API中,在JS中有直接选择DOM元素的API函数;

    分别是

      document.querySelector('.类名 或者 #id名');返回所选符合元素的第一个元素

      document.querySelectorAll('.类名 或者 #id名');返回所选符合元素的元素集合,有length属性.

    该API函数在IE7标准模式下不兼容,其他的还行.

    贴代码:

      

     1 <title>无标题文档</title>
     2 <script>
     3     window.onload=function(){
     4         var Box=document.querySelector('#box');
     5         var A=document.querySelectorAll('.hi');
     6         console.log(A.length);
     8         Box.style.background='red';
     9     }
    10 </script>
    11 </head>
    12 
    13 <body>
    14     <div id="box">
    15         HelloWorld
    16     </div>
    17     <div class="hi">
    18         HelloWorld
    19     </div>
    20     <div class="hi">
    21         HelloWorld
    22     </div>
    23 </body>

      

  • 相关阅读:
    C++ 四种cast 用法
    Wannafly挑战赛1 B Xorto
    python里的闭包
    编译器对类的编译顺序
    class和struct
    typedef类型别名
    decltype类型指示符
    左值和右值
    const限定符
    hdu5678 树上第k小
  • 原文地址:https://www.cnblogs.com/hellome/p/3959259.html
Copyright © 2011-2022 走看看