jquery文档点击功能仅适用于Firefox

发布于 2024-12-28 07:27:57 字数 532 浏览 0 评论 0原文

我正在使用 dropkick.js 来设置选择菜单的样式。当您单击菜单之外的任何位置时,我试图关闭菜单。但removeClass()(和document.click())仅适用于firefox。在 webkit 中,单击外部或文档中的其他任何位置都不会关闭菜单。

删除“.dk_open”和“.dk_focus”类会将元素返回到 display:none,从而隐藏菜单。

有谁看到任何可能导致这种不一致的情况吗?谢谢!

        $('.wpcf7 select').dropkick();

        var $dkopen = $('.dk_open');

        $(document).click(function(){
            $dkopen.removeClass('dk_open dk_focus');
        });

        $dkopen.click(function(){
            e.stopPropagation();
        });

I'm using dropkick.js to style a select menu. I am trying to make the menu close when you click anywhere outside of the menu. But the removeClass() ( and the document.click() ) are only working on firefox. In webkit, the menu does not close by clicking outside or anywhere else in the document.

Removing the ".dk_open" and ".dk_focus" classes returns the element back to display:none, thereby hiding the menu.

Does anyone see anything that might be causing this inconsistency? Thanks!

        $('.wpcf7 select').dropkick();

        var $dkopen = $('.dk_open');

        $(document).click(function(){
            $dkopen.removeClass('dk_open dk_focus');
        });

        $dkopen.click(function(){
            e.stopPropagation();
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

你与昨日 2025-01-04 07:27:58

您需要为每个选择添加 tabindex。之后它就会起作用。
祝你好运!

You need to add tabindex to each select. After that it will work.
Good luck!

九八野马 2025-01-04 07:27:57

这可能需要进行一些清理,但它可以解决下拉列表未按预期关闭以匹配本机选择功能的问题。

$(document).click(function(){
    $('.dk_open').removeClass('dk_open');
});

$('.dk_open').live('click',function(e){
    e.stopPropagation();
});

$('.dk_container, .dk_toggle').live('click', function(e){
     $('.dk_open').removeClass('dk_open');
});

添加了最后一个事件处理程序,以在选择另一个下拉菜单时关闭所有打开的下拉菜单。

This could use some cleaning up but it worked to solve the issue of drop downs not closing as expected to match native select functionality.

$(document).click(function(){
    $('.dk_open').removeClass('dk_open');
});

$('.dk_open').live('click',function(e){
    e.stopPropagation();
});

$('.dk_container, .dk_toggle').live('click', function(e){
     $('.dk_open').removeClass('dk_open');
});

Added the last event handler to close any open drop downs when selecting another.

顾铮苏瑾 2025-01-04 07:27:57

我不知道这是否是您问题的原因,但您应该尝试在点击函数中添加对该事件的引用:

 $dkopen.click(function(e /*<-- here*/){
     e.stopPropagation();
 });

I don't know if this is the cause of your problem, but you should try to add a reference to the event in your click function:

 $dkopen.click(function(e /*<-- here*/){
     e.stopPropagation();
 });
指尖上的星空 2025-01-04 07:27:57

刚刚面临同样的问题。

这是讨论此问题的相关链接: https://github.com/JamieLottering/DropKick/issues/ 45

您的解决方案(Corey Aubuchon)确实“有效”...因为:

当您单击下拉列表的“外部”时,它确实会关闭...但是...

现在,当您单击在下拉菜单“内部”(单击:“dk_container”或“dk_toggle”)-下拉菜单不会关闭...

(而且,我不是在谈论在这里选择“选项”,我在说什么关于单击与首先单击查看选项相同的“向下”箭头)

我不是 JS 开发人员,我只是将此处找到的解决方案与引用链接中的解决方案结合起来,形成一些没有给出的内容不管怎样,它确实有效。

无论如何,试试这个...这个适用于 Chrome、IE、+ FF(在任何情况下,您都可以单击内部或外部)它会起作用:)

    $(document).click(function(){$('.dk_open').removeClass('dk_open');});
    $('dk_open').on('click',function(e){e.stopPropagation();});
    $('.dk_container, .dk_toggle').on('click',function(e){var$dk=$(this).parents('.dk_container').first();{$('.dk_open').removeClass('dk_open');$dk.toggleClass('dk_open');}return false;});

..或者...如果您愿意...这是非缩小版本:

    $(document).click(function(){
        $('.dk_open').removeClass('dk_open');
    });
    $('.dk_open').on('click',function (e) {
        e.stopPropagation();
    });
    $('.dk_container, .dk_toggle').on('click',function (e) {

        var $dk  = $(this).parents('.dk_container').first();

        if ( $.browser.webkit ){
            $('.dk_open').removeClass('dk_open');
                $dk.toggleClass('dk_open');
        }
        return false;
    });

Just faced the same issue.

Here is a relevant link talking about this: https://github.com/JamieLottering/DropKick/issues/45

Your solution (Corey Aubuchon) does 'work'... in that:

When you click 'Outside' the dropdown, it DOES close...However...

Now, when you click 'Inside' the dropdown (which would be clicking on: "dk_container' or 'dk_toggle') — the dropdown DOES NOT close...

(And, I'm not talking about selecting an 'option' here, what I'm talking about clicking on the same 'down' arrow you click to see the options in the first place)

I'm not a JS developer, I merely combined the solution found here, with the solution from the referenced link, into something that did not give me errors. Somehow it worked though.

Anyway, Try this instead...this works in Chrome, IE, + FF (you can click inside or outside, in every case it will work :)

    $(document).click(function(){$('.dk_open').removeClass('dk_open');});
    $('dk_open').on('click',function(e){e.stopPropagation();});
    $('.dk_container, .dk_toggle').on('click',function(e){var$dk=$(this).parents('.dk_container').first();{$('.dk_open').removeClass('dk_open');$dk.toggleClass('dk_open');}return false;});

..or... if you prefer... This is the NOT minified version:

    $(document).click(function(){
        $('.dk_open').removeClass('dk_open');
    });
    $('.dk_open').on('click',function (e) {
        e.stopPropagation();
    });
    $('.dk_container, .dk_toggle').on('click',function (e) {

        var $dk  = $(this).parents('.dk_container').first();

        if ( $.browser.webkit ){
            $('.dk_open').removeClass('dk_open');
                $dk.toggleClass('dk_open');
        }
        return false;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文