简体   繁体   English

我应该使用哪个Event.target属性?

[英]Which Event.target Property Should I Use?

When finding which element an event originated from is there an event.target property that works best? 当找到事件起源于哪个元素时,是否有一个event.target属性最有效?

Take this block of code for example: 以下面的代码块为例:

document.addEventListener("keypress", function (event) {

    if (event.target.className == "input-field")
        callFunctionName();

}, false);

This same piece of code works whether the if statement contains event.target.nodeName == "INPUT" , event.target.dataset.field == "input" , etc. 无论if语句是否包含event.target.nodeName == "INPUT"event.target.dataset.field == "input"等,这段代码都有效。

Is there any reason why one of these properties should be used over the others or maybe why one is worse than the rest? 是否有任何理由为什么应将这些属性之一用于其他属性?或者为什么其中一个属性比其他属性差?

It depends on how specific you need to be. 这取决于您需要的具体程度。 In particular, your current: 特别是,您当前:

event.target.className == "input-field"

is not particularly flexible - this will work only if the element has that class , input-field , and that class only. 并不是特别灵活-仅当元素具有那个classinput-field和那个class时才起作用。 What if, later, you figure out that you want to add another class to that element, for styling or something? 如果以后发现要为该元素添加另一个类以进行样式设置或其他操作,该怎么办? Then, the above test will fail, and you'll have to go back and fix it. 然后,上述测试将失败,并且您必须返回并对其进行修复。

This same piece of code works whether the if statement contains event.target.nodeName == "INPUT" 无论if语句是否包含event.target.nodeName == "INPUT"这段代码均有效

This will match any input field on the page, which may well be undesirable - what if additional HTML you add later happens to include an <input> that you don't want to trigger this handler? 这将匹配页面上的任何 input字段,这可能是不希望的-如果以后添加的其他HTML恰巧包含不想触发此处理程序的<input>怎么办? Then, you'll have to come back and fix this. 然后,您必须返回并解决此问题。

event.target.dataset.field == "input"

This will result in the handler being triggered for any element with data-field="input" . 这将导致为任何具有data-field="input"元素触发处理程序。 While perhaps unlikely, what if such an element is added to the HTML later, one that you don't want this listener to be connected to? 尽管可能不太可能,但是如果以后将这样的元素添加到HTML中,而又又不想将此侦听器连接到该元素,该怎么办呢? Then, again, you'll have to come back and fix it. 然后,再次,您必须回来修复它。

It's usually a good idea to be as specific as possible when targeting elements. 在定位元素时,尽可能具体是一个好主意。 You can do this with Element.prototype.matches - pass it a selector string (selector strings are very flexible), and it will return a boolean indicating whether the selector string matches the element: 您可以使用Element.prototype.matches做到这一点-向其传递选择器字符串(选择器字符串非常灵活),它将返回一个布尔值,指示选择器字符串是否与元素匹配:

if (event.target.matches('input.input-field[data-field="input"]')) {
  callFunctionName();
}

No valid answer for that, depends of your case. 没有有效的答案,取决于您的情况。

For example: 例如:

If you want to do only with one target, you will use ID. 如果只想对一个目标执行操作,则将使用ID。

If you want to do with inputs type text you will use e.target.type 如果要处理输入文字,则将使用e.target.type

If you want to do with inputs than can be selects or inputs but have the same class in common you will use className.. 如果您要使用输入而不是选择或输入,但是具有相同的类,则可以使用className。

Think what you want and this will be the correct answer! 想想您想要什么,这将是正确的答案!

The most standard and reliable way to know what element triggered an event is with event.target . 知道触发事件的元素的最标准,最可靠的方法是event.target

What property(ies) you choose to access from that object really depends on what aspect of that object you are interested in. 您选择从该对象访问哪些属性实际上取决于您对该对象的哪个方面感兴趣。

nodeName always returns an all caps string containing the name of the HTML element (the tag name, not the element's name attribute or its id ). nodeName始终返回一个全大写的字符串,其中包含HTML元素的名称(标记名称,而不是元素的name属性或其id )。

 console.log(document.getElementById("test1").nodeName); // SELECT console.log(document.getElementById("test2").nodeName); // OPTION console.log(document.getElementById("test3").nodeName); // OPTION 
 <select id="test1"> <option id="test2">Choice 1</option> <option id="test3">Choice 2</option> </select> 

dataset is used for a completely different reason. dataset原因完全不同。 It returns a data attribute value for the data attribute specified after .dataset . 它为.dataset之后指定的data属性返回一个data属性值。 The only way this API would be comparable to event.target.nodeName would be if you had an element similar to this: 该API与event.target.nodeName相类似的唯一方法是,如果您具有与此类似的元素:

<input type="text" data-field="input">

which is unlikely. 这不太可能。 Here's an example of how values of data-* attributes in your HTML elements can be retrieved in JavaScript. 这是一个示例,说明如何在JavaScript中检索HTML元素中data-*属性的值。

 console.log(document.getElementById("test1").dataset.policyType); // Home 
 <div id="test1" data-policy-type="Homeowners">Policy Number: 1234546</div> 

The two APIs each serve their purpose, but they are not interchangeable. 这两个API各自都有其用途,但它们不可互换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM