Polymer元素中的getElementById

Zvi*_*arp 11 html javascript polymer polymer-1.0

如何在Polymer自定义元素中使用getElementById?

这是我的元素:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../styles/shared-styles.html">

<dom-module id="bb-calendar">

  <template>

    <style is="custom-style" include="shared-styles"></style>

    <div class="card">
            <paper-toolbar>
                <div title>Calendar</div>
            </paper-toolbar>
            <div id="hideme">
                <div>this should be hidden</div>
            </div>
    </div>

  </template>

  <script>

    Polymer({

      is: 'bb-calendar',

      ready: function() {
        document.getElementById("hideme").style.display = 'none';
      }

    });

  </script>

</dom-module>
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我收到此错误消息: Uncaught TypeError: Cannot read property 'style' of null

显然我做错了但我不知道是什么.

Gün*_*uer 11

我用了

ready: function() {
  this.$.hideme.style.display = 'none';
}
Run Code Online (Sandbox Code Playgroud)

当元素在里面<template dom-if...>或时<template dom-repeat...>

ready: function() {
  this.$$('#hideme').style.display = 'none';
}   
Run Code Online (Sandbox Code Playgroud)

最后,我将使用类绑定并将类绑定到元素并更新属性以反映该更改并使用CSS来设置 style.display

<template>
  <style>
    .hidden { display:none; }    
  </style>
   ...
  <div class$="{{hiddenClass}}">
    <div>this should be hidden</div>
  </div>
Run Code Online (Sandbox Code Playgroud)
Polymer({

  is: 'bb-calendar',

  properties: {
      hiddenClass: String,
  },

  ready: function() {
    this.hiddenClass = 'hidden';
  }

});
Run Code Online (Sandbox Code Playgroud)