single onclick function for buttons with a similar id pattern - JavaScript

前端 未结 4 479
耶瑟儿~
耶瑟儿~ 2021-01-21 03:24

I want to reduce the code.

function one() {
 console.log(\"hai\");
}

document.getElementById(\'dealsButton_1\').onclick = one;
document.getElementById(\'dealsBu         


        
4条回答
  •  [愿得一人]
    2021-01-21 03:32

    Are you looking for something like this:

    function onClick(){
      //single handler
    }
    
    $('[id*="dealsbutton_"]').click(onClick)
    

提交回复
热议问题