Html5 Selectors API

本文深入探讨了HTML5选择器API,包括querySelector和querySelectorAll方法的使用,并提供了丰富的实例和代码演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新QuerySelector方法

querySelector():根据指定的选择规则,返回在页面中找到的第一匹配元素

querySelectorAll():根据指定规则返回页面中所有相匹配的元素

实例:

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2   <head>
 3     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 4     <title>Selectors API Example</title>
 5     <style>
 6         .green{background-color:green; }
 7     </style>
 8   </head>
 9   <body>
10         <div id="d1">
11             <p><a href="http://www.sina.com.cn">SINA</a></p>
12             <input type='text' value='lingyibin' id='test'/>
13             <input type='text' value='jasonling' id='test2' disabled />
14             <span class='testSpan'> 这里是span的地盘! </span>
15             <ul>
16                 <li>111</li>
17                 <li class='2'>222</li>
18                 <li>333</li>
19             </ul>
20             <span><label>label000</label></span>            
21             <label>label111</label>
22         </div>
23         <label>label222</label></br>
24         <label>label333</label></br>
25         <label>label444</label></br>
26         
27         <script type="text/javascript">  
28             //alert(document.querySelector('div a')); // -> http://www.sina.com.cn/
29             //alert(document.querySelector('div a').innerHTML); // -> SINA
30             //alert(document.querySelectorAll('div a').length); // -> 1 
31             //alert(document.querySelectorAll('div a')[0]); // -> http://www.sina.com.cn/
32             //alert(document.querySelector('#test').value); // -> lingyibin 
33             //alert(document.querySelector('input:disabled').value); // -> jasonling 
34             //alert(document.querySelector('span:not(label)').innerHTML); // -> jasonling 
35             //alert(document.querySelectorAll('input[id^=test]')[1].value); // -> jasonling //以test开头的
36             //alert(document.querySelectorAll('input[id$=t2]').length); // -> 1 //以test结尾的,注意,id$=2就会错,不能以数字开头
37             //alert(document.querySelectorAll('input[id*=est]').length); // -> 2 //模糊匹配
38             //alert(document.querySelector('.testSpan').innerHTML); // -> 这里是span的地盘!
39             //alert(document.querySelector('ul li:nth-child(2)').innerHTML); // -> 222
40             //alert(document.querySelectorAll('ul li:nth-child(3n)')[0].innerHTML); // -> 333
41             //alert(document.querySelectorAll('ul li:nth-child(odd)')[1].innerHTML); // -> 333
42             //alert(document.querySelector('ul li:first-child').innerHTML); // -> 111
43             //alert(document.querySelector('ul li:last-child').innerHTML); // -> 333
44             //alert(document.querySelectorAll('li[class]')[0].innerHTML); // -> 222 //有class属性的li
45             //alert(document.querySelectorAll('div label')[0].innerHTML); // -> label000 //得到div里面所有的label
46             //alert(document.querySelectorAll('div > label')[0].innerHTML); // -> label111 //得到div里面的直接label,在本例中只有一个
47             //alert(document.querySelectorAll('div + label')[0].innerHTML); // -> label222 //注意,这里只能得到一个label
48             //alert(document.querySelectorAll('div ~ label')[1].innerHTML); // -> label333
49         </script>
50 
51   </body>
52 </html>
html5

 

posted on 2014-05-26 11:48 stickout 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/linhui/p/3752758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值