Jquery - Difference between event.target and this keyword?

折月煮酒 提交于 2019-12-03 09:40:55

问题


What is the difference between event.target and this?

Let's say I have

$("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.


回答1:


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.

You can see the difference here: http://jsfiddle.net/qPwu3/1/

given this markup:

<style type="text/css">div { width: 200px; height: 100px; background: #AAAAAA; }​</style>    
<div>
    <input type="text" />
</div>​

If you had this:

$("div").click(function(e){
  alert(e.target);
  alert(this);
});

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.




回答2:


Events can be attached to any element. However, they also apply to any elements within said object.

this is the element that the event is bound to. e.target is the element that was actually clicked.

For example:

<div>
  <p>
    <strong><span>click me</span></strong>
  </p>
</div>
<script>$("div").click(function(e) {
  // If you click the text "click me":
  // e.target will be the span
  // this will be the div
});  </script>



回答3:


Crispy answer

this gives you the reference of the DOM element where the event is actually attached.

event.target gives you the reference of the DOM element where the event occurs.

Long answer

jQuery(document).ready(function(){
    jQuery(".outer").click(function(){
	   var obj = jQuery(event.target);
	   alert(obj.attr("class"));
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
        Outer div starts here
	<div class="inner">
	    Inner div starts here
	</div>
</div>

When you run the above code snippet you will see that event.target is alerting the class name of the div that is actually clicked.

However this will give the reference of the DOM object on which the click event is bind. Check the below code snippet to see how this works, always alerting the class name of the div on which the click event is bind even if you click the inner div.

jQuery(document).ready(function(){
  jQuery(".outer").click(function(){
    var obj = jQuery(this);
    alert(obj.attr("class"));
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
    Outer div starts here
    <div class="inner">
	    Inner div starts here
    </div>
</div>


来源:https://stackoverflow.com/questions/2654141/jquery-difference-between-event-target-and-this-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!