美文网首页
classNameSet

classNameSet

作者: Jadon7 | 来源:发表于2018-10-09 15:57 被阅读0次
<!DOCTYPE html>
<html>
<head>
    <title>classNameSet</title>
</head>
<script type="text/javascript">

    function getByClass(oParent,sClass)
    {
        var sClassName=[];
        var aEle=oParent.getElementsByTagName('*');

        for (var i = 0; i < aEle.length; i++) 
        {
             if (aEle[i].className==sClass)
             {
                sClassName.push(aEle[i]);
             } 
        }
        return sClassName;
    }

    window.onload=function()
    {
        var oUl=document.getElementById('ul1');
        
        var aBox=getByClass(oUl,'box');

        // alert(aBox);

        for (var i = 0; i < aBox.length; i++) {
            aBox[i].style.background='red';
        }   
    }
</script>
<body>
    <ul id="ul1">
        <li class="box"></li>
        <li class="box"></li>
        <li></li>
        <li class="box"></li>
        <li></li>
    </ul>
</body>
</html>

相关文章

网友评论

      本文标题:classNameSet

      本文链接:https://www.haomeiwen.com/subject/oufyaftx.html