
JavaScript CSSStyleDeclaration 对象
cssText属性设置或返回元素内联样式的值。
语法:
返回cssText属性:
element.style.cssText
设置cssText属性:
element.style.cssText = style
<h1 id="s1" style="color:red;">CSSStyleDeclaration cssText 属性</h1>
<p id="result"></p>
<script>
function myFunc() {
var elem = document.getElementById("s1");
var output = document.getElementById("result");
output.innerHTML = elem.style.cssText; // "color: red;"
}测试看看‹/›浏览器兼容性
所有浏览器都完全支持cssText属性:
| 属性 | ![]() | ![]() | ![]() | ![]() | ![]() |
| cssText | 是 | 是 | 是 | 是 | 是 |
技术细节
| 返回值: | 一个字符串,表示指定元素的内联样式 |
|---|---|
| DOM版本: | 二级样式CSS |
更多示例
此示例设置元素的内联样式声明的文本:
document.getElementById("s1").style.cssText = 'color: coral;';测试看看‹/›本示例在不使用cssText属性的情况下设置元素文本的颜色:
document.getElementById("s1").style.color = 'coral';测试看看‹/›




