jQuery(“sel1”)中是否有任何区别.add(“sel2”)和jQuery(“sel1,sel2”)

好吧,问题在于标题。 多个选择器之间是否有任何差异(性能,警告)

jQuery("selector1, selector2") 

并使用add添加元素到选择:

 jQuery("selector1").add("selector2") 

是的,第一个将创建一个jQuery对象,其中包含两个选择器匹配的元素。 第二个将创建一个对象,该对象具有与第一个选择器匹配的元素,然后创建并返回两个新对象, 而不修改第一个对象

例如:

 var jq1 = $('h1, h2'); // Will contain all 

and

elements. jq1.add('h3'); alert(jq1.filter('h3').length); // Will alert 0, because the // original object was not modified. jq1 = jq1.add('h3'); alert(jq1.filter('h3').length); // Will alert the number of

elements.