【发布时间】:2015-07-30 09:37:19
【问题描述】:
我正在使用 Polymer 1.0,并且正在构建一个小型手风琴示例。我有数据绑定到手风琴文本很好,我只是想在点击它时更改手风琴的图标。
下面是我的代码
<dom-module id="ab-accordion">
<template>
<iron-ajax
auto
handle-as="json"
on-response="handleResponse"
debounce-duration="300"
id="ajaxreq"></iron-ajax>
<template is="dom-repeat" id="accordion" items="{{items}}" as="item">
<div class="accordion" on-click="toggleParentAcc">
<div id="accordion_header" class="accordion__header is-collapsed">
<i class="icon icon--chevron-down"></i>
<span>{{item.name}}</span>
</div>
<div id="standard_accordion_body" class="accordion__body can-collapse">
<div class="accordion__content">
<content id="content"></content>
</div>
</div>
</div>
</template>
</template>
<script>
Polymer({
is: "ab-accordion",
//Properties for the Element
properties: {
accordian: Object,
childaccordions: Object,
// Param passed in from the element - Set if the accordion is open by default.
open: String,
data: String,
reqUrl: {
type: String,
value: "https://example.com/service.aspx"
},
},
ready: function () {
this.items = [];
},
attached: function () {
// Run once the element is attached to the DOM.
},
toggleParentAcc: function (event) { // Toggle the classes of the accordions
//This is where I want to toggle the class
this.$.accordion_header.classList.toggle('is-collapsed');
if (typeof event !== 'undefined') {
event.stopPropagation(); // Stop the click from going up to the parent.
}
},
handleResponse: function (e) {
this.items = e.detail.response.sports;
}
});
</script>
</dom-module>
基本上在toggleParentAcc 函数中,我想切换ID 为accordion_header 的div 的类。但我只是得到未定义或空值。
我尝试了以下两行:
this.$.accordion_header // #1
this.$$('#accordion_header') // #2
如何在 dom-repeat 中访问该元素?
更新:我什至无法访问附加函数中的元素。
attached: function(){
this.$.accordion_header // This is null?!
this.$$('#accordion_header'); // this is also null!
}
【问题讨论】:
-
仅供参考 - 我已经删除了 ajax 调用的 URL,只是为了保证我的 API 不会受到攻击。
-
查询本地DOM找到解决方案了吗?
标签: javascript html polymer web-component