Jquery - event.target和此关键字之间的区别? [英] Jquery - Difference between event.target and this keyword?
问题描述
event.target
和
之间的区别是什么?
What is the difference between event.target
and this
?
我想说,我有
$("test").click(function(e) {
$thisEventOb = e.target;
$this = this;
alert($thisEventObj);
alert($this);
});
我知道警报会弹出不同的值。任何人都可以解释差异?感谢一百万。
I know the alert will pop different value. Anyone could explain the difference? Thanks a million.
推荐答案
如果您点击事件的元素,他们将是一样的。但是,如果您点击小孩并且它发生泡沫,那么这个
是指此处理程序绑定到的元素,而 e.target
仍然是事件起源的元素。
They will be the same if you clicked on the element that the event is rigged up to. However, if you click on a child and it bubbles, then this
refers to the element this handler is bound to, and e.target
still refers to the element where the event originated.
你可以看到这里的区别: http://jsfiddle.net/qPwu3/1/
You can see the difference here: http://jsfiddle.net/qPwu3/1/
给定标记:
<style type="text/css">div { width: 200px; height: 100px; background: #AAAAAA; }</style>
<div>
<input type="text" />
</div>
如果你这样做:
$("div").click(function(e){
alert(e.target);
alert(this);
});
点击< input>
将提醒输入,然后是div,因为输入源于事件,div在冒泡时处理它。但是如果你有这样的话:
A click on the <input>
would alert the input, then the div, because the input originated the event, the div handled it when it bubbled. However if you had this:
$("input").click(function(e){
alert(e.target);
alert(this);
});
它总是提醒输入两次,因为它既是事件的原始元素,也是一个处理它。
It would always alert the input twice, because it is both the original element for the event and the one that handled it.
这篇关于Jquery - event.target和此关键字之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!