当您在对话框外部单击时,我想关闭对话框,但我不确定您如何在 jquery/plain javascript 中进行测试。
有些人建议使用 blur 事件,但这似乎不受 jquery 对话框的支持。
编辑我也有这个问题,但无法处理当前提供的任何答案,因为我无法使我的对话框成为模态。
我需要这个,以便我可以仅在对话框位于最顶部时注册键处理程序,并在另一个对话框置于顶部时立即取消注册它们。
有没有人有一个解决方案 - 理想情况下,每次其他对话框到达顶部时都会引发一个事件?
当您在对话框外部单击时,我想关闭对话框,但我不确定您如何在 jquery/plain javascript 中进行测试。
有些人建议使用 blur 事件,但这似乎不受 jquery 对话框的支持。
编辑我也有这个问题,但无法处理当前提供的任何答案,因为我无法使我的对话框成为模态。
我需要这个,以便我可以仅在对话框位于最顶部时注册键处理程序,并在另一个对话框置于顶部时立即取消注册它们。
有没有人有一个解决方案 - 理想情况下,每次其他对话框到达顶部时都会引发一个事件?
http://jsfiddle.net/marcosfromero/x4GXy/
// Bind the click event to window
$(window).click(function(event) {
if (($(event.target).closest('.ui-dialog')).length>0) {
// if clicked on a dialog, do nothing
return false;
} else {
// if clicked outside the dialog, close it
// jQuery-UI dialog adds ui-dialog-content class to the dialog element.
// with the following selector, we are sure that any visible dialog is closed.
$('.ui-dialog-content:visible').dialog('close');
}
})
你能把你的对话框变成模态的吗?如果是这样,那么您可以(可能)通过模态叠加层上的事件来实现您所追求的......
完全hacky,未经测试的想法,但它可能只是工作......
模态对话框创建称为 click.dialog-overlay 等的事件......当鼠标在对话框外单击时,在模态覆盖层上会触发这些事件。挂钩这些事件并关闭对话框可能只是做你想做的事情......
blur 事件并不是您想要的。模糊事件发生在单个元素上。您正在寻找的是当用户单击“外部”特定元素组时 - 某个父节点下方的所有内容。**没有事件,因此您必须使用您有权访问的事件来模拟它.
$('.dialogSelector').dialog({
open: function(e) { // on the open event
// find the dialog element
var dialogEl = $(this).parents('.ui-dialog')[0];
$(document).click(function (e) { // when anywhere in the doc is clicked
var clickedOutside = true; // start searching assuming we clicked outside
$(e.target).parents().andSelf().each(function () { // search parents and self
// if the original dialog selector is the click's target or a parent of the target
// we have not clicked outside the box
if (this == dialogEl) {
clickedOutside = false; // found
return false; // stop searching
}
});
if (clickedOutside) {
$('a.ui-dialog-titlebar-close').click(); // close the dialog
// unbind this listener, we're done with it
$(document).unbind('click',arguments.callee);
}
});
}
});
**更准确地说,您正在寻找一个事件,当用户在特定可见元素组之外单击时。对于用户来说,绝对定位的 div 可能看起来在一组元素的“外部”,而实际上它是其中一个元素的子元素。这不太适用,但它应该适用于您的目的。
希望有帮助。:)
类似于@marcosfromero(但性能更高)的解决方案是用于$.contains
测试一个元素是否存在于另一个元素中。$.contains
如果存在,则利用本地document.documentElement.compareDocumentPosition
方法,这意味着您不必event.target
转换为 jQuery 对象,不需要查询 DOM .ui-dialog
,并且底层逻辑甚至不需要遍历 DOM(在现代浏览器中) )。
$(document).click(function(event) {
if( !$.contains( dialog.dialog('widget')[0], event.target ) ){
$(':ui-dialog').dialog('close');
}
});
如果目标元素不存在由小部件创建的对话框标记(通过调用对话框的widget
方法获得),则关闭对话框。
演示:http: //jsfiddle.net/ehynds/auKAu/
看看jquery tools overlay这可能会帮助你做模态窗口。它在过去帮助了我。
要检查点击是否在模态窗口之外,您可以执行以下操作:
echo '<div class="mybody">Body of the webpage';
echo '<div class="myoverlay">Body of overlay</div></div>';
jQuery:
$(function() {
$('body').click(function(e) {
var inOverlay = false;
$(e.target).parents().each(function(idx,parent) {
if ('mybody' == parent.className) {
inOverlay=true;
}
});
if (!inOverlay) {
alert('outside');
}
});
});
然后您可以在模态窗口中添加键盘检查:
$(".myoverlay").keydown(function(e) {
// your specific keyboard handler
});
我认为这可能会做到:
$("element").blur(function(){
/* Callback goes here */
$("element").hide();
});
使用 .blur() 函数......它很棒:D
使用 CSS {position:fixed;height:100%;width:100%;background:transparent;z-index:100} 创建一个透明叠加层并使用$('.overlay').click(function() {$('ui-dialog').remove();}
. 当然,您需要在创建<div class="overlay"></div>
对话框的同时创建。并且对话框将需要更高的 z-index!