简体   繁体   中英

How to get the object which fired the event in jquery?

In javascript, we have event.srcElement that gives us the element on which some event is occured. Is there any way by which we can get this object in jQuery.

Also,

function myFun(){
  alert ( $(this) ); // what do this object represent? Is it what i need ?
}

Short answer: If you're inside an event handler then yes, this is what you care about most of the time.


Inside an event handler this refers to what you're after or if needed access the event object passed in, for example:

$(".element").click(function(e) {
  //this is the element clicked
  //e.target is the target of the event, could be a child element
});

For example if your click handler was actually on a parent and you clicked something inside, the anchor in this case:

<div id="parent"><a href="#">Test</a></div>

With this handler:

$("#parent").click(function(e) {
  //this is the <div id="parent"> element
  //e.target is the <a> element
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM