简体   繁体   English

子点击事件触发父鼠标悬停事件

[英]child click event firing parent mouseover event

I have tree structure in which mouse over on node name displays (UL) list. 我具有树形结构,其中将鼠标悬停在节点名称上会显示(UL)列表。 Each item in the list has a click event attached to it. 列表中的每个项目都附有click事件。 The issue Im facing is when I click on any child item in list, it fires the mouseover event attached to parent span. 我面临的问题是,当我单击列表中的任何子项时,它会触发附加到父跨度的mouseover事件。 Can you guys please help how to solve this issue? 你们能帮忙解决这个问题吗?

<span id="treeNodeText">
<ul><li id="firstItem">First Item</li></ul>
</span>

My code is like this: I have conman event attach method: 我的代码是这样的:我有conman事件附加方法:

attachEvents(domId,eventType,callBackFunction,otherParams)

In attachEvent function I attach events to dom ids and assign appropriate call back functions 在attachEvent函数中,我将事件附加到dom ID并分配适当的回调函数

The mouseover event is fired before you click. 单击会触发mouseover事件。 So, apart with a delay, you can't prevent its handling. 因此,除了延迟之外,您无法阻止对其进行处理。

Here's one way to deal with that : 这是解决该问题的一种方法:

var timer;
document.getElementById("treeNodeText").addEventListener('mouseover', function(){
    clearTimeout(timer);
    timer = setTimeout(function(){
       // handle mouseover
    }, 400); // tune that delay (depending on the sizes of elements, for example)
});
document.getElementById("firstItem").addEventListener('click', function(){
    clearTimeout(timer); // prevents the mouseover event from being handled
    // handle click
};

In JavaScript, events bubble up the DOM. 在JavaScript中,事件使DOM冒泡。 Please read more about it: event order and propagation or preventDefault/stopPropagation . 请详细了解它: 事件顺序和传播preventDefault / stopPropagation

In short, you can pervent event bubbling by 简而言之,您可以防止事件冒泡

function callBackFunction(event){
  event.stopPropagation()
}

or 要么

function callBackFunction(event){
  return false
}

return false also has the effect of preventing the default behavior, so it's technically equivalent to: return false还具有防止默认行为的作用,因此从技术上讲,它等效于:

function callBackFunction(event){
  event.stopPropagation()
  event.preventDefault()
}
function myfunction(e){
  e.stopPropagation()
 e.preventDefault()
}

this will Help 这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM