jQuery.click() vs onClick

前端 未结 17 1482
情话喂你
情话喂你 2020-11-21 07:35

I have a huge jQuery application, and I\'m using the below two methods for click events.

First method

HTML

17条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 08:03

    IMHO, onclick is the preferred method over .click only when the following conditions are met:

    • there are many elements on the page
    • only one event to be registered for the click event
    • You're worried about mobile performance/battery life

    I formed this opinion because of the fact that the JavaScript engines on mobile devices are 4 to 7 times slower than their desktop counterparts which were made in the same generation. I hate it when I visit a site on my mobile device and receive jittery scrolling because the jQuery is binding all of the events at the expense of my user experience and battery life. Another recent supporting factor, although this should only be a concern with government agencies ;) , we had IE7 pop-up with a message box stating that JavaScript process is taking to long...wait or cancel process. This happened every time there were a lot of elements to bind to via jQuery.

提交回复
热议问题