JavaScript - onClick to get the ID of the clicked button

前端 未结 16 1230
名媛妹妹
名媛妹妹 2020-11-22 05:44

How do find the id of the button which is being clicked?


16条回答
  •  一向
    一向 (楼主)
    2020-11-22 06:13

    Button 1 Button 2 Button 3

    var reply_click = function() { 
         alert("Button clicked, id "+this.id+", text"+this.innerHTML); 
    } 
    document.getElementById('1').onclick = reply_click; 
    document.getElementById('2').onclick = reply_click; 
    document.getElementById('3').onclick = reply_click;
    

提交回复
热议问题