diff --git a/src/modules/form.js b/src/modules/form.js index c866c06f..34cbd936 100644 --- a/src/modules/form.js +++ b/src/modules/form.js @@ -457,7 +457,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ hideDown(); initValue && input.val(initValue); }, - {ignore: title} + {ignore: title, detectIframe: true} ); }; diff --git a/src/modules/lay.js b/src/modules/lay.js index f0c369d9..ed3e1977 100644 --- a/src/modules/lay.js +++ b/src/modules/lay.js @@ -629,6 +629,7 @@ * @param {HTMLElement | Window} [options.scope=document] - 监听范围 * @param {Array} [options.ignore] - 忽略监听的元素或选择器字符串 * @param {boolean} [options.capture=true] - 对内部事件侦听器使用捕获阶段 + * @param {boolean} [options.detectIframe] - 是否检测 iframe * @returns {() => void} - 返回一个停止事件监听的函数 */ lay.onClickOutside = function(target, handler, options){ @@ -637,6 +638,7 @@ var scopeTarget = options.scope || document; var ignore = options.ignore || []; var useCapture = 'capture' in options ? options.capture : true; + var detectIframe = options.detectIframe; var listener = function(event){ var el = target; @@ -693,21 +695,36 @@ return [eventTarget].concat(getParents(eventTarget)); } - if(document.addEventListener){ - scopeTarget.addEventListener(eventType, listener, useCapture); - }else{ - scopeTarget.attachEvent('on' + eventType, listener); - } + function bindEventListener(elem, eventName, handler, opts){ + elem.addEventListener + ? elem.addEventListener(eventName, handler, opts) + : elem.attachEvent('on' + eventName, handler); - var stop = function(){ - if(document.removeEventListener){ - scopeTarget.removeEventListener(eventType, listener, useCapture); - }else{ - scopeTarget.detachEvent('on' + eventType, listener); + return function(){ + elem.removeEventListener + ? elem.removeEventListener(eventName, handler, opts) + : elem.detachEvent('on' + eventName, handler); } } - return stop; + var cleanup = [ + bindEventListener(scopeTarget, eventType, listener, lay.passiveSupported ? { passive: true, capture: useCapture } : useCapture), + detectIframe && bindEventListener(window, 'blur', function(event){ + setTimeout(function(){ + if(document.activeElement && document.activeElement.tagName === 'IFRAME' + && target.contains && !target.contains(document.activeElement) + ){ + handler(event); + } + }, 0); + }) + ] + + return function(){ + for(var i=0; i < cleanup.length; i++){ + cleanup[i] && cleanup[i](); + } + } };