JQuery assign events to buttons

前端 未结 8 836
小蘑菇
小蘑菇 2020-12-11 23:43

I have 50 dynamically generated HTML buttons as follows:




        
8条回答
  •  情深已故
    2020-12-11 23:57

    I would apply it to the parent element of the buttons. So if all of the buttons were in

    :

    $('#myButtons').on('click', 'button' function () {
        // Do stuff...
    });
    

    The key is to be specific enough that you do not have to specify each selector but not too lose as there may be other buttons, etc. on the page that you do not want to include.

    Updated code to include delegation.

提交回复
热议问题