【问题标题】:Polymer 1.0 can't access elements within nested <template> elementPolymer 1.0 无法访问嵌套 <template> 元素中的元素
【发布时间】: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


【解决方案1】:

https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#node-finding

注意:使用数据绑定动态创建的节点(包括 dom-repeat 和 dom-if 模板中的节点)不会添加到 this.$ 哈希中。哈希仅包括静态创建的本地 DOM 节点(即元素最外层模板中定义的节点)。

我认为如果您改用Polymer.dom(this.root) 会更好。另外,我建议您不要在 dom-repeat 中使用静态 ID,因为它们是唯一的。改用类。

【讨论】:

    【解决方案2】:

    您可能会遇到事件重定向,当事件“冒泡”沿 DOM 树向上时会发生这种情况。 Read this documentation 了解更多。

    当我遇到这个时,我使用类似的方法解决了它:

    var bar = Polymer.dom(event).path[2].getAttribute('data-foo');
    

    在我的 Polymer() 函数中。

    要弄清楚你的情况,你应该去控制台并搜索 DOM 树/事件日志来找到你的目标。如果您无法找到控制台的正确区域,请发表评论,我可能会提供进一步帮助。

    【讨论】:

    • 感谢您的回答,虽然我什至无法访问附加函数中的元素。我想传入一个参数“Open”,如果它是真的,我想默认打开手风琴。这是我需要使用 id 为“accordion_header”的 div 并更改它的类的地方。请查看上面的更新代码并查看我附加的函数。
    【解决方案3】:

    我最终找到了一种无需在嵌套模板中选择元素的方法。

    <template id="accord_template" is="dom-repeat" items="{{items}}" as="item">
            <ab-accordion-row id="[[item.id]]" name="[[item.name]]" open="[[item.open]]">
            </ab-accordion-row>
        </template>
    

    ab-accordion 是另一个元素,我只是将数据提供给它,然后我可以根据参数更改类。

        <div id="accordion" class="accordion" on-click="toggleAccordion">
            <div  class$="{{getClassAccordionHeader(open)}}">
                <i class="icon icon--chevron-down"></i>
                <span>{{name}}</span>
            </div>
            <div id="standard_accordion_body" class$="{{getClassAccordionChild(open)}}">
                <div class="accordion__content">
                    <content></content>
                </div>
            </div>
        </div>
    

    【讨论】:

      【解决方案4】:

      试试这个。

      toggleParentAcc: function (event) { // Toggle the classes of the accordions
          //This is where I want to toggle the  class
          var header = event.target.parentElement;
          Polymer.dom(header).classList.toggle('is-collapsed');
          // rest of your code
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        • 2018-09-12
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多