onclick=“” vs event handler

前端 未结 8 2189
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 00:20

If I want a function to be executed, I prefer doing inline js:

Click me

becaus

8条回答
  •  伪装坚强ぢ
    2020-11-22 00:48

    I'm not aware of and advantage of using inline handler vs attach the handler later. In my experience I prefer to use the inline method when I have to deal with dynamic elements, for example if I want to add an handler to each image and the number of images changes according to other data (ex images in db). In this case using inline allow me to add an handler to each image, otherwise i have to recalculate the number of images in the javascript file to attach and handler to each image.

    Of course, when you can use libraries like jQuery where you can easily get the list of elements inline aren't useful

提交回复
热议问题