javascript基础--className选取节点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        function getByClass(oParrent, sClass) {
            var aEle = oParrent.getElementsByTagName('*');
            var aResult = [];
            var i = 0;

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

        }
        window.onload = function() {
            var oUl = document.getElementById('ul1');
            var oBox = getByClass(oUl, 'box');
            var i = 0;
            for (i = 0; i < oBox.length; i++) {
                oBox[i].style.background = "yellow";
            }
            // var aLi = document.getElementsByTagName('li');
            // var i = 0;
            // // alert('1');
            // for (i = 0; i < aLi.length; i++) {
            //     if (aLi[i].className == 'box') {
            //         aLi[i].style.background = "red";
            //     }
            // }
        }
    </script>
</head>

<body>
    <ul id="ul1">
        <li></li>
        <li></li>
        <li class="box"></li>
        <li></li>
        <li></li>
        <li class="box"></li>
        <li class="box"></li>
        <li></li>
    </ul>
</body>

</html>

 

posted @ 2017-03-01 17:38  Mr_W_Blog  阅读(325)  评论(0编辑  收藏  举报