如何从jQuery的事件对象中检索属性

How to retrieve the attribute from event object of jQuery?

本文关键字:对象 检索 属性 事件 jQuery      更新时间:2023-09-26

我正在开发一个web应用程序。在我的应用程序中,我使用的是jQuery。但我对jQuery有一个问题或一件事很好奇,那就是我想不使用$(this)来检索事件对象的属性。

这是的正式方式

$('.selector').on('click',function(e){
  alert($(this).attr('attribute')) // I am retrieving attribute using $(this)
})

但这就是我想要的

$('.selector').on('click',function(e){
  alert(e.attr('attribute')) // I am retrieving from e
})

我想你明白我的第二个密码了。这就是我想要使用事件对象检索的方式。这可能吗?

代码中的

e只是事件对象(接口)。

您可能正在寻找event.target,并且必须将其封装在jQuery中才能使用attr()

$('.selector').on('click',function(e){
    alert( $(e.target).attr('attribute') );
});