Fly*_*Cat 9 html javascript jquery dom
event.target
和之间有什么区别this
?
让我说我有
$("test").click(function(e) {
$thisEventOb = e.target;
$this = this;
alert($thisEventObj);
alert($this);
});
Run Code Online (Sandbox Code Playgroud)
我知道警报会弹出不同的值.有人可以解释这个区别吗?太感谢了.
Nic*_*ver 18
如果您单击事件所绑定的元素,它们将是相同的.但是,如果单击子项并且它出现气泡,则this
引用此处理程序绑定的元素,并e.target
仍引用事件源自的元素.
你可以在这里看到不同之处:http: //jsfiddle.net/qPwu3/1/
鉴于此标记:
<style type="text/css">div { width: 200px; height: 100px; background: #AAAAAA; }?</style>
<div>
<input type="text" />
</div>?
Run Code Online (Sandbox Code Playgroud)
如果你有这个:
$("div").click(function(e){
alert(e.target);
alert(this);
});
Run Code Online (Sandbox Code Playgroud)
单击<input>
会提示输入,然后是div,因为输入发起了事件,div在冒泡时处理它.但是如果你有这个:
$("input").click(function(e){
alert(e.target);
alert(this);
});
Run Code Online (Sandbox Code Playgroud)
它总是会警告输入两次,因为它既是事件的原始元素,也是处理事件的原始元素.