我试图找出确定事件目标属于哪个父节点的最佳方法。
您有一个定制的聚合物元素,其中包含2种或更多种形式。每个表单都有一个提交按钮:
<form is="iron-form" id="form1" method="post" action="/action1">
<paper-button on-tap="_submitHandler">Send<paper-button>
</form>
<form is="iron-form" id="form2" method="post" action="/action2">
<paper-button on-tap="_submitHandler">Send<paper-button>
</form>
...
<form is="iron-form" id="formN" method="post" action="/action3">
<paper-button on-tap="_submitHandler">Send<paper-button>
</form>
然后在标签中你有了处理程序:
_submitHandler: function(e) {
var path = Polymer.dom(e).path;
var item = null;
var form = null;
for (var i = 0; i < path.length; i++) {
item = path[i];
switch (item) {
case this.$.form1:
form = this.$.form1;
break;
case this.$.form2:
form = this.$.form2;
break;
// ... N cases
}
}
if (form != null) {
form.submit()
}
}
必须有更好的方法来做到这一点......处理这种情况的正确方法是什么?
我正在使用Polymer 1.0
答案 0 :(得分:0)
试试这个:
_submitHandler: function(e) {
var form = var form = Polymer.dom(e).parentNode;
if (form != null) {
form.submit()
}
}
或者这个:
_submitHandler: function(e) {
var form = var form = Polymer.dom(e.target).parentNode;
if (form != null) {
form.submit()
}
}
答案 1 :(得分:0)
执行此操作的方法如下:
_submitHandler: function(e) {
Polymer.dom(e).localTarget.parentNode.submit();
}