聚合物,如何访问内部元素 - 纸张输入

时间:2014-12-18 17:06:47

标签: javascript polymer

如何访问Polymer元素上脚本部分的内部元素?

1 个答案:

答案 0 :(得分:1)

以下是示例plnkr.co

<script>
      Polymer("my-element", {

       ready: function() {

              //access the input element by 'Change' event
              container_test = this.$.container; //correct one
             // or: var container_test  = this.shadowRoot.querySelector('#' + container);
             // from: http://stackoverflow.com/questions/24588708/polymer-automatic-node-finding-with-dynamic-id-value
              container_test.addEventListener('input', function(event) {

                  console.log('input event');
                  console.log(event);
                  console.log(event.target.tagName);
                  //console.log(event.target);
                  console.log(event.target.attributes.custom_attr.value);

            });
        },

        //access the input element by Declarative event mapping
        clickHandler: function(event) {
                  console.log('clickHandler event');
                  console.log(event);
                  console.log(event.target.tagName);
         },

    });
  </script>