【问题标题】:IE modifying stylesheet rule does not refreshIE修改样式表规则不刷新
【发布时间】:2013-01-29 19:35:41
【问题描述】:

我正在通过 JS 向样式表添加规则,然后尝试编辑该规则。它在其他地方可以正常工作,但在 IE 中不行。

我知道在 IE 中访问样式表和规则的区别,我想修改现有属性也一定有区别,请参阅我的(仅限 IE)示例:

<style type="text/css" rel="stylesheet" id="tmp-style"></style>
<h3>Test</h3>
<script type="text/javascript">
    var sheet = document.styleSheets[0];
    sheet.addRule('h3', 'background-color: red', 0);
    console.log( sheet.rules[0].selectorText + ' = ' + sheet.rules[0].style['background-color'] );
    sheet.rules[0].style['background-color'] = 'blue';
    console.log( sheet.rules[0].selectorText + ' = ' + sheet.rules[0].style['background-color'] );
</script>

H3 保持红色,控制台显示:

LOG: h3 = undefined 
LOG: h3 = blue

【问题讨论】:

    标签: javascript internet-explorer stylesheet


    【解决方案1】:

    好的,事实证明,要在 IE 中通过 JS 以这种方式访问​​样式属性,您必须使用驼峰式表示法来访问它们,例如:

    sheet.rules[0].style['backgroundColor'] = 'blue';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      相关资源
      最近更新 更多