HTML5中的 querySelector

本文详细解析HTML DOM操作与选择器API的使用方法,包括元素选择、属性获取、事件监听等核心概念,并通过实例代码演示如何高效地进行DOM操作。

原文地址:

http://www.cnblogs.com/lingyibin/articles/2135120.html


 

  <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
      <title>Selectors API Example</title>
      <style>
          .green{background-color:green; }
      </style>
    </head>
   <body>
         <div id="d1">
             <p><a href="http://www.sina.com.cn">SINA</a></p>
             <input type='text' value='lingyibin' id='test'/>
             <input type='text' value='jasonling' id='test2' disabled />
             <span class='testSpan'> 这里是span的地盘! </span>
             <ul>
                 <li>111</li>
                 <li class='2'>222</li>
                 <li>333</li>
             </ul>
            <span><label>label000</label></span>            
             <label>label111</label>
         </div>
         <label>label222</label></br>
         <label>label333</label></br>
         <label>label444</label></br>
         
         <script type="text/javascript">  
             //alert(document.querySelector('div a')); // -> http://www.sina.com.cn/
             //alert(document.querySelector('div a').innerHTML); // -> SINA
             //alert(document.querySelectorAll('div a').length); // -> 1 
             //alert(document.querySelectorAll('div a')[0]); // -> http://www.sina.com.cn/
             //alert(document.querySelector('#test').value); // -> lingyibin 
             //alert(document.querySelector('input:disabled').value); // -> jasonling 
             //alert(document.querySelector('span:not(label)').innerHTML); // -> jasonling 
             //alert(document.querySelectorAll('input[id^=test]')[1].value); // -> jasonling //以test开头的
             //alert(document.querySelectorAll('input[id$=t2]').length); // -> 1 //以test结尾的,注意,id$=2就会错,不能以数字开头
             //alert(document.querySelectorAll('input[id*=est]').length); // -> 2 //模糊匹配
             //alert(document.querySelector('.testSpan').innerHTML); // -> span的地盘!
             //alert(document.querySelector('ul li:nth-child(2)').innerHTML); // -> 222
             //alert(document.querySelectorAll('ul li:nth-child(3n)')[0].innerHTML); // -> 333
             //alert(document.querySelectorAll('ul li:nth-child(odd)')[1].innerHTML); // -> 333
             //alert(document.querySelector('ul li:first-child').innerHTML); // -> 111
             //alert(document.querySelector('ul li:last-child').innerHTML); // -> 333
             //alert(document.querySelectorAll('li[class]')[0].innerHTML); // -> 222 //有class属性的li
             //alert(document.querySelectorAll('div label')[0].innerHTML); // -> label000 //得到div里面所有的label
             //alert(document.querySelectorAll('div > label')[0].innerHTML); // -> label111 //得到div里面的直接label,在本例中只有一个
             //alert(document.querySelectorAll('div + label')[0].innerHTML); // -> label222 //注意,这里只能得到一个label
             //alert(document.querySelectorAll('div ~ label')[1].innerHTML); // -> label333
         </script>
 
   </body>
 </html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值