最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getElementById in Polymer element - Stack Overflow

programmeradmin5浏览0评论

How can I use getElementById in a Polymer custom element?

Here is my element:

<link rel="import" href="../bower_ponents/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>

When I run the code I get this error message: Uncaught TypeError: Cannot read property 'style' of null

Obviously I'm doing something wrong but I don't know what.

How can I use getElementById in a Polymer custom element?

Here is my element:

<link rel="import" href="../bower_ponents/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>

When I run the code I get this error message: Uncaught TypeError: Cannot read property 'style' of null

Obviously I'm doing something wrong but I don't know what.

Share Improve this question edited Sep 26, 2016 at 20:53 Dan Dascalescu 152k64 gold badges332 silver badges419 bronze badges asked Jul 25, 2016 at 8:21 Zvi KarpZvi Karp 3,9043 gold badges28 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

I'd use

ready: function() {
  this.$.hideme.style.display = 'none';
}

of when the element is inside <template dom-if...> or <template dom-repeat...>

ready: function() {
  this.$$('#hideme').style.display = 'none';
}   

In the end, I'd use class binding and bind a class to the element and update a property to reflect that change and use CSS to set style.display

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

  is: 'bb-calendar',

  properties: {
      hiddenClass: String,
  },

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

});

Your problem is actually that your element is not attached to the document DOM when the ready callback is fired. For simply showing/hiding an element you may use the hidden attribute like this: <div hidden$="{{!shouldShow}}">

发布评论

评论列表(0)

  1. 暂无评论