[英]input.addEventListener is not a function &
Firstly, I had this code 首先,我有这段代码
var input = document.querySelector("fieldset > input");
var label = document.querySelector("fieldset > label");
input.addEventListener("focus", function(event) {
if (label.classList.contains("label-blur")) {
label.classList.remove("label-blur");
label.classList.add("label-focus");
} else {
label.classList.add("label-focus");
}
});
input.addEventListener("blur", function(event) {
if (!input.value) {
label.classList.remove("label-focus");
} else {
label.classList.add("label-blur");
}
});
It works correctly. 它可以正常工作。
Then I changed 'querySelector' on 'querySelectorAll'. 然后,我在“ querySelectorAll”上更改了“ querySelector”。 I found information, that addEventListener doesn't work with querySelectorAll ( JS error object has no method addEventListener ).
我发现以下信息,即addEventListener不适用于querySelectorAll( JS错误对象没有方法addEventListener )。 So the resolution is something like this:
所以解决方法是这样的:
var input = document.querySelectorAll("fieldset > input");
var label = document.querySelectorAll("fieldset > label");
for(var i=0; i < input.length; i++) {
input[i].addEventListener("focus", function(event) {
if (label.classList.contains("label-blur")) {
label.classList.remove("label-blur");
label.classList.add("label-focus");
} else {
label.classList.add("label-focus");
}
});
input[i].addEventListener("blur", function(event) {
if (!input.value) {
label.classList.remove("label-focus");
} else {
label.classList.add("label-blur");
}
});
};
But this way I have new mistakes: 但是这样我有新的错误:
Please, let me know, how to resolve this problems. 请让我知道如何解决此问题。
I tryed this way (recommended by Conor Hastings): 我尝试过这种方式(由Conor Hastings推荐):
for(var i=0; i < input.length; i++) {
input[i].addEventListener("focus", function(event) {
for(var j=0; j < label.length; j++) {
if (label[j].classList.contains("label-blur")) {
label[j].classList.remove("label-blur");
label[j].classList.add("label-focus");
} else {
label[j].classList.add("label-focus");
}
};
});
input[i].addEventListener("blur", function(event) {
for(var j=0; j < label.length; j++) {
if (!input.value) {
label[j].classList.remove("label-focus");
} else {
label[j].classList.add("label-blur");
}
};
});
};
It's working better, but every 'addEventListener' works wrong. 它工作得更好,但是每个“ addEventListener”都工作不正确。 Now just "label-focus" is working.
现在,仅“标签焦点”起作用。 And it's working for every i (if any input:focus, every label is shown).
它适用于每个i(如果有输入:focus,则显示每个标签)。
And 'let j=i' instead of 'for(var j=0; j < label.length; j++)' does not do anything. 并且用'let j = i'代替'for(var j = 0; j <label.length; j ++)'不会执行任何操作。
The same way you are accessing input through the i
in the for loop you need to do with the label in order to get the label in the same index as the input in the labels NodeList 在for循环中通过
i
访问输入的方式与对标签执行的操作相同,以便使标签与标签NodeList中的输入位于同一索引中
You'll also have to do the same thing inside of the input[i] event listener 您还必须在input [i]事件监听器中执行相同的操作
for(var i=0; i < input.length; i++) {
input[i].addEventListener("focus", function(event) {
if (label[i].classList.contains("label-blur")) {
label[i].classList.remove("label-blur");
label[i].classList.add("label-focus");
} else {
label[i].classList.add("label-focus");
}
});
input[i].addEventListener("blur", function(event) {
if (!input[i].value) {
label[i].classList.remove("label-focus");
} else {
label[i].classList.add("label-blur");
}
});
};
var inputs = document.querySelectorAll("fieldset > input"),
labels = document.querySelectorAll("fieldset > label");
for(var i=0; i < inputs.length; i++) {
var input = inputs[i],
label = labels[i];
input.addEventListener("focus", function(event) {
var input = this.input,
label = this.label;
if (label.classList.contains("label-blur")) {
label.classList.remove("label-blur");
label.classList.add("label-focus");
} else {
label.classList.add("label-focus");
}
}.bind({input: input, label: label}));
input.addEventListener("blur", function(event) {
var input = this.input,
label = this.label;
if (!input.value) {
label.classList.remove("label-focus");
} else {
label.classList.add("label-blur");
}
}.bind({input: input, label: label}));
};
If you are using ES6, then you should use let instead of var after the for loop 如果您使用的是ES6,则应在for循环后使用let而不是var
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.