Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery选择器比每个元素处理更复杂_Jquery_Performance_Selector - Fatal编程技术网

jQuery选择器比每个元素处理更复杂

jQuery选择器比每个元素处理更复杂,jquery,performance,selector,Jquery,Performance,Selector,我正在使用ez复选框插件,它只是将一个复选框包装成一个div,这样复选框看起来更好 <div class="ez-checkbox ez-checked"> <input type="checkbox" checked autocomplete="off" class="ezmark ez-hide ez-init"> </div> 第二,和第一个一样,但我们自己做“检查”工作 oPhotos .find('input') .no

我正在使用ez复选框插件,它只是将一个复选框包装成一个div,这样复选框看起来更好

<div class="ez-checkbox ez-checked">
  <input type="checkbox" checked autocomplete="off" class="ezmark ez-hide ez-init">
</div>
第二,和第一个一样,但我们自己做“检查”工作

oPhotos
    .find('input')
        .not(':checked')
        .each(function() {
            $(this)
                .prop('checked', true)
                .parent()
                    .addClass('ez-checked');
        });
oPhotos
    .find('input')
        .each(function() {
            if (! $(this).is(':checked')) {
                $(this)
                    .prop('checked', true)
                    .parent()
                        .addClass('ez-checked');
            }
        });
第三个-循环所有复选框,如果当前未选中-触发单击

oPhotos
    .find('input')
        .each(function() {
            if (! $(this).is(':checked')) $(this).click();
        });
第四-循环所有复选框,如果当前未选中-自己进行“检查”工作

oPhotos
    .find('input')
        .not(':checked')
        .each(function() {
            $(this)
                .prop('checked', true)
                .parent()
                    .addClass('ez-checked');
        });
oPhotos
    .find('input')
        .each(function() {
            if (! $(this).is(':checked')) {
                $(this)
                    .prop('checked', true)
                    .parent()
                        .addClass('ez-checked');
            }
        });

我跑了。事实证明,使用更复杂的选择器+使用
.not()
+处理事件而不是触发事件来过滤项目是最快的选择。最糟糕的选择是循环所有元素以过滤它们并触发事件。

或者让库为您执行所有检查和循环:
$(“.ez checkbox input[type='checkbox']:not(:checked)”。单击()小提琴:问题是-这种复杂的选择器(“.ez checkbox input[type='checkbox']:not(:checked)”)是否比处理每个元素更快?