dom-change
事件对象e在Polymer.dom(e).localTarget
给我<template is="dom-if"
,因为它应该。但我如何访问孩子<div id="uploadedImage1"
? firstChild
和firstElementChild
为空。如何从模板元素中获取子元素?
<template is="dom-if" if="[[uploadedImage1]]">
<div id="uploadedImage1" class="row-image horizontal layout">
foo
</div>
</template>
我想也许我需要做的另一个查询离localTarget的对象,但没有成功:
this.$['images-container'].addEventListener('dom-change', (e)=> {
var bob = Polymer.dom(e).localTarget;
var sue = Polymer.dom(bob).querySelector('div')
console.log(sue);
});
我也尝试了这些没有成功:
this.$['images-container'].addEventListener('dom-change', (e)=> {
var bob = Polymer.dom(e).localTarget;
var sue = this.queryEffectiveChildren(bob);
console.log(sue);
});
this.$['images-container'].addEventListener('dom-change', (e)=> {
var bob = Polymer.dom(e).localTarget;
var sue = bob.getEffectiveChildNodes();
console.log(sue);
});
}
从children length documentation example,甚至Polymer.dom(this).children.length
返回0:
<dom-module id="image-uploader">
<template>
<style include="iron-flex iron-flex-factors iron-flex-alignment">
<div id="images-container" class="horizontal layout center wrap">
<div hidden="[[uploadedImage1]]" class="layout vertical center-center">
<div class="row-image horizontal layout">
<input
type="file"
name="file"
data-uploaded="uploadedImage1"
id="image1"
accept="image/jpeg"
class="inputfile" />
<label
for="image1"
id="image1"
class="image-show horizontal layout center center-justified">
<iron-icon icon="add" prefix></iron-icon>
Upload Image
</label>
</div>
<i>*Main Image</i>
</div>
<template is="dom-if" id="foo" if="[[uploadedImage1]]">
<div id="uploadedImage1">
foo
</div>
</template>
</div>
</template>
<script>
Polymer({
is: 'image-uploader',
properties: {
uploadedImage1: {
type: Boolean,
value: false
}
},
ready: function(e) {
console.log(Polymer.dom(this).children.length);
我想这可能在这里回答:http://stackoverflow.com/questions/28593494/accessing-a-div-inside-of-a-polymer-element-template –
谢谢,但这不会工作我。它将在'dom-repeat'内部,所以class/id将不起作用。因此,我会一直抓住'localTarget'的第一个孩子。但是'localTarget'对于有孩子的孩子显示为空。 – dman