From cf2531947c5b49ff84f7c61f7526eb040c5213ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Fri, 26 Aug 2022 23:26:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20dropdown=20=E7=9A=84=20cli?= =?UTF-8?q?ckScope=20=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/dropdown.html | 5 ++++- src/modules/dropdown.js | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/dropdown.html b/examples/dropdown.html index 51bd13b2..18203d6b 100644 --- a/examples/dropdown.html +++ b/examples/dropdown.html @@ -155,7 +155,10 @@ layui.use('dropdown', function(){ ,id: 'demo1' - //菜单被点击的事件 + // 触发点击事件的元素范围 --- default: 仅子菜单触发点击事件(默认,可不填); all: 所有父子菜单均触发点击事件 + ,clickScope: 'all' + + // 菜单被点击的事件 ,click: function(obj){ console.log(obj); } diff --git a/src/modules/dropdown.js b/src/modules/dropdown.js index bfaf1a12..7587f0e1 100644 --- a/src/modules/dropdown.js +++ b/src/modules/dropdown.js @@ -261,13 +261,15 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){ //触发菜单列表事件 that.elemView.find('.layui-menu li').on('click', function(e){ - var othis = $(this) - ,data = othis.data('item') || {} - ,isChild = data.child && data.child.length > 0; + var othis = $(this); + var data = othis.data('item') || {}; + var isChild = data.child && data.child.length > 0; + var isClickAllScope = options.clickScope === 'all'; // 是否所有父子菜单均触发点击事件 - if(!isChild && data.type !== '-'){ - that.remove(); + if((!isChild || isClickAllScope) && data.type !== '-'){ + isChild || that.remove(); typeof options.click === 'function' && options.click(data, othis); + layui.stope(e); } });