聚合物事件目标

时间:2018-03-15 09:35:45

标签: javascript events polymer

如果我将tap-event-listener添加到聚合物组件的实例中,并在其本地dom中包含一些元素,则事件的目标(和源)可以是本地dom的元素。

什么是始终引用主机元素而不是本地dom中的任何子元素的最佳方法?

示例:具有本地dom的组件

<dom-module id="x-foo">
  <template> 
    <p>Foo</p>
    <img id="image" src$="[[src]]" width$="[[width]]" height$="[[height]]">
  </template>
  <script src="x-foo.js"></script>
</dom-module>

使用组件并添加侦听器

<x-foo on-tap="_onTap">
...
_onTap: function(e) {
   // here i need a reference to the element
   // that listens to the tap (host / x-foo)
   // but if i tap on the img, the target is a
   // reference to the img element
}

谢谢

电贺, Meisenmann

2 个答案:

答案 0 :(得分:2)

您可以尝试使用

join -j2 <(sort -g -k2 file1) <(sort -g -k2 file2) | awk '{t=$2;$2=$1;$1=t}1' | sort -g -k1 

而不是

e.currentTarget

here

所述

您可以在说明中看到它&#34; 标识事件的当前目标,因为事件遍历DOM。它始终引用事件处理程序附加到的元素,而不是event.target,它标识发生事件的元素& #34;

答案 1 :(得分:0)

正如Mishu在他关于e.currentTarget的评论中所建议的那样,还有一点需要注意的是,如果你想知道你的事件所经过的地方有多少,请检查composedPath()这种方法是否跟踪你的活动经历过的所有地方!