如何将一键式事件处理程序用于多个按钮 [英] How to use one click event handler for multiple buttons
本文介绍了如何将一键式事件处理程序用于多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有4个具有不同ID的按钮.当我单击它们时,我需要执行一项功能.所以我需要这样的东西:
I have 4 buttons with different id's. When i click on them i need to perform one function. So i need something like this:
$('#button #button').click(function()
推荐答案
要一次性选择4个按钮,请输入以下内容:
To select the 4 buttons at once you write the following:
$('#button1, #button2, #button3, #button4').click(function(event){ /* code here */ });
如果您需要确定按下了哪个按钮,请输入以下内容:
If you need to identify which button was pressed, write something like:
$('#button1, #button2, #button3, #button4').click(function(event){
if($(event.target).attr('id')=='button1'){
/* specific code for button1 */
} else if($(event.target).attr('id')=='button2'){
/* specific code for button2 */
} else if($(event.target).attr('id')=='button3'){
/* specific code for button3 */
} else if($(event.target).attr('id')=='button4'){
/* specific code for button4 */
}
});
这篇关于如何将一键式事件处理程序用于多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文