javascript - js匿名函数作用域问题

查看:144
本文介绍了javascript - js匿名函数作用域问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

问题已经解决,属于个例问题;不具有普遍参考性
代码如下:

(function(){
    var loginForm = document.forms.loginForm;
    var login_inputs = loginForm.getElementsByTagName('input');
    ...
    login_inputs .forEach(function(item,index,array){});
})()

代码中报错:
login_inputs is not defined
login_inputs .forEach is not a function

loginForm测试正常
开发工具sublime

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        body,legend,input,button{
            margin: 0;
            padding: 0;
            font: 21px "微软雅黑";
        }
        #loginForm{
            width: 515px;
            margin: 150px auto;
            border: 1px solid #ddd;
        }
        #loginForm legend{
            padding-left:24px;
            box-sizing: border-box;
            width: 100%;
            height: 45px;
            line-height: 45px;
            background-color: #2D2D2D;
            color: #fff;
        }
        #loginForm fieldset{
            border: none;
        }
        #loginForm div{
            margin: 30px 65px;
        }
        #login{
            width: 100%;
            height: 43px;
            line-height: 43px;
            border: none;
            text-align: center;
            color: #fff;
            background-color: #347BD0;
            cursor: pointer;
        }
        .invalid{
            border-color: red;
            color: red;    
        }
        .disabled{
            background-color: #ddd;
            cursor: default;
        }
    </style>
</head>
<body>
    <form id="loginForm" action="/login">
        <legend>手机号码登录</legend>
        <fieldset>
            <div>
                <label for="mobile">手机号:</label>
                <input id="mobile" name="mobile" type="text">
            </div>
            <div>
                <label for="password">密 码:</label>
                <input id="password" type="password" name="password">
            </div>
            <div><button id="login">登 录</button></div>
        </fieldset>
    </form>

    <script type="text/javascript">
        (function(){
            var input_modile = document.getElementById('mobile'),
                input_password = document.getElementById('password'),
                loginBtn = document.getElementById('login'),
                loginForm = document.forms.loginForm,
                login_inputs = loginForm.getElementsByTagName('input');

            login_inputs = Array.prototype.slice.call(login_inputs);
                   
            
            function addCss(elm,clazz){
                elm.classList.add(clazz);
            }
            input_modile.addEventListener('blur', function(){
                var value = input_modile.value;
                var isNext = (/1\d{10}/).test(value);
                if(isNext == false){
                    addCss(input_modile,'invalid');
                }
            });
            
            login_inputs.forEach(function(item,index,array){
                item.addEventListener('focus', function(){
                    item.className = " ";
                })
            })

            input_password.addEventListener('submit', function(){
                var value = input_password.value;
                var isNext = (/.{6,16}/).test(value);
                if(!isNext){
                    addCss(input_password,'invalid');
                } else {
                    addCss(loginBtn,'disabled');
                }
            })
        })();
    </script>
</body>
</html>

解决方案

(function(){
    var login_inputs = loginForm.getElementsByTagName('input');
    ...
    login_inputs.forEach(function(item,index,array){});
})()

以上代码再执行前
如果loginForm未定义过,报 ReferenceError: Can't find variable: loginForm
如果loginForm定义过并且是一个DOM元素,那么login_inputs为一个NodeList对象,这个对象没有forEach方法的,所以会报forEach is not a function
想要使用Array的forEach方法,使用函数的call/apply方法

Array.prototype.forEach.call(login_inputs, function( item,index,array ){
  console.log(item);
});

这篇关于javascript - js匿名函数作用域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆