首页 > Web开发 > 详细

js通过class获取元素

时间:2014-11-24 14:57:12      阅读:185      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8">
<title>通过class获取元素</title>
<style>
#box div { width:100px; height:100px; border:1px solid red; margin:10px; }
#box .bar { background:#ccc; }
</style>
<script>
window.onload=function (){
    var oDiv=document.getElementById(box);
    
    var arr=getByClass(oDiv, bar);
    
    alert(arr.length);
};

function getByClass(oParent, sClassName)
{
    var res=[];
    var aTmp=oParent.getElementsByTagName(*);
    
    for (var i=0; i<aTmp.length; i++)
    {
        if (aTmp[i].className.indexOf(sClassName) != -1)
       { res.push(aTmp[i]); } }
return res; } </script> </head> <body> <div id="box"> <div class="bar item"></div> <div class="bar"></div> <div class="bar"></div> <div></div> <div></div> <p class="bar"></p> </div> </body> </html>

 

js通过class获取元素

原文:http://www.cnblogs.com/heboliufengjie/p/4118622.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!