0

我正在尝试使用 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);
4

2 回答 2

0

为项目实现此目的的一种方法<dom-repeat>是使用事件currentTarget,以便您可以选择调用该方法的元素。我有一个示例,其中您有多个列表项 ( paper-listbox),其中右箭头图标在iron-collapse项目展开时变为向下箭头。在我的示例中,不需要任何课程,但您可以轻松拥有e.currentTarget.querySelector('.theClass')

例子:

<dom-repeat items={{list}}>
    <template>
        <paper-listbox on-click="toggle">
            <iron-icon id="icon" icon="icons:chevron-right"></iron-icon>
            <paper-item>{{item.title}}</paper-item>
            <iron-collapse class="collapse">
                <p>Content here . . . </p>
            </iron-collapse>
        </paper-listbox>
    </template>
</dom-repeat>

和它下面的JS:

toggle(e) {
    // currentTarget selects the paper-listbox where the toggle method occurred.
    var collapseArea = e.currentTarget.querySelector('iron-collapse');
    var theIcon = e.currentTarget.querySelector('iron-icon');
    collapseArea.toggle();
    // toggle the icon for list item from right arrow to downward arrow
    (theIcon.getAttribute('icon') === 'icons:chevron-right') ? theIcon.setAttribute('icon', 'icons:expand-more') : theIcon.setAttribute('icon', 'icons:chevron-right');
}
于 2018-06-19T17:18:41.077 回答
0

我建议你使用自定义属性。这是有效的解决方案:https ://codepen.io/Sahero/pen/VzeWNd

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>      
      <button on-click="toggle" my-attribute="collapse1">toggle 
      collapse</button>
      <iron-collapse id="collapse1" class="collapse">
        <div>Enter text collapse1</div>
      </iron-collapse>   

      <button on-click="toggle" my-attribute="collapse2">toggle 
      collapse</button>
      <iron-collapse id="collapse2" class="collapse">
      <div>Enter text collapse2</div>
      </iron-collapse>

      <button on-click="toggle" my-attribute="collapse3">toggle 
      collapse</button>
      <iron-collapse id="collapse3" class="collapse">
      <div>Enter text collapse3</div>
      </iron-collapse>

    </template>
  </dom-module>
</body>

JS:

  class XFoo extends Polymer.Element {
    static get is() { return 'x-foo'; }

    static get properties() {
      return {};

    }
   toggle(e) {
     //console.log(this);
     var id = e.target.getAttribute('my-attribute');
     console.log(id);
     this.shadowRoot.querySelector('#'+id).toggle();
     //this.shadowRoot.querySelector('.collapse').toggle();
  }

  }
  customElements.define(XFoo.is, XFoo);
于 2017-07-30T04:02:55.860 回答