CSSStyleDeclaration.setProperty()
方法接口为一个声明了CSS样式的对象设置一个新的值 。
语法
style.setProperty(propertyName, value, priority);
参数
返回值
异常
DOMException
(NoModificationAllowedError): 如果属性或装饰块为只读的,抛出此异常。
JavaScript 有一个特别的,更简单的在 CSSStyleDeclaration 对象上设置CSS属性值的语法:
style.cssPropertyName = 'value';
示例
下面的Javascript代码为一个选中元素样式的 margin
属性设置一个新的值:
var declaration = document.styleSheets[0].rules[0].style;
declaration.setProperty('margin', '1px 2px');
// Equivalent to:
// declaration.margin = '1px 2px';
说明
描述 | 状态 | 注释 |
---|---|---|
CSS Object Model (CSSOM) CSSStyleDeclaration.setProperty() |
工作草案 | |
Document Object Model (DOM) Level 2 Style Specification CSSStyleDeclaration |
被废弃 |
浏览器兼容
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
特性 | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
基础支持 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
特性 | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
基础支持 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |