javascript addEventListener在所有元素上触发
我肯定我错过了一些明显的东西,但你知道为什么下面的addEventListener代码到处都在运行吗(不仅仅是在submit按钮上) HTML: 到 我的打字错误:(更改此项:javascript addEventListener在所有元素上触发,javascript,validation,addeventlistener,attachevent,Javascript,Validation,Addeventlistener,Attachevent,我肯定我错过了一些明显的东西,但你知道为什么下面的addEventListener代码到处都在运行吗(不仅仅是在submit按钮上) HTML: 到 我的打字错误:(更改此项: if (el.addEventListener) { el = addEventListener('click', validate, false); 为此: if (el.addEventListener) { el.addEventListener('click', validate,
if (el.addEventListener) {
el = addEventListener('click', validate, false);
为此:
if (el.addEventListener) {
el.addEventListener('click', validate, false);
冗长的评论
在代码中:
// inputs is an HTMLCollection of all the inputs
var inputs = document.getElementsByTagName('input');
// No need for the -1
var inputs_length = inputs.length;
// Don't forget to declare counters too, very important
// Just use < length
for (var i=0; i<inputs_length; i++) {
// Instead of this very inefficient method
var inputs_value = document.getElementsByTagName('input')[i].value;
// use the collection you already have
var inputs_value = inputs[i].value;
if (inputs_value == "") {
alert('there is a text box empty');
}
}
//输入是所有输入的HTMLCollection
var inputs=document.getElementsByTagName('input');
//不需要-1
var inputs_length=inputs.length;
//别忘了申报柜台,这很重要
//只需使用 对于(var i=0;iCool)。为RobG的提示干杯
el.addEventListener('click', validate, false);
if (el.addEventListener) {
el = addEventListener('click', validate, false);
if (el.addEventListener) {
el.addEventListener('click', validate, false);
// inputs is an HTMLCollection of all the inputs
var inputs = document.getElementsByTagName('input');
// No need for the -1
var inputs_length = inputs.length;
// Don't forget to declare counters too, very important
// Just use < length
for (var i=0; i<inputs_length; i++) {
// Instead of this very inefficient method
var inputs_value = document.getElementsByTagName('input')[i].value;
// use the collection you already have
var inputs_value = inputs[i].value;
if (inputs_value == "") {
alert('there is a text box empty');
}
}