问题已经解决,属于个例问题;不具有普遍参考性
代码如下:
(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>
以上代码再执行前
如果
loginForm
未定义过,报ReferenceError: Can't find variable: loginForm
如果
loginForm
定义过并且是一个DOM元素,那么login_inputs为一个NodeList对象,这个对象没有forEach方法的,所以会报forEach is not a function
想要使用Array的forEach方法,使用函数的call/apply方法