JavaScript - onClick to get the ID of the clicked button

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

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


16条回答
  •  礼貌的吻别
    2020-11-22 06:28

    (I think the id attribute needs to start with a letter. Could be wrong.)

    You could go for event delegation...

    function reply_click(e) { e = e || window.event; e = e.target || e.srcElement; if (e.nodeName === 'BUTTON') { alert(e.id); } }

    ...but that requires you to be relatively comfortable with the wacky event model.

提交回复
热议问题