Chrome 在开放(集中)“选择”中的行为非常奇怪元素
这是一个
<select>
<option>Hello</option>
<option>Banana</option>
<option>Balloon</option>
<option>Something</option>
<option>Potato</option>
<option>Cleveland</option>
</select>
这是一些 JavaScript(jQuery“就绪”处理程序):
$(function() {
function foo() {
var s = $('select').find(':selected');
}
setInterval(foo, 200);
});
该处理程序设置一个间隔计时器,每 200 毫秒查找 元素,然后尝试选择一个条目,选择突出显示会不断跳回到第一个选定的元素。我可以单击显示的任何
元素,当然,它可以工作,然后它下次会执行相同的操作。
现在,如果我更改计时器处理程序,以便它不使用 jQuery 来查找当前选定的 元素,如下所示:
$(function() {
function foo() {
var select = $('select')[0];
var s = $(select.options[select.selectedIndex]);
}
setInterval(foo, 200);
});
然后我不再看到的效果。
火狐浏览器不这样做。我还没有尝试过 Safari。
就我个人而言,我认为这里有些事情出了问题。是 Chrome 吗? jQuery?
编辑 - 还有一个细节 - 我在 Linux 上运行 Chrome。我稍后会尝试 Windows。 (在 Windows 中进行相同的编辑。)
Here's a <select>
element:
<select>
<option>Hello</option>
<option>Banana</option>
<option>Balloon</option>
<option>Something</option>
<option>Potato</option>
<option>Cleveland</option>
</select>
Here's a little bit of JavaScript (a jQuery "ready" handler):
$(function() {
function foo() {
var s = $('select').find(':selected');
}
setInterval(foo, 200);
});
Here is the jsfiddle for this question..
The handler sets up an interval timer which, every 200 milliseconds, finds the currently-selected <option>
of the <select>
, and does nothing at all with it. When I run the fiddle in Chrome (13.0.782.112), and I click on the <select>
element, and then try to select an entry, the selection highlight keeps jumping back to the first selected element. I can click on any of the <option>
elements shown and that works, of course, and then it does the same thing the next time.
Now, if I change the timer handler so that it doesn't use jQuery to find the currently-selected <option>
element, as follows:
$(function() {
function foo() {
var select = $('select')[0];
var s = $(select.options[select.selectedIndex]);
}
setInterval(foo, 200);
});
then I no longer see the effect.
Firefox does not do this. I haven't tried Safari yet.
Personally I think that something's doing something wrong here. Is it Chrome? jQuery?
edit — one more detail - I'm running Chrome on Linux. I'll try Windows in a sec. (edit same in Windows.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将代码更改为:
不确定它为什么这样做,但我的猜测是它与 jQuery 中伪选择器的工作方式有关。它们被实现为与选择器名称配对的函数(在本例中为“selected”)。因此,它们针对上下文中的每个可能元素运行(而不仅仅是那些可能可能被选择的元素)。
也许存在某种奇怪的幽灵元素,“选定的”伪选择器在不应该执行的时候被执行。这里的解决方案是在执行伪选择器之前将上下文限制为选项。总是一件好事。
Change the code to:
Not sure why exactly it does this, but my guess would be that it's related to the way pseudoselectors work in jQuery. They're implemented as functions which are paired with the name of the selector (In this case "selected"). Consequently they are run against every possible element in the context (not just those which could potentially be selected).
Maybe there some sort of weird ghost element against which the "selected" pseudoselector is being executed when it shouldn't. The solution here is that I restrict the context to options before doing the pseudoselector. Always a good thing to do.