简体   繁体   English

试图理解传递“this”与对事件的引用之间的区别

[英]Trying to understand the difference between passing 'this' vs. a reference to the event

I am trying to understand the differences between passing 'this' to a function versus passing a reference to the event itself.我试图了解将“this”传递给函数与传递对事件本身的引用之间的区别。

I am testing with two separate divs and each has a separate function for mouseover and mouseout events.我正在用两个单独的 div 进行测试,每个 div 都有一个单独的鼠标悬停和鼠标移出事件功能。 I pass just 'this' to one function and I pass both 'this' and 'e' to the other.我只将“this”传递给一个函数,而将“this”和“e”传递给另一个函数。 My example is here: http://jsfiddle.net/jkolden/NQvaL/13/我的例子在这里: http : //jsfiddle.net/jkolden/NQvaL/13/

document.getElementById('output').onmouseover = function(e) {mousein(e, this);};
document.getElementById('myDiv').onmouseover = function() {mouseinAlt(this);};

It seems as though the 'this' keyword is always going to refer to the html element to which I attach my listener, but 'e' will refer to the child of that html element is being moused over;似乎“this”关键字总是指代我将侦听器附加到的 html 元素,但“e”将指代鼠标悬停在该 html 元素的子元素上; is that a correct statement?这是正确的说法吗? I'm just curious if I am understanding this properly and if my example is using these in an appropriate manner.我只是好奇我是否正确理解了这一点,以及我的示例是否以适当的方式使用这些。

this refers to the element to which you attached the event. this指的是您将事件附加到的元素。 e refers to the event object. e指的是事件对象。 Within that event object is a reference to the event's target (not always the same property in every browser, in IE it's srcElement ), which is the element on which the event was dispatched.在该事件对象中是对事件target的引用(在每个浏览器中并不总是相同的属性,在 IE 中它是srcElement ),它是调度事件的元素。

See more here: https://developer.mozilla.org/en-US/docs/Web/API/event.target在此处查看更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/event.target

暂无
暂无

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

相关问题 使用绑定与仅传递“ this”之间的性能差异 - Performance difference between using bind vs. just passing 'this' 将事件处理程序道具传递为箭头 function 或 function 参考之间的区别 - Difference between passing event handler prop as arrow function or function reference 传递事件和 $event 之间有区别吗? - Is there a difference between passing event and $event? 'this' vs. 'bind.(this)' vs. 将事件传递给事件处理程序,React JS vs. vanilla JavaScript - 'this' vs. 'bind.(this)' vs. passing an event to an event handler, React JS vs. vanilla JavaScript Javascript - 命名空间与闭包之间的区别? - Javascript - difference between namespace vs. closure? 将函数 VARIABLE 传递给事件侦听器与传递函数 REFERENCE 有什么区别? - What is the difference between passing in a function VARIABLE to an event listener versus passing in a function REFERENCE? 试图了解事件之间的jQuery的区别 - Trying to understand the difference between jquery on events 试图理解 JavaScript 中原型和构造函数的区别 - Trying to understand the difference between prototype and constructor in JavaScript Polymer 中“观察者”与“通知”(在属性块中)与“观察者”与“听众”之间的区别 - Difference between "observer" vs. "notify" (in property block) vs. "observers" vs. "listeners" in Polymer JS:我正在尝试理解事件发射器与事件侦听器 - JS: I am trying to understand event emitters vs event listeners
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM