如何点击Fancytree展开器时不触发节点activate事件
解决Fancytree点击展开器触发activate事件跳转的问题
针对你提出的两个问题,给出具体解决方案:
1. 在activate事件中识别并忽略展开器触发的情况
可以通过判断触发事件的目标元素是否为展开器,直接跳过跳转逻辑:
修改activate事件处理函数,添加对展开器的判断:
activate: function(event, data){ // 检查是否点击了展开器,是的话直接返回 const isExpander = $(event.target).closest('.fancytree-expander').length > 0; if (isExpander) return; const node = data.node; //prevent looping (hns is false or a page id) if(node.key === JSINFO.id || node.data.hns === JSINFO.id) { //node is equal to current page return; } if(node.data.url === false) { return false; } if(node.data.url){ window.location.href = node.data.url; // console.log("reload"); } },
2. 通过click事件阻止展开器触发激活,同时保留展开/折叠功能
之前使用return false会同时阻止默认的展开行为,改用event.stopPropagation()仅阻止事件冒泡,避免触发activate事件即可:
启用并修改click事件处理函数:
click: function(event, data) { // 仅当点击展开器时,阻止事件冒泡,不触发activate if(data.targetType === 'expander') { event.stopPropagation(); } },
修改后的完整配置代码示例
$tree.fancytree({ extensions: [], clickFolderMode: 3, // expand with single click instead of dblclick autoActivate: false, // we use scheduleAction(). Otherwise, looping in combination with clicking activeVisible: false, focus: function(event, data) { // Auto-activate focused node after 1 second (practical for use with keyboard) if(data.node.key){ data.node.scheduleAction("activate", 1000); } }, blur: function(event, data) { data.node.scheduleAction("cancel"); }, click: function(event, data) { // 仅阻止展开器触发激活,不影响展开/折叠 if(data.targetType === 'expander') { event.stopPropagation(); } }, activate: function(event, data){ const node = data.node; //prevent looping (hns is false or a page id) if(node.key === JSINFO.id || node.data.hns === JSINFO.id) { //node is equal to current page return; } if(node.data.url === false) { return false; } if(node.data.url){ window.location.href = node.data.url; // console.log("reload"); } }, init: function(event, data) { data.tree.reactivate(); }, enhanceTitle: function(event, data) { let node = data.node; if(node.data.url === false) { return; } data.$title.html("<a href='" + node.data.url + "'>" + node.title + "</a>"); }, source: { url: DOKU_BASE + 'lib/exe/ajax.php', data: { ... } }, lazyLoad: function(event, data) { const node = data.node; // Issue an Ajax request to load child nodes data.result = { url: DOKU_BASE + 'lib/exe/ajax.php', data: { .. } } } });
内容的提问来源于stack exchange,提问作者Klap-in
相关产品推荐
相关产品推荐

