typescript div 和 childs 上的 Angular 2 鼠标悬停事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39767987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:53:12  来源:igfitidea点击:

Angular 2 mouseover event on div and childs

javascriptangulartypescript

提问by Gerald Hughes

I have a span child in a div.

我在一个 div 中有一个跨度孩子。

On my div I have a mouseoverevent, when I hover on the span, my event triggers.

在我的 div 上,我有一个mouseover事件,当我将鼠标悬停在 上时span,我的事件就会触发。

Simple code:

简单代码:

<div (mouseover)="showOverlay($event, FooBar)" (mouseleave)="showOverlay($event, FooBar)">
    <span>{{ someDataHere }}</span>
</div>


 public showOverlay($event, op, element): void {
    op.toggle($event, element);
    $event.preventDefault();
}

What I want is to keep showing my overlay when on child, how do I achieve this?

我想要的是在孩子身上继续显示我的叠加层,我该如何实现?

回答by Günter Z?chbauer

mouseenterand mouseleavecover this use case better because entering a child doesn't mouseleaveto fire, only leaving the outside border of the actual element makes it to fire.

mouseentermouseleave更好地覆盖这个用例,因为进入一个孩子不会mouseleave触发,只有离开实际元素的外边界才能触发。

See also What is the difference between the mouseover and mouseenter events?

另请参见mouseover 和 mouseenter 事件有什么区别?