jquery是()来匹配所有元素,而不是任何元素

时间:2012-09-14 11:31:11

标签: jquery

如果我有这个HTML:

<input type="button" />
<input type="button" />
<input class="btn" type="button" />
<div class="btn"></div>
<div class="btn"></div>

我在jQuery对象中有3个按钮..说var buttons = $(':button');

然后我有另一个具有不同3组元素的对象,其中一个元素是相同的:var nextButtons = $('.btn');

是否有jQuery方法检查所有元素是否相同?如果任何元素匹配,is()似乎会返回true。

修改

我的意思是与is()完全相同的比较。我猜,对于第二组中的每一个,这个函数会为第一组中的每个元素调用is()

3 个答案:

答案 0 :(得分:5)

内置任何东西,但这很容易做到:

$.fn.all = function(selector) {
    return this.filter(selector).length == this.length;
}

<强> See it in action

答案 1 :(得分:0)

您可以尝试.filter()

var buttons= $(".btn").filter(function(i){
                   return $(this).is("input[type='button']");
              });

fiddle

答案 2 :(得分:0)

if (buttons.length === nextButtons.length && buttons.filter(nextButtons).length) {
     console.log("Same");
}