diff --git a/docs/tab/index.md b/docs/tab/index.md index cf893eff..3f5d6a4d 100644 --- a/docs/tab/index.md +++ b/docs/tab/index.md @@ -110,7 +110,7 @@ tab 组件提供了三种 UI 风格,分别为: | var element = layui.element | 获得 `element` 模块。 | | [element.render(\'tab\', filter)](#element.render) | 渲染 tab 组件 | | [element.tabAdd(filter, options)](#element.tabAdd) | 添加 tab 选项 | -| [element.tabDelete(filter, layid)](#element.tabDelete) | 删除 tab 选项 | +| [element.tabDelete(filter, layid, force)](#element.tabDelete) | 删除 tab 选项 | | [element.tabChange(filter, layid, force)](#element.tabChange) | 切换 tab 选项 | | [element.tab(options)](#element.tab) | 绑定自定义 tab 元素 | @@ -197,10 +197,12 @@ layui.use(function(){
+ +diff --git a/src/modules/element.js b/src/modules/element.js index e8bbce92..f7863405 100644 --- a/src/modules/element.js +++ b/src/modules/element.js @@ -57,12 +57,21 @@ layui.define('jquery', function(exports){ return this; }; - // 外部 Tab 删除 - Element.prototype.tabDelete = function(filter, layid){ + /** + * 外部 Tab 删除 + * @param {string} filter - 标签主容器 lay-filter 值 + * @param {string} layid - 标签头 lay-id 值 + * @param {boolean} force - 是否强制删除 + * @returns {this} + */ + Element.prototype.tabDelete = function(filter, layid, force){ var tabElem = $('.layui-tab[lay-filter='+ filter +']'); var titElem = tabElem.children(TITLE); var liElem = titElem.find('>li[lay-id="'+ layid +'"]'); - call.tabDelete(null, liElem); + call.tabDelete.call(liElem[0], { + liElem: liElem, + force: force + }); return this; }; @@ -185,21 +194,26 @@ layui.define('jquery', function(exports){ } // Tab 删除 - ,tabDelete: function(e, othis){ - var li = othis || $(this).parent(); + ,tabDelete: function(obj){ + obj = obj || {}; + + var li = obj.liElem || $(this).parent(); var index = li.parent().children('li').index(li); var tabElem = li.closest('.layui-tab'); var item = tabElem.children('.layui-tab-content').children('.layui-tab-item'); var filter = tabElem.attr('lay-filter'); var hasId = li.attr('lay-id'); - var shouldClose = layui.event.call(li[0], MOD_NAME, 'tabBeforeDelete('+ filter +')', { - elem: tabElem, - index: index, - id: hasId - }); - if(shouldClose === false) return; - + // 若非强制删除,则根据 tabBeforeDelete 事件的返回结果决定是否删除 + if (!obj.force) { + var shouldClose = layui.event.call(li[0], MOD_NAME, 'tabBeforeDelete('+ filter +')', { + elem: tabElem, + index: index, + id: hasId + }); + if(shouldClose === false) return; + } + if(li.hasClass(THIS)){ if (li.next()[0] && li.next().is('li')){ call.tabClick.call(li.next()[0], { @@ -256,7 +270,11 @@ layui.define('jquery', function(exports){ var li = $(this); if(!li.find('.'+CLOSE)[0] && li.attr('lay-allowclose') !== 'false'){ var close = $(''); - close.on('click', call.tabDelete); + close.on('click', function(e) { + call.tabDelete.call(this, { + e: e + }); + }); li.append(close); } });