2010-10-12 70 views
2

在JavaScript中,我们有event.srcElement,它给了我们一些事件发生的元素。有没有什么办法可以让我们在jQuery中获得这个对象。如何获取在jQuery中触发事件的对象?

此外,

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

回答

4

简短的回答:如果您是在事件处理中则是,this是你关心什么的时间。


内的事件处理程序this指的是你后或需要访问event object传递什么,比如:

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

例如,如果你click处理程序实际上是在家长和你点击了里面的东西,在这种情况下,锚:

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

有了这个处理程序:

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