In jQuery, is it faster to check if an element exists before trying to bind event handlers?

前端 未结 4 561
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 03:38

Is it faster to first check if an element exists, and then bind event handlers, like this:

if( $(\'.selector\').length ) {
    $(\'.selector\').on(\'click\',         


        
4条回答
  •  既然无缘
    2020-12-20 04:14

    $('.selector').on('click',function() { // Do stuff on click } will bind if element exists otherwise not. So this is faster.

提交回复
热议问题