简体   繁体   English

在Polymer中访问html内部内容标签

[英]Access html inside content tag in Polymer

I am trying to access the html that i wrote inside my own polymer-element but it isn't working. 我正在尝试访问我在自己的聚合物元素中编写的html,但它无法正常工作。 How can I achieve this? 我该如何实现?

index.html index.html

<my-element> content I am trying to access </my-element>

my-element.html my-element.html

<polymer-element name='my-element'>
    <template>
        <content id='content'></content>
    </template>
    <script>
        Polymer({
            ready: function(){
                console.log('html: '+this.$.content.innerHTML);
            }
        });
    </script>
</polymer-element>

console 安慰

html:

Unfortunately, the implementation inserts content nodes as siblings of content , not as nested nodes. 不幸的是,执行插入内容节点为兄弟姐妹 content ,而不是嵌套节点。 That said, after everything is ready, the structure of document would look like: 也就是说,一切准备就绪后,文档的结构将如下所示:

<content id='content'></content>
content I am trying to access

not as you probably were expecting: 不像您预期​​的那样:

<!-- THAT IS NOT HOW IT’S COMPOSED -->
<content id='content'>content I am trying to access</content>

Fortunately, you are still having an access to childNodes within template context: 幸运的是,您仍然可以访问模板上下文中的childNodes

  Polymer({
      domReady: function() {
        var content = '';
        this.childNodes.array().forEach( function(el) {
          content += el.textContent; // do whatever you want here
        });
        console.log(content);
      }
  });

  //⇒ content I am trying to access 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM