如何在jquery中获取触发事件的对象?

时间:2010-10-12 09:55:40

标签: javascript jquery html

在javascript中,我们event.srcElement为我们提供了发生某些事件的元素。有没有办法在jQuery中获取这个对象。

此外,

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

1 个答案:

答案 0 :(得分: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
});
相关问题