【问题标题】:Running a selector on, and visiting, the light DOM tree在轻量级 DOM 树上运行选择器并访问
【发布时间】:2016-11-25 20:13:05
【问题描述】:

我有一个这样的小部件:

<link rel="import" href="../polymer/polymer.html">

<dom-module id="hot-network">
  <template>
    <style>
      :host {
        display: block;
      }
    </style>

    <content id="content"></content>

  </template>

  <script>
    Polymer({

      is: 'hot-network',

      attached: function(){
        var form = this.queryEffectiveChildren( 'iron-form');
        var paperInput = this.queryEffectiveChildren( 'paper-input');

        console.log("WIDGETS:", ironAjaxWidgets );
    },


    });
  </script>
</dom-module>

我想像这样使用这个小部件:

<dom-module id="my-view2">

  <template>

    <hot-network>
      <div>
        <form is="iron-form" id="form" on-iron-form-response="_response" method="PUT" action="/stores/polymer">
          <paper-input required id="name" name="name" label="Your name"></paper-input>
          <paper-button raised type="submit">Click!</paper-button>
        </form>
      </div>
    </hot-network>
  </template>

  <script>


    Polymer({
      is: 'my-view2',
      _response: function(){
        console.log("AH!");
      }
    });
  </script>

</dom-module>

问题 1:如何在 light DOM 中查找元素(使用选择器)?

这两个调用实际上不起作用:

var form = this.queryEffectiveChildren( 'iron-form');
var paperInput = this.queryEffectiveChildren( 'paper-input');

因为他们只在有效子级的“第一级”上运行选择器。

如果我需要在分发的内容中寻找小部件(对于合成很重要),我怎样才能可靠地做到这一点?

问题 2:访问所有有效儿童的最佳方式是什么?

我已经想出了这个代码,但它是近乎淫秽的......我什至无法看到它而不觉得肚子好笑!

  attached: function(){
    this.async( function(){


      function inspect( e ){
        console.log("INSPECTING", e );
      }

      this.getEffectiveChildren().forEach( (effectiveChild) => {
        console.log("Effective child found.");
        inspect( effectiveChild );
        console.log("Getting into all children of it...");
        var foundChildren = effectiveChild.getElementsByTagName("*");
        for( var i = 0, l = foundChildren.length; i < l; i ++ ){
          var child = foundChildren[ i ];
          inspect( child );
        };
        console.log("All children inspected!");
      });

    });
  },

【问题讨论】:

    标签: javascript polymer polymer-1.0 shadow-dom


    【解决方案1】:

    问题 1:

    var form = Polymer.dom(this.root).querySelector('#form');
    // or
    var form = this.$$('#form');
    // or if you wanna use tags/attributes
    var form = Polymer.dom(this.root).querySelector('form[is="iron-form"]');
    

    纸张输入的查询与上述类似。

    【讨论】:

    • 你确实意识到我是在轻量级 DOM 上运行查询,而不是本地 DOM,对吧?
    • 我的意思是你的选择器是错误的。如果您的元素本地 DOM 和轻量级 DOM 的内部和外部没有相同的对象类型,则在选择子节点时几乎没有区别。此外,根据定义,您的轻 DOM 是空的。见:stackoverflow.com/questions/30638632/…
    猜你喜欢
    • 2016-07-28
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    相关资源
    最近更新 更多