您的位置:首页 > Web前端 > HTML5

HTML5的selector选择DOM元素API.

2014-09-06 11:30 375 查看
在html5新增的API中,在JS中有直接选择DOM元素的API函数;

分别是

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

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

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

贴代码:

  

<title>无标题文档</title>
<script>
window.onload=function(){
var Box=document.querySelector('#box');
var A=document.querySelectorAll('.hi');
console.log(A.length);
Box.style.background='red';
}
</script>
</head>

<body>
<div id="box">
HelloWorld
</div>
<div class="hi">
HelloWorld
</div>
<div class="hi">
HelloWorld
</div>
</body>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐