jQuery串联选择器而不创建新实例? [英] jQuery concatenation of selectors without creating a new instance?
问题描述
是否可以连接两个jQuery选择器而无需在过程中创建新的选择器?一个示例如下:
Is is possible to concatenate two jQuery selectors without creating a new selector in the process? An example would be as follows:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<span class="a">A</span>
<span class="a">B</span>
<span class="b">C</span>
<script language="JavaScript" type="text/javascript">
<!--
var a = $('.a');
var b = $('.b');
var c = a.add(b);
console.log(a);
console.log(b);
console.log(c);
//-->
</script>
</body>
</html>
按上述编写时会产生以下选择器:
Which yields the following selectors when written as above:
a = [ A, B ]
b = [ C ]
c = [ A, B, C ]
我想对其进行修改以产生:
I would like to modify it to produce:
a = [ A, B, C ]
b = [ C ]
c = whatever
没有在此过程中创建新的选择器,等效于在选择器中的每个元素上使用数组的"push"方法.
Without creating a new selector in the process, the equivalent of using an Array's "push" method on each element in the selector.
推荐答案
You can use internal hack method push
:
a.push.apply(a, b.get()); // it accepts only native DOM elements
但是我不建议您使用它,因为它没有文档记录,仅供内部使用.而是使用简单的a = a.add(b)
.
But I wouldn't suggest you to use it, since it is not documented and provided for internal use only. Instead use simple: a = a.add(b)
.
REF: https://stackoverflow.com/a/14345461/1249581
这篇关于jQuery串联选择器而不创建新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!