问题
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