如何让firefox使用style ['attribute-name'] =设置样式属性?

Dan*_* W. 4 javascript css firefox

在此示例中, Chrome将背景设置为红色,firefox和IE则不设置.

试:

document.getElementById("firefoxDiv").style['backgroundColor'] = "Red";
Run Code Online (Sandbox Code Playgroud)

document.getElementById("firefoxDiv").style['background-color'] = "Red";
Run Code Online (Sandbox Code Playgroud)

我宁愿能够使用外部CSS中使用的相同语法 background-color与内联使用javascript.style.backgroundColor =

谢谢你的帮助!

注意:不要 jQuery.

Rob*_*b W 8

使用.style.setProperty(propertyName, value [, priority])而不是expando属性.

例:

document.getElementById("firefoxDiv").style.setProperty('background-color', 'red');
Run Code Online (Sandbox Code Playgroud)

  • 你打败了我...我有一个小提琴和一切:http://jsfiddle.net/zZyVQ/ (2认同)