简体   繁体   中英

Multiple Button Click Events Jquery On

我可以将多个Button Click事件绑定到以下相同功能吗?

$(document).on("click", "#btn1 #btn2 #btn3", callbackFunction);

Use commas to separate the values:

$(document).on("click", "#btn1, #btn2, #btn3", callbackFunction);

Then you can determine which one called you by accessing the this object, so the following would alert the id of the element clicked:

$(document).on("click", "#btn1, #btn2, #btn3", function () { 
     alert($(this).attr("id"));
});

For example, this fiddle: http://jsfiddle.net/SFLDw/1/

Yes you can. However, you need to separate them with a comma.

$(document).on("click", "#btn1, #btn2, #btn3", callBackFunction);

If possible, it might be wise to assign a class to the buttons and use the class as the seletor. Any button with that class will be bound to the event. This will keep you from having to add additional selectors.

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