我有一個jQuery UI對話框,當(dāng)單擊特定元素時會顯示該對話框。如果單擊不是在那些觸發(fā)元素或?qū)υ捒虮旧砩系娜魏蔚胤桨l(fā)生的,我想關(guān)閉對話框。這是打開對話框的代碼:$(document).ready(function() { var $field_hint = $('<div></div>') .dialog({ autoOpen: false, minHeight: 50, resizable: false, width: 375 }); $('.hint').click(function() { var $hint = $(this); $field_hint.html($hint.html()); $field_hint.dialog('option', 'position', [162, $hint.offset().top + 25]); $field_hint.dialog('option', 'title', $hint.siblings('label').html()); $field_hint.dialog('open'); }); /*$(document).click(function() { $field_hint.dialog('close'); });*/});如果我取消注釋的最后一部分,該對話框?qū)⒂肋h不會打開。我認(rèn)為這是因為打開對話框的同一點擊再次將其關(guān)閉。最終工作代碼注意:這是使用jQuery外部事件插件$(document).ready(function() { // dialog element to .hint var $field_hint = $('<div></div>') .dialog({ autoOpen: false, minHeight: 0, resizable: false, width: 376 }) .bind('clickoutside', function(e) { $target = $(e.target); if (!$target.filter('.hint').length && !$target.filter('.hintclickicon').length) { $field_hint.dialog('close'); } }); // attach dialog element to .hint elements $('.hint').click(function() { var $hint = $(this); $field_hint.html('<div style="max-height: 300px;">' + $hint.html() + '</div>'); $field_hint.dialog('option', 'position', [$hint.offset().left - 384, $hint.offset().top + 24 - $(document).scrollTop()]); $field_hint.dialog('option', 'title', $hint.siblings('label').html()); $field_hint.dialog('open'); }); // trigger .hint dialog with an anchor tag referencing the form element $('.hintclickicon').click(function(e) { e.preventDefault(); $($(this).get(0).hash + ' .hint').trigger('click'); });});
jQuery UI-在外部單擊時關(guān)閉對話框
喵喵時光機
2019-09-21 11:16:54