封装一个getElementByClassName的方法

代码如下,我封装了一个classname方法,为什么console不到box,显示undifined

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>百度遮罩拖拽</title>
        <style>
            *{margin:0;padding:0}
            body{background:url(images/baidu_demo.png) no-repeat top center;}
            .login a{position:absolute;top:0;right:0;color:#000000}
            .mask{height:100%;width:100%;background: #5E5E5E;opacity: 0.5;filter:Alpha(opacity=50);display:none;}
            .box{width:260px;height:auto;position:absolute;background:#fff;border:1px solid #5E5E5E}
            .box-title{height:36px;line-height:36px;background:#E3E3E3;text-align:center;}
            .box-close{background: url(images/close_def.png) no-repeat 2px 2px;width:40px;height:36px;position:absolute;right:0;top:0;cursor: pointer;}
            .input1{width:80%;height:30px;outline:none;margin:0 auto;display:block;}
            .input2{width:80%;height:30px;outline: none;margin:0 auto;display:block;}
            .forget-password{display:block;}
            .login-in{background: #FFA500;height:40px;width:100%;display:block;}
            
        </style>
    </head>
    <body>
        <div class="login">
            <a href="">登陆</a>
        </div>
        
        <div class="mask"></div>
        
        <div class="box">
            <div class="box-title">
                <h2>登陆通行证</h2>
                <div class="box-close"></div>
            </div>
            <input type="text" class="input1"  />
            <input type="password" class="input2" />
            <a href="#" class="forget-password">忘记密码</a>
            <a href="#" class="login-in">登陆</a>
            <a href="#" class="zhuce">立即注册</a>
        </div>
        <script>
        
            function getElementsByClassName(className,parent){
                var oParent=parent?document.getElementById("parent"):document;
                var oLis=oParent.getElementsByTagName("*");
                var arr=[];
                for(var i=0;i<oLis.legnth;i++){
                    if(oLis[i].className==className){
                        arr.push(oLis[i])
                    };
                    return arr;
                }
            };
            function g(id){
                return document.getElementById("id");
            };
            function autoCenter(el){
                var clientX=document.documentElement.clientWidth||document.body.clientWidth;
                var clientY=document.documentElement.clientHeight||document.body.clientHeight;
                el.style.left=(clientX-el.offsetLeft)/2+"px";
                el.style.top=(clientY-el.offsetTop)/2+"px";
            };
            function fullscreen(el){
                var clientX=document.documentElement.clientWidth||document.body.clientWidth;
                var clientY=document.documentElement.clientHeight||document.body.clientHeight;
                el.style.left=clientX+"px";
                el.style.top=clientY+"px";
            }
            console.log(getElementsByClassName("login"));
        </script>
    </body>
</html>
阅读 4.1k
3 个回答

两个错误

  1. for(var i=0;i<oLis.legnth;i++){ 这里拼写错误,应该是 oLis.length

  2. return arr; 应该放在 for 的外面

另外,建议你用 filterclassList.contains(),看起来简洁一点

function getElementsByClassName(className, parent) {
    var oParent = parent ? document.getElementById("parent") : document;
    var oLis = oParent.getElementsByTagName("*");
    return [].filter.call(oLis, function(e) {
        return e.classList.contains(className);
    });
}

题主标题少了个s?

for(var i=0;i<oLis.legnth;i++){
    if(oLis[i].className==className){
        arr.push(oLis[i])
    };
    return arr; // Error:由于是for循环遍历,不管if语句是否执行,该for循环只执行一次
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题