简体   繁体   中英

How to retrieve the attribute from event object of jQuery?

I am developing a web application. In my app I am using jQuery. But I am having a problem or a thing that I am so curious with jQuery that is I want to retrieve the attribute of event object not using $(this).

This is formal way

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

But this is what I want

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

I think you understand my second code. That is how I want to retrieve using event object. Is that possible?

e in your code is just the event object (interface) .

You're probably looking for the event.target and you'd have to wrap that in jQuery to use attr()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM