[英]AddEventListener for multiple elements doesn't work with “focus” event
I'm guessing I have a basic error in thinking but I just can't get around it. 我猜我在思考方面有一个基本错误,但我无法绕过它。
I have a couple of text fields which I want to add an EventListener to. 我有几个文本字段,我想添加一个EventListener。 I put them all in one class and added the EventListener to this class.
我将它们全部放在一个类中,并将EventListener添加到此类中。 Everyhing works perfect when event of choice is "click".
当选择的事件是“点击”时,Everyhing工作完美。 But when I change it to "focus" nothing happens.
但当我把它变成“焦点”时,没有任何反应。 Why is that?
这是为什么?
this works: 这工作:
document.getElementById('parent').addEventListener('click', emptyField, false);
this doesn not: 这不是:
document.getElementById('parent').addEventListener('focus', emptyField, false);
text fields: 文字字段:
function emptyField(e){ var clicked = e.target; if (clicked.value == clicked.name) { clicked.value=''; if (clicked.id=='password') { clicked.type='password'; } } }
<class id="parent"> <input type="text" name="USERNAME" id="username" value="USERNAME"><br> <input type="text" name="PASSWORD" id="password" value="PASSWORD" ><br> </class>
The click
event bubbles up to ancestor elements. click
事件冒泡到祖先元素。
The focus
event does not, and it is the <input>
that is being focussed, not the outer element. focus
事件没有,正是focus
的<input>
,而不是外部元素。
I think you have to use querySelectorAll()
that will return all the input
s : 我认为你必须使用
querySelectorAll()
来返回所有input
:
var fields = document.querySelectorAll('#parent input');
And use loop to attach focus
event to every field : 并使用循环将
focus
事件附加到每个字段:
for (var i = 0; i < fields.length; i++) {
fields[i].addEventListener('focus', emptyField, false);
}
Hope this helps. 希望这可以帮助。
If you want to attach an event to several elements without an explicit loop, you can use a helper function: 如果要在没有显式循环的情况下将事件附加到多个元素,可以使用辅助函数:
function attachEvents(elementList, eventName, handlerFunction) {
if(typeof elementList == "string")
elementList = document.querySelectorAll(elementList);
for(var i = 0; i < elementList.length; i++)
elementList[i].addEventListener(eventName, handlerFunction);
}
You call it like this: 你这样称呼它:
attachEvents("#area button", "click", function(event) {
this.style.backgroundColor = "red";
});
Or like this: 或者像这样:
attachEvents(document.getElementById("area").getElementsByTagName("button"), "click", function(event) {
this.style.backgroundColor = "red";
});
You don't always want document.querySelectorAll
- doing it yourself means you also do things like some_element.querySelectorAll
which is really nice when working with things that aren't yet part of the document, or have no unique selector. 你并不总是想要
document.querySelectorAll
- 自己动手意味着你也可以做像some_element.querySelectorAll
这样的事情,这对于处理尚未成为文档的一部分或者没有唯一选择器的东西来说非常好。
But regardless, putting the loop in a helper function gives you that jquery-esque one-liner without a huge library, it is just a few lines of simple code. 但无论如何,将循环放在辅助函数中会为你提供jquery-esque单行程,而不需要庞大的库,它只是几行简单的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.