JQuery assign events to buttons

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

I have 50 dynamically generated HTML buttons as follows:




        
8条回答
  •  失恋的感觉
    2020-12-11 23:54

    Best way would be to delegate to the surrounding container, that way you only have one listener rather than 50. Use .on()

    https://api.jquery.com/on/

    If you must assign to each button, figure out a way to write only one selector, like this:

    $('button').click(function(){});
    

    Note your selector may need to be more specific to target just these 50 buttons, as @Drewness points out in the comments.

提交回复
热议问题