为什么* [checked]表现得像:使用jQuery的过滤方法时检查过? [英] Why does *[checked] act like :checked when using jQuery's filter method?

查看:90
本文介绍了为什么* [checked]表现得像:使用jQuery的过滤方法时检查过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续之前的问题。$
假设我在装有jQuery 1.5.2的页面上有两个复选框

Following on from a previous question.
Lets say I have two checkboxes on a page loaded with jQuery 1.5.2

<input id="test1" type="checkbox"/>
<input id="test2" type="checkbox"/>

然后让我们点击这两个来给每个选中财产。
现在让我们抓住所有复选框并过滤掉未经检查的复选框

Then lets click both of them to give each the checked property. Now lets grab all checkboxes and filter out the ones that aren't checked

var numOfCheckedBoxes = $("input:checkbox").filter("*[checked]").size();

现在根据上一个问题,我希望 numOfCheckedBoxes 等于 0 但它等于 2 。我现在应该使用 $(input:checkbox:checked) $(input:checkbox)。filter(:checked )

Now based on the answer to the previous question, I would expect numOfCheckedBoxes to equal 0 but it equals 2. I now I should just use $("input:checkbox:checked") or $("input:checkbox").filter(":checked").

这与我的不同

var numOfCheckedBoxes = $("#test1").filter("*[checked]").size();

设置 numOfCheckedBoxes 等于 0

这是的jsFiddle有问题的第一个场景
这是第二种情况的jsFiddle。

which sets numOfCheckedBoxes equal to 0.
Here is a jsFiddle of the first scenario in question. Here is a jsFiddle of the second scenario in question.

推荐答案

因为在现代浏览器中,DOM选择将使用 querySelectorAll 完成。

Because in modern browsers, the DOM selection will be done with querySelectorAll.

这意味着在进行DOM选择时, * [checked] 实际上会查找该特定属性。

This means that when doing DOM selection, *[checked] will actually look for that specific attribute.

当您在DOM选择中执行':checked'时,您使用的是 Sizzle 扩展是无效的CSS,所以 querySelectorAll 未被使用,因此 Sizzle 被使用,它看着属性以及属性。

When you do ':checked' in DOM selection, you're using a Sizzle extension that is invalid CSS, so querySelectorAll is not used, and therefor Sizzle is used, and it looks at the property as well as the attribute.

所以这里的区别是 .filter() 不能/不能使用 querySelectorAll ,这意味着您将获得给出的专有行为Sizzle 而不是有效的DOM行为。

So the difference here is that .filter() doesn't/can't use querySelectorAll, which means that you're going to get the proprietary behavior given by Sizzle instead of the valid DOM behavior.

I如果 Sizzle 实际上是 querySelectorAll 的兼容性补丁,那会很棒,但它不是,所以你必须处理导致的不一致。

It would be great if Sizzle was actually a compatibility patch for querySelectorAll, but it isn't, so you have to deal with the resulting inconsistencies.

编辑:

似乎有一个优化,如果针对多个元素调用 filter() Sizzle 被使用,但是如果只有一个元素,则使用是()

It appears as though there's an optimization where if filter() is called against multiple elements, Sizzle is used, but if only one element, then is() is used.

这很重要,因为 is()将在拥有它的浏览器中使用 matchesSelector ,并且 matchesSelector 将给出与 querySelectorAll 相同的评估。该评估与 Sizzle 不同。

This matters because is() will use matchesSelector in browsers that have it, and matchesSelector will give the same evaluation as querySelectorAll. That evaluation differs from Sizzle.

这篇关于为什么* [checked]表现得像:使用jQuery的过滤方法时检查过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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