jQuery.click() vs onClick

前端 未结 17 1480
情话喂你
情话喂你 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 07:59

    For better performance, use the native JavaScript. For faster development, use jQuery. Check the comparison in performance at jQuery vs Native Element Performance.

    I've done a test in Firefox 16.0 32-bit on Windows Server 2008 R2 / 7 64-bit

    $('span'); // 6,604 operations per second
    document.getElementsByTagName('span'); // 10,331,708 operations/sec
    

    For click events, check Native Browser events vs jquery trigger or jQuery vs Native Click Event Binding.

    Testing in Chrome 22.0.1229.79 32-bit on Windows Server 2008 R2 / 7 64-bit

    $('#jquery a').click(window.testClickListener); // 2,957 operations/second
    
    [].forEach.call( document.querySelectorAll('#native a'), function(el) {
        el.addEventListener('click', window.testClickListener, false);
    }); // 18,196 operations/second
    

提交回复
热议问题