简体   繁体   中英

Get the clicked element ID in javascript

I have a jQuery like :

jQuery("#id1,#id2").click(function(){
     //do stuff
});

I am checking that either id1 or id2 is clicked then do something. But in the function I need to get which ID is clicked. Can somebody help me in that?

尝试这个...

var idClicked = jQuery(this).attr('id');

比较简单的版本是:

var idClicked = this.id;

You could try a simpler code: Add the same class to the elements you want to attach the click listener and when clicked just retrieve the attribute's value.

jQuery(".my-class").click(function(){
    var element_id = jQuery(this).attr("id");
});

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