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