简体   繁体   English

获取元素内容而不在Polymer中的页面上显示

[英]Get element content without displaying on page in Polymer

I am building a custom element with Polymer 1.0 and need to get the content inside the tag using javascipt without it being displayed on the page. 我正在使用Polymer 1.0构建自定义元素,并且需要使用javascipt在标签内获取内容,而无需在页面上显示该内容。 I tried using the <content> tag and fetching it inside the javascript with a query selector. 我尝试使用<content>标记,并使用查询选择器将其提取到javascript内部。 This works except it displays on the page even if I use a style="display: none;" 即使我使用style="display: none;"它也可以在页面上显示,但效果很好style="display: none;" attribute. 属性。 How can I get the content inside the tag without it displaying on the page? 如何在标签内显示内容而不在页面上显示?

Not sure what exactly your use-case is, but how about simply wrapping your <content> tag with a hidden <div> ? 不确定用例到底是什么,但是用隐藏的<div>简单包装<content>标记又如何呢?

<dom-module id="x-test">
  <template>
    <div hidden>
      <content id="content"></content>
    </div>
  </template>
  <script>
    Polymer({
      is: "x-test",
      attached: function () {
        // access distributed content like this
        var myContent = Polymer.dom(this.$.content).getDistributedNodes();
      }
    });
  <script>
</dom-module>

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

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