我創建了這個組件來演示我的問題。正如預期的那樣,該組件在Chrome和Firefox中運行。但如果我寫this.$.wrapper.setAttribute('class','blue');
而不是this.$.wrapper.setAttribute('class','blue style-scope poly-test');
它停止在Firefox中工作。聚合物1.0元素上的動態類
這是在事件處理程序中更改影子DOM元素上類的首選方法,還是我正在做一件意外的事情,可能 在未來版本中崩潰?
另外,爲什麼我必須指定style-scope
和我的元素名稱作爲類手動爲Firefox?
<link rel="import" href="../js/bower_components/polymer/polymer.html">
<dom-module id="poly-test">
<style>
.blue { border: 10px solid blue; }
.red { border: 10px solid red; }
#wrapper { font-weight: bold; font-size: 42px; }
</style>
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {'blue': { type: 'Boolean', value: false }},
listeners: { 'click': 'clickHandler' },
clickHandler: function() {
this.blue = !this.blue;
if (this.blue) {
this.$.wrapper.setAttribute('class','blue style-scope poly-test');
} else {
this.$.wrapper.setAttribute('class','red style-scope poly-test');
}
this.updateStyles();
}
});
</script>
我應該提到,我測試的所有三個答案,這是一個有效的解決方案太多,但我接受zerodevx的答案指出聲明的做法。 – kazmer