Tianhe Gao

如何理解 JS 中的 (e)

e 是将传递给事件处理程序的 event 对象的短 var 引用。

事件对象本质上有许多有趣的方法和属性,可以在事件处理程序中使用。

一些小例子:

1function doSomething(e) {
2  if (!e) var e = window.event;
3  alert(e.type);
4}
1function doSomething(e) {
2  var targ;
3  if (!e) var e = window.event;
4  if (e.target) targ = e.target;
5  else if (e.srcElement) targ = e.srcElement;
6  if (targ.nodeType == 3) targ = targ.parentNode;
7}

https://stackoverflow.com/a/10323409/12539782

https://www.quirksmode.org/js/events_properties.html


No notes link to this note

Welcome to tell me your thoughts via "email"
UP