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 the开发者_开发知识库re 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
});
精彩评论