在 javascript 中,我们有 event.srcElement
,它为我们提供了发生某些事件的元素。有什么方法可以让我们在 jQuery 中获取这个对象吗?
另外,
function myFun(){
alert ( $(this) ); // what do this object represent? Is it what i need ?
}
最佳答案
简短回答:如果您在事件处理程序中,那么是的,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
处理程序实际上位于父级上,并且您单击了内部的某些内容,则在本例中为 anchor :
<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
});
关于javascript - 如何获取jquery中触发事件的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3913399/