如何在我要连接的事件处理程序中获取对HTML DOM元素的引用?
var dashboard =
{
WireHandlers : function()
{
$(".approveButton")
.unbind("click")
.click(dashboard.approve)
},
approve : function()
{
// In here, I would like a reference
// to the HTML DOM element that was clicked
// Also, I would like its id
}
}
最佳答案
只需将一个参数添加到您的approve
函数(通常命名为event
)并在其上调用currentTarget
即可:
approve : function(event) {
// In here, I would like a reference
// to the HTML DOM element that was clicked
event.currentTarget
// Also, I would like its id
event.currentTarget.id
}
请注意,您还可以在
this
方法中使用approve
来引用单击的元素。approve : function() {
this // or $(this) if you want to use jQuery
}
请注意,在这种情况下,
target
和currentTarget
是可互换的。但是,在更复杂的情况下,请使用there are important differences between the two。