jQuery UI日期选择器能否与点击事件的目标匹配? [英] Can a jQuery UI datepicker be matched to a click event's target?

查看:96
本文介绍了jQuery UI日期选择器能否与点击事件的目标匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个简单的jQuery(1.4.2)UI(1.8)小部件,该小部件嵌入了jQuery UI datepicker.我想检测用户何时在我的窗口小部件外部单击,在这种情况下,它应该自动隐藏.我的代码大致如下:

I've written a simple jQuery (1.4.2) UI (1.8) widget that embeds a jQuery UI datepicker. I'd like to detect when a user clicks outside my widget, in which case it should automatically hide itself. My code looks roughly like this:

var self = this;
$(document).bind("click.mywidget": function(event) {
    var target = event.target;
    if (!self.container.has(target).length && target != self.container.get(0)) {
        self.hide();
    }
});

单击日期选择器中的日期或月份上一个/下一个按钮时出现问题.由于某些原因,即使Firebug将这些元素显示为我的容器的后代,但has()检查也对它们失败.

The problem comes when clicking on a date or month prev/next button in the datepicker. For some reason, even though Firebug shows those elements as being descendants of my container, the has() check fails for them.

发生了什么,我该如何解决?

What is happening, and how might I fix this?

推荐答案

在对日期选择器源进行了一些挖掘之后,我意识到了发生了什么.显然,每当您单击日期或在几个月中循环时,它就会清空整个div并重新创建它.

After digging through the datepicker source a bit, I realized what was going on. Apparently whenever you click on a date, or cycle through months, it empties the entire div and re-creates it.

由于选择器在DOM中位于更下方,因此它首先处理click事件.到文档处理程序被调用时,目标元素不再在DOM中,因此不再是容器的后代.

Since the picker is further down in the DOM, it handles the click event first. By the time the document handler gets called, the target element is no longer in the DOM, and is therefore no longer a descendant of the container.

我匆忙的破解方法是检查目标的更直接的父母,看看他们中的任何一个是日期选择器表还是表头.到目前为止,由于此小部件是我唯一使用的datepicker,因此我可以假定对此类元素的单击不应算作容器外部的单击.

My hasty hack was to check the target's more direct parents, to see whether any of them is a datepicker table or header. Since this widget is thus far my only use of datepicker, I can assume that a click on such an element should not count as a click outside the container.

一旦提出真正的解决方案,我将更新此答案.

I'll update this answer once I come up with a real solution.

这篇关于jQuery UI日期选择器能否与点击事件的目标匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆