Polymer 1.0 on-tap event's target is child

谁说我不能喝 提交于 2019-12-13 14:09:23

问题


I'm using on-tap to detect element clicks.

<div id="parent" on-tap="myclickEvent">
    <h1>Some text<h1>
</div>

But when i click on it, the target attribute in the fired event is the h1. How do I change to make the parent element be the target?


回答1:


Try e.currentTarget. Don't expect it to be shown in console.log though as that browser function is asynchronous in Chrome (actually don't even bother logging event objects or objects inside events). But you can see it actually returns a div if you try this in your code.

myclickEvent: function(e){
  ...
  var tag = e.currentTarget.tagName;
  console.log(tag);
}



回答2:


here is very useful notes you should take care of:

  • make sure your element has a unique id (this is very important)
  • if your element inside the template use event.currentTarget
  • if your element inside the content (shady DOM or shadow DOM) use Polymer.dom(event).rootTarget


来源:https://stackoverflow.com/questions/30596180/polymer-1-0-on-tap-events-target-is-child

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!