jQuery(“sel1”)。add(" sel2")和jQuery(" sel1,sel2")是否有任何区别 [英] Is there any difference in jQuery("sel1").add("sel2") and jQuery("sel1, sel2")

查看:86
本文介绍了jQuery(“sel1”)。add(" sel2")和jQuery(" sel1,sel2")是否有任何区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Well, the question is in the title. Is there any difference (performance, caveats) between multiple selector

jQuery("selector1, selector2")

并使用添加元素到选择

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


推荐答案

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

Yes, the first will create a jQuery object that contains elements matched by both selectors. The second will create an object that has elements matched by the first selector, then create and return a new object with both, without modifying the first object.

例如:

var jq1 = $('h1, h2'); // Will contain all <h1> and <h2> 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 <h3> elements.

这篇关于jQuery(“sel1”)。add(&quot; sel2&quot;)和jQuery(&quot; sel1,sel2&quot;)是否有任何区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆