diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/cssstyledeclaration/setproperty | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/cssstyledeclaration/setproperty')
-rw-r--r-- | files/zh-cn/web/api/cssstyledeclaration/setproperty/index.html | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/cssstyledeclaration/setproperty/index.html b/files/zh-cn/web/api/cssstyledeclaration/setproperty/index.html new file mode 100644 index 0000000000..269faaad26 --- /dev/null +++ b/files/zh-cn/web/api/cssstyledeclaration/setproperty/index.html @@ -0,0 +1,137 @@ +--- +title: CSSStyleDeclaration.setProperty() +slug: Web/API/CSSStyleDeclaration/setProperty +translation_of: Web/API/CSSStyleDeclaration/setProperty +--- +<p>{{ APIRef("CSSOM") }}</p> + +<p><span class="seoSummary"><strong><code>CSSStyleDeclaration.setProperty()</code></strong> 方法接口为一个声明了CSS样式的对象设置一个新的值 。</span></p> + +<h2 id="语法">语法</h2> + +<pre><em>style</em>.setProperty(<em>propertyName</em>, <em>value, priority</em>);</pre> + +<h3 id="参数">参数</h3> + +<ul> + <li><em><code>propertyName</code></em> 是一个 {{domxref('DOMString')}} ,代表被更改的CSS属性。</li> + <li><em><code>value</code></em>{{optional_inline}} 是一个 {{domxref('DOMString')}} ,含有新的属性值。如果没有指定, 则当作空字符串。 + <ul> + <li>注意: <em><code>value</code></em> 不能包含 <code>"!important"</code> --那个应该使用 <em><code>priority</code></em> 参数.</li> + </ul> + </li> + <li><em><code>priority</code></em>{{optional_inline}} 是一个 {{domxref('DOMString')}} 允许设置 "important" CSS 优先级。如果没有指定, 则当作空字符串。</li> +</ul> + +<h3 id="返回值">返回值</h3> + +<ul> + <li>{{jsxref('undefined')}}</li> +</ul> + +<h3 id="异常">异常</h3> + +<ul> + <li>{{domxref('DOMException')}} (NoModificationAllowedError): 如果属性或装饰块为只读的,抛出此异常。</li> +</ul> + +<p>JavaScript 有一个特别的,更简单的在 CSSStyleDeclaration 对象上设置CSS属性值的语法:</p> + +<pre class="syntaxbox"><em>style</em>.cssPropertyName = 'value';</pre> + +<h2 id="示例">示例</h2> + +<p>下面的Javascript代码为一个选中元素样式的 <code>margin</code> 属性设置一个新的值:</p> + +<pre class="brush: js">var declaration = document.styleSheets[0].rules[0].style; +declaration.setProperty('margin', '1px 2px'); +// Equivalent to: +// declaration.margin = '1px 2px'; +</pre> + +<h2 id="说明"> 说明</h2> + +<table> + <thead> + <tr> + <th scope="col">描述</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('CSSOM', '#dom-cssstyledeclaration-setproperty', 'CSSStyleDeclaration.setProperty()')}}</td> + <td>工作草案</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('DOM2 Style', 'css.html#CSS-CSSStyleDeclaration', 'CSSStyleDeclaration')}}</td> + <td>被废弃</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <thead> + <tr> + <th scope="col">特性</th> + <th scope="col">Chrome</th> + <th scope="col">Edge</th> + <th scope="col">Firefox (Gecko)</th> + <th scope="col">Internet Explorer</th> + <th scope="col">Opera</th> + <th scope="col">Safari (WebKit)</th> + </tr> + </thead> + <tbody> + <tr> + <td>基础支持</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>特性</th> + <th>Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>基础支持</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> + +<div> +<h3 id="want_learn_more">want learn more</h3> +<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Window/getComputedStyle">getComputedStyle</a><br> +<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/CSSStyleDeclaration/getPropertyValue">getPropertyValue</a></div> +</div> |