gpt4 book ai didi

jquery - find ('option[selected]' ) 和 find ('option' ).filter ('[selected]' ) 之间的区别

转载 作者:行者123 更新时间:2023-12-03 22:27:41 25 4
gpt4 key购买 nike

场景:

我有 2 个 jQuery 表达式:

/* A */ $('select').find('option[selected]');
/* B */ $('select').find('option').filter('[selected]');

这意味着(为简单起见,我们假设文档中只有一个 select):

  • A:获取 select ,然后查找所有option具有名为 selected 的属性的后代.
  • B:获取 select ,然后查找所有option后代,然后按具有名为 selected 的属性的人进行过滤.

预期行为:

AB 应该给出相同的结果。

实际行为:

用户更改下拉列表中的选择后,

  • A 返回所选的默认 option .
  • B 返回所选的 option .

问题:

那么它们为什么不同呢?我对CSS选择器的理解是错误的吗?

现场演示:

现场演示是 here here .

源代码:

HTML:

<select>
<option value='p'>p</option>
<option value='q' selected>q</option>
<option value='r'>r</option>
<option value='s'>s</option>
</select>


<input type='button' value='click me!'/> <br/>
ResultA : <span id='ResultA'>
here
</span> <br/>
ResultB : <span id='ResultB'>
here
</span> <br/>

Javascript:

function SetResult(ResultObj, ElementObj) {
ResultObj.text("length=" + ElementObj.length + " " + "val()=" + ElementObj.val());
}

$(function() {
$('input[type=button]').click(function() {
var SelectObj = $('select');
SetResult($("#ResultA"), SelectObj.find('option[selected]'));
SetResult($("#ResultB"), SelectObj.find('option').filter('[selected]'));
});
});

测试结果:

+---------------------------+--------------+---------------------+---------+-----+
| Browser | Environment | jQuery | A | B |
+---------------------------+--------------+---------------------+---------+-----+
| Chrome 22.0.1229.94m | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Chrome 23.0.1271.64 m | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 15.0.1 | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 16.0.2 | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| IE 6 | WinXP | 1.8.2, 1.7.2, 1.6.4 | *new* | new |
| IE 9 | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.02 | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.10 | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Safari 5.1.7 (7534.57.2) | Win7 | 1.8.2, 1.7.2, 1.6.4 | default | new |
+---------------------------+--------------+---------------------+---------+-----+
| Chrome 22.0.1229.94 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Chrome 23.0.1271.64 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 13.0 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 14.0.1 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 16.0.2 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.01 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.10 | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Safari 6.0.1 (7536.26.14) | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
+---------------------------+--------------+---------------------+---------+-----+
| Chrome 21.0.1180.82 | iOS 4.3.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 7.0.5 | iOS 4.3.5 | 1.8.2 | default | new |
| Safari | iOS 4.3.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
+---------------------------+--------------+---------------------+---------+-----+
  • default 表示返回所选的默认 option .
  • new 表示返回所选的 option .

如您所见,除 IE6 之外的所有浏览器都会给出不同的结果。

最佳答案

Sizzle 引擎检查元素的选定属性(包含当前值),而不是属性 em> 其中包含原始(默认)值。

参见https://github.com/jquery/sizzle/blob/master/sizzle.js#L788

我还没有弄清楚为什么你的第二个选择器显然调用了 Sizzle,但第一个选择器似乎没有。

无论如何,您应该检查的是属性而不是属性,因此您应该使用 :selected 伪选择器,而不是 [selected]

关于jquery - find ('option[selected]' ) 和 find ('option' ).filter ('[selected]' ) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13137737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com