CSSStyleDeclaration setProperty() 方法

定义和用法

setProperty() 方法在 CSS 声明块中设置新的 CSS 属性,或修改现有的属性。

实例

例子 1

设置新的 CSS 属性:

var declaration = document.styleSheets[0].cssRules[0].style;
var setprop = declaration.setProperty("background-color", "yellow");

亲自试一试

例子 2

设置具有 "important" 优先级的新 CSS 属性:

var declaration = document.styleSheets[0].cssRules[0].style;
var setprop = declaration.setProperty("background-color", "yellow", "important");

亲自试一试

例子 3

修改现有的 CSS 属性:

var declaration = document.styleSheets[0].cssRules[0].style;
var setprop = declaration.setProperty("color", "blue");

亲自试一试

语法

object.setProperty(propertyname, value, priority)

参数

参数 描述
propertyname 必需。字符串,表示要设置的属性的名称。
value Optional. A string representing the new value.
priority

Optional. A string indicating whether the priority of the attribute should be set to important.

Valid values:

  • "important"
  • undefined
  • ""

Technical details

DOM version: CSS Object Model
Return value: undefined

Browser support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support 9.0 Support Support Support