2016-07-27 64 views
1

在此jsbin如何更新元素的內容並添加類?如何更新聚合物組件中的內容和類?

看來,this.$.content.innerHTML = "Changed?"this.classList.add('new')將無法​​正常工作。

在jsbin的代碼如下:

<!DOCTYPE html> 
<html> 
<head> 
    <base href="http://polygit.org/components/"> 
    <script src="webcomponentsjs/webcomponents.min.js"></script> 
    <link href="polymer/polymer.html" rel="import"> 
</head> 
<body> 
    <my-element>Some text</my-element> 
    <dom-module id="my-element"> 
    <template> 
     <style> 
     :host { 
      background: lightgrey; 
     } 
     .new { 
      background: blue; 
     } 
     </style> 
     <content> 
     </content> 
     <button on-tap="make">Make blue</button> 
    </template> 
    <script> 
     HTMLImports.whenReady(function() { 
     Polymer({ 
      is: 'my-element', 
      make: function() { 
      this.$.content.innerHTML = "Changed?"; 
      this.classList.add('new'); 
      } 
     }) 
     }) 
    </script> 
    </dom-module> 
</body> 
</html> 

回答

0

這奏效了:

:host.new { 
    background: blue; 
} 
... 
make: function() { 
    this.classList.add("new"); 
    Polymer.dom(this).innerHTML = "changed?"; 
}