JavaScript - onClick to get the ID of the clicked button

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

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


16条回答
  •  别跟我提以往
    2020-11-22 06:08

    In general, things are easier to keep organized if you separate your code and your markup. Define all of your elements, and then in your JavaScript section, define the various actions that should be performed on those elements.

    When an event handler is called, it's called within the context of the element that was clicked on. So, the identifier this will refer to the DOM element that you clicked on. You can then access attributes of the element through that identifier.

    For example:

    
    
    
    
    
    

提交回复
热议问题