简体   繁体   English

Firefox event.target问题

[英]Firefox event.target issue

Generally speaking, and if I recall correctly I've used this at least a dozen times before, but for some god-forsaken reason it doesn't work since this morning... Somebody have a clue? 一般而言,如果我没记错的话,我至少已经使用了十二次,但是出于某种不可思议的原因,它从今天早上开始就不起作用了……有人知道了吗?

JSFiddle Example JSFiddle示例

HTML: HTML:

<button id="testID" onclick="foo()">
test me
</button>

<button id="testID2" onclick="foo(this)">
test me 2
</button>

JS: JS:

function foo(event){
alert(event.target.id);
}

In the first example, you pass undefined to event . 在第一个示例中,您将undefined传递给event In the second example, you pass the button to event . 在第二个示例中,将按钮传递给event

If you want the event object, then you have to pass the event object. 如果要使用事件对象,则必须传递事件对象。 That's called event , but I don't know how standard it is for it to be created for intrinsic event attributes. 这就是event ,但是我不知道为内部事件属性创建它的标准。

In general, you are better off binding your event handlers with JavaScript. 通常,最好将事件处理程序与JavaScript绑定。

 function foo(event){ alert(event.target.id); } var buttons = document.querySelectorAll("button"); for (var i = 0; i < buttons.length; i++) { buttons[i].addEventListener("click", foo); } 
 <button id="testID"> test me </button> <button id="testID2"> test me 2 </button> 

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

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