简体   繁体   English

如何在Ember中获取click事件的目标元素?

[英]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.

相关问题 如何从ember中的输入助手上的事件获取event.target - How to get an event.target from event on input helper in ember 当 event.target 返回子元素时,如何只获取点击事件的父元素? - How do you get just the parent element of a click event when event.target is returning the children? 灰烬:如何在不是目标的视图中获取鼠标事件 - Ember: How to get mouse event in a view that's not the target 如何从点击事件目标中获取样式? - How to get style from click event target? 如何检测元素外部的点击并通过ember发送事件? - How to detect a click outside an element and send the event by ember? 如何从点击目标中获取上一个元素? - How to get previous element from click target? 如何在单击处理程序中获取与event.target元素对应的react组件实例? - How to get the react component instance corresponding to event.target element in a click handler? React/Next.js 如何在按钮单击时获取其他元素事件目标值 - React/Next.js how to get other Element event target value on button click 如何使元素(而不是目标)成为jQuery中的事件? - How to get the element made (not target) the event in jQuery? 如何获取&#39;selectionchange&#39;dom事件的目标元素 - How to get the target element for an 'selectionchange' dom event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM