[英]How to get target element of click event in Ember?
I'm trying to add a modal to Ember app and got this template: 我正在尝试向Ember应用程序添加模式,并获得以下模板:
<div class="modal-overlay" {{action "close"}}>
<div class="modal-body">
{{yield}}
</div>
</div>
The problem is that if I click inside body element, the click event bubbles and triggers close
action on the overlay. 问题是,如果我在body元素内单击,单击事件会冒泡并触发叠加层上的close
动作。 If I try to prevent bubbling by adding an action that doesn't do anything to the body element: 如果我尝试通过对body元素添加任何不做任何动作来防止冒泡:
<div class="modal-overlay" {{action "close"}}>
<div class="modal-body" {{action "nop" bubbles=false>
{{yield}}
</div>
</div>
I can't click any links in the body anymore... 我无法再点击体内的任何链接...
What's the best way to solve it? 解决问题的最佳方法是什么? If I could access click event in the close
action, I could check if the element clicked was actually the overlay. 如果我可以在close
操作中访问click事件,则可以检查单击的元素是否实际上是叠加层。
The only way i found to access the target element was using jquery 我发现访问目标元素的唯一方法是使用jquery
didInsertElement(){
this.$()
.on('click', '.gn-icon-menu', (event) => {
event.preventDefault();
event holds the target element. 事件保存目标元素。 So notice im not using ember actions here. 因此请注意,我此处未使用余烬动作。
I believe most modals I've seen have solved this by structuring things differently. 我相信我见过的大多数模态都通过不同地构造事物来解决了这个问题。 Checkout lightbox for example. 例如,检出灯箱 。 It would look kinda like this for you. 对于您来说,看起来像这样。
<div class="modal-overlay" {{action "close"}}></div>
<div class="modal-wrap">
<div class="modal-body>
{{yield}}
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.