模糊事件似乎停止了click事件处理程序的工作?我有一个组合框,只有当文本字段有焦点时,选项才会出现. Select 选项链接应该会导致事件发生.

我这里有一个小提琴的例子:http://jsfiddle.net/uXq5p/6/

To reproduce:

  1. Select 文本框
  2. Links appear
  3. Click a link
  4. 甚至出现模糊,链接消失
  5. 没有别的事情发生.

预期行为:

On step 5, after blur occurs, the click even should also then fire. How do I make that happen?

UPDATE:

After playing with this for a while, it seems that someone has gone to great lengths to prevent an already-occurred click event from being handled if a blur event makes the clicked element Un-clickable.

For example:

$('#ShippingGroupListWrapper').css('left','-20px');

works just fine, but

$('#ShippingGroupListWrapper').css('left','-2000px');

prevents the click event.

This appears to be a bug in Firefox, since making an element un-clickable should prevent future clicks, but not cancel ones that have already occurred when it could be clicked.

阻止click事件处理的其他事项:

$('#ShippingGroupListWrapper').css('z-index','-20');
$('#ShippingGroupListWrapper').css('display','none');
$('#ShippingGroupListWrapper').css('visibility','hidden');
$('#ShippingGroupListWrapper').css('opacity','.5');

我在这个网站上发现了一些其他问题,它们也有类似的问题.似乎有两种解决方案:

  1. Use a delay. This is bad because it creates a race condition between the hiding and the click event handler. Its also sloppy.

  2. Use the mousedown event. But this isn't a great solution either since click is the correct event for a link. The behavior of mousedown is counter-intuitive from a UX perspective, particularly since you can't cancel the click by moving the mouse off the element before releasing the button.

I can think of a few more.

3.Use mouseover and mouseout on the link to enable/disable the blur event for the field. This doesn't work with keyboard tabing since the mouse is not involved.

4.最佳解决方案如下所示:

$('#ShippingGroup').blur(function()
{
   if($(document.activeElement) == $('.ShippingGroupLinkList'))
      return; // The element that now has focus is a link, do nothing
   $('#ShippingGroupListWrapper').css('display','none'); // hide it.
}

不幸的是,$(document.activeElement)似乎总是返回body元素,而不是单击的元素.但如果有可靠的方法来了解.哪个元素现在有焦点或两个焦点,哪个元素在模糊处理程序中导致模糊(而不是哪个元素正在模糊).此外,在模糊之前是否还有其他触发事件(mousedown除外)?

推荐答案

click事件在blur之后触发,因此链接被隐藏.用mousedown代替click,它会起作用.

$('.ShippingGroupLinkList').live("mousedown", function(e) {
    alert('You wont see me if your cursor was in the text box');
});

另一种 Select 是在隐藏blur事件的链接之前进行一些延迟. Select 哪种方法取决于你.

Demo

Jquery相关问答推荐

如果文本框内容在 X 秒内没有更改,则进行 post 调用

JQuery AJAX 成功事件未触发

在 jQuery 中构建 html 元素的最清晰方法

将 HTML5 Canvas 转换为要上传的文件?

使用 jQuery Validate 确认密码

组合数组时无法读取未定义的属性推送

jQuery/JavaScript 碰撞检测

从 iframe 访问父窗口的元素

删除所有子元素的 CLASS

如何在 HTML选项标签上显示工具提示?

jQuery Select 器中前导冒号的目的是什么?

覆盖 twitter bootstrap Textbox Glow and Shadows

jQuery从字符串中删除'-'字符

如何使用 jQuery 格式化电话号码

当 iframe 在 jQuery 中完成加载时,如何触发事件?

jQuery.active 函数

$.ajax 的成功和 .done() 方法有什么区别

添加到数组 jQuery

fancybox2 / fancybox 导致页面跳转到顶部

Bootstrap:在 Modal 中打开另一个 Modal