Chrome 在开放(集中)“选择”中的行为非常奇怪元素

发布于 2024-12-01 15:49:33 字数 1380 浏览 2 评论 0原文

这是一个

<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);
});

这是这个问题的jsfiddle。

该处理程序设置一个间隔计时器,每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-12-08 15:49:33

将代码更改为:

function foo() {
    var s = $('select option:selected');
}

setInterval(foo, 200);

不确定它为什么这样做,但我的猜测是它与 jQuery 中伪选择器的工作方式有关。它们被实现为与选择器名称配对的函数(在本例中为“selected”)。因此,它们针对上下文中的每个可能元素运行(而不仅仅是那些可能可能被选择的元素)。

也许存在某种奇怪的幽灵元素,“选定的”伪选择器在不应该执行的时候被执行。这里的解决方案是在执行伪选择器之前将上下文限制为选项。总是一件好事。

Change the code to:

function foo() {
    var s = $('select option:selected');
}

setInterval(foo, 200);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文