简体   繁体   English

如何在没有单击链接的情况下使用jquery模糊调用函数?

[英]How to call a function with jquery blur UNLESS clicking on a link?

I have a small jquery script: 我有一个小的jQuery脚本:

$('.field').blur(function() {
    $(this).next().children().hide();
});

The children that is hidden contains some links. 隐藏的子项包含一些链接。 This makes it impossible to click the links (because they get hidden). 这样就无法单击链接(因为它们被隐藏了)。 What is an appropriate solution to this? 有什么合适的解决方案?

this is as close as I have gotten: 这与我所获得的接近:

$('.field').blur(function() {
       $('*').not('.adress').click(function(e) {
            foo = $(this).data('events').click;
            if(foo.length <= 1) {
//             $(this).next('.spacer').children().removeClass("visible");
            }
            $(this).unbind(e);
        });
});

The uncommented line is suppose to refer to the field that is blurred, but it doesn't seem to work. 假定未注释的行是指模糊的字段,但它似乎不起作用。 Any suggestions? 有什么建议么?

You can give it a slight delay, like this: 您可以稍微延迟一下,如下所示:

$('.field').blur(function() {
  var kids = $(this).next().children();
  setTimeout(function() { kids.hide(); }, 10);
});

This gives you time to click before those child links go away. 这使您有时间单击,然后这些子链接消失。

This is how I ended up doing it: 这就是我最终这样做的方式:

var curFocus;
    $(document).delegate('*','mousedown', function(){
        if ((this != curFocus) && // don't bother if this was the previous active element                
            ($(curFocus).is('.field')) && // if it was a .field that was blurred
            !($(this).is('.adress'))
        ) {
            $('.' + $(curFocus).attr("id")).removeClass("visible"); // take action based on the blurred element
        }

        curFocus = this; // log the newly focussed element for the next event
    });

I believe you can use .not('a') in this situation: 相信您可以在这种情况下使用.not('a'):

$('.field').not('a').blur(function() {
    $(this).next().children().hide();
});

This isn't tested, so I am not sure if this will work or not. 这未经测试,所以我不确定这是否行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM