我正在尝试使用 class 而不是 id 来实现多次铁塌陷
问题中提到的解决方案-聚合物多重铁塌陷 有效,但我有更多铁塌陷,我不想为相同的功能创建重复的代码
我尝试了不同的选项,试图使用 class 而不是 id 选择相应的元素
codepen- https://codepen.io/nagasai/pen/YxyqmE
HTML:
<head>
<base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-collapse/iron-collapse.html">
<link rel="import" href="paper-input/paper-input.html">
</head>
<body>
<x-foo attr="{{text}}"></x-foo>
<dom-module id="x-foo">
<template id="collapse1">
<button on-click="toggle">toggle collapse</button>
<iron-collapse class="collapse">
<div>Enter text collapse1</div>
</iron-collapse>
<button on-click="toggle">toggle collapse</button>
<iron-collapse class="collapse">
<div>Enter text collapse2</div>
</iron-collapse>
<button on-click="toggle">toggle collapse</button>
<iron-collapse class="collapse">
<div>Enter text collapse2</div>
</iron-collapse>
</template>
</dom-module>
</body>
JS:
class XFoo extends Polymer.Element {
static get is() { return 'x-foo'; }
static get properties() {
return {};
}
toggle() {
console.log(this);
this.$.querySelector('.collapse').toggle();
}
}
customElements.define(XFoo.is, XFoo);