使用带有context参数的选择器会更快吗?

时间:2015-08-03 20:18:05

标签: jquery jquery-selectors selector

使用带有context参数的选择器而不是指定它会更快吗?

示例:

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

VS

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

哪个更快,为什么?

1 个答案:

答案 0 :(得分:3)

如果context参数是缓存值而不是选择器,那么context参数主要是有益的:

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