【问题标题】:Is it faster to use selectors with the context parameter?使用带有上下文参数的选择器会更快吗?
【发布时间】:2015-10-26 01:48:25
【问题描述】:

将选择器与上下文参数一起使用而不是指定它会更快吗?

例子:

var source = $('option:selected', 'select#source').text();

对比

var source = $('select#source option:selected').text();

哪个更快,为什么?

【问题讨论】:

    标签: jquery jquery-selectors selector


    【解决方案1】:

    如果上下文参数是缓存值而不是选择器,则它主要是有用的:

    var source = $('select#source');
    var selected = $('option:selected', source);
    var nonselected = $('option:not(:selected)', source);
    

    【讨论】:

      猜你喜欢
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2011-01-26
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多