问题描述
我发现Angular2有一个奇怪的行为:
I found a strange behaviour with Angular2:
(click)
并未对此触发:
<div (click)="test()">test</div>
但是它在这里起作用:
<div style="position: relative;" (click)="test()">test</div>
有人可以解释这种行为吗?为什么需要设置位置样式才能触发(点击)?
Can anyone explain this behaviour? Why there is a need to set position style in order for (click) to fire?
我想念什么吗?
推荐答案
您的代码段看起来很好!
Your code snippet looks all good!
问题出在您的CSS样式中.您的<div>
可能继承了不同的position
值,或者只是-紧随另一个元素,该元素阻止了您的<div>
(不允许单击)..
The issue is in your CSS styles. Your <div>
probably either inherits a different position
value or simply - goes behind another element which element blocks your <div>
(does not allow it to be clicked).
通过将位置更改为relative
可以正常工作,这很可能是因为该位置启用了z-index
并将您的<div>
移至其他阻止它的元素上.
By changing the position to relative
it works, most probably because this position enables z-index
and moves your <div>
on top to the other element that's blocking it.
这应该足以让您弄清楚.但是,如果您想获得更详细的答案,请也分享您的CSS.
This should be enough for you to figure it out. But if you want more detailed answer - please share your CSS too.
这篇关于为什么Angular2(点击)事件没有在< div>上触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!