diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
| commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
| tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/api/cssstylesheet/insertrule | |
| parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
| download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip | |
initial commit
Diffstat (limited to 'files/zh-tw/web/api/cssstylesheet/insertrule')
| -rw-r--r-- | files/zh-tw/web/api/cssstylesheet/insertrule/index.html | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/cssstylesheet/insertrule/index.html b/files/zh-tw/web/api/cssstylesheet/insertrule/index.html new file mode 100644 index 0000000000..3cd8ba5a87 --- /dev/null +++ b/files/zh-tw/web/api/cssstylesheet/insertrule/index.html @@ -0,0 +1,219 @@ +--- +title: CSSStyleSheet.insertRule() +slug: Web/API/CSSStyleSheet/insertRule +translation_of: Web/API/CSSStyleSheet/insertRule +--- +<div>{{APIRef("CSSOM")}} </div> + +<p><strong>CSSStyleSheet.insertRule() </strong>方法新增一個新的 CSS 規則,到當前的樣式表,他伴隨著一些<a href="#Restrictions">限制</a>. </p> + +<p>更明確的說,雖然 <strong>insertRule()</strong> 只是一個 {{domxref("CSSStyleSheet")}} 的方法, 他實際上插入這份規則到 {{domxref("CSSStyleSheet")}}.<em>cssRules</em>, 在 {{domxref("CSSRuleList")}} 之中。</p> + +<p>這份規則,必須包含的內容,取決於它的類型: 對於規則集 (rule-sets),規則同時指定了選擇器和样式聲明。 對於規則 (at-rules),規則同時指定 at 標識符( at-identifier )和規則內容。</p> + +<h2 id="Syntax"><em><em>Syntax</em></em></h2> + +<pre class="syntaxbox"><em> +<em> +<var>stylesheet</var>.insertRule(<var>rule</var>[, <var>index</var>])</em></em></pre> + +<h3 id="Parameters"><em><em>Parameters</em></em></h3> + +<dl> + <dt><em><em>rule</em></em></dt> + <dd><em><em>一個 {{domxref("DOMString")}} 包含要被插入的規則,這份規則同時指定了選擇器( <a href="/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors">selector</a> )和样式聲明,或 at 標識符 (at-identifier ) 和規則內容。</em></em></dd> + <dt><em><em>index {{optional_inline}}</em></em></dt> + <dd> + <p><em><em>無符號整數,代表在 <em><em>{{domxref("CSSStyleSheet")}}<em>.</em>cssRules </em></em>中插入的位置,其中 index-0 是第一個規則,而 index-max 就是最後一個規則,並且與 CSSStyleSheet 的長度相同。cssRules 在舊的實現中是必需的。查詢「瀏覽器兼容」取得詳細信息。 默認值為 0。</em></em></p> + </dd> +</dl> + +<h3 id="Return_value"><em><em><em><em>Return value</em></em></em></em></h3> + +<p><em><em><em><em>The index within the style sheet's rule-list of the newly inserted rule.</em></em></em></em></p> + +<h3 id="Restrictions_限制"><em><em><em><em>Restrictions 限制</em></em></em></em></h3> + +<p><em><em>CSS 樣式表規則列表,有一些直覺的、和不是那麼直覺的<a href="https://drafts.csswg.org/cssom/#insert-a-css-rule">限制</a> ,影響著規則的插入方式和位置。<br> + 違反這些可能會導致 DOM 異常 ({{domxref("DOMException")}}) 引發錯誤。</em></em></p> + +<ul> + <li><em><em><em><em>如果 index > 樣式表中規則數量 (`CSSRuleList.length`),他會中止,顯示 IndexSizeError (索引大小錯誤)。</em></em></em></em></li> + <li><em><em><em><em>如果 rule 無法在索引 0 插入,因為一些 CSS 因素的限制,他會中止,顯示 HierarchyRequestError (層次結構請求錯誤)。</em></em></em></em></li> + <li><em><em><em><em>如果規則參數中給出多個規則,他會中止,顯示 SyntaxError (語法錯誤)。</em></em></em></em></li> + <li><em><em><em><em>如果嘗試在樣式規則之後插入 `@import at-rule`,他會中止,顯示 HierarchyRequestError (層次結構請求錯誤)。</em></em></em></em></li> + <li><em><em><em><em>如果規則是 `@namespace at-rule`,且列表不只有 `@import at-rules` 和 / 或 `@namespace at-rules`他會中止,顯示 InvalidStateError (狀態錯誤無效)。</em></em></em></em></li> +</ul> + +<h2 id="Examples">Examples</h2> + +<h3 id="Example_1">Example 1</h3> + +<pre class="brush: js">// push a new rule onto the top of my stylesheet +myStyle.insertRule("#blanc { color: white }", 0); +</pre> + +<h3 id="Example_2">Example 2</h3> + +<pre class="brush: js">/** + * Add a stylesheet rule to the document (may be better practice, however, + * to dynamically change classes, so style information can be kept in + * genuine stylesheets (and avoid adding extra elements to the DOM)) + * Note that an array is needed for declarations and rules since ECMAScript does + * not afford a predictable object iteration order and since CSS is + * order-dependent (i.e., it is cascading); those without need of + * cascading rules could build a more accessor-friendly object-based API. + * @param {Array} rules Accepts an array of JSON-encoded declarations + * @example +addStylesheetRules([ + ['h2', // Also accepts a second argument as an array of arrays instead + ['color', 'red'], + ['background-color', 'green', true] // 'true' for !important rules + ], + ['.myClass', + ['background-color', 'yellow'] + ] +]); + */ +function addStylesheetRules (rules) { + var styleEl = document.createElement('style'), + styleSheet; + + // Append style element to head + document.head.appendChild(styleEl); + + // Grab style sheet + styleSheet = styleEl.sheet; + + for (var i = 0, rl = rules.length; i < rl; i++) { + var j = 1, rule = rules[i], selector = rules[i][0], propStr = ''; + // If the second argument of a rule is an array of arrays, correct our variables. + if (Object.prototype.toString.call(rule[1][0]) === '[object Array]') { + rule = rule[1]; + j = 0; + } + + for (var pl = rule.length; j < pl; j++) { + var prop = rule[j]; + propStr += prop[0] + ':' + prop[1] + (prop[2] ? ' !important' : '') + ';\n'; + } + + // Insert CSS Rule + styleSheet.insertRule(selector + '{' + propStr + '}', styleSheet.cssRules.length); + } +}</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('CSSOM', '#dom-cssstylesheet-insertrule', 'CSSStyleSheet.insertRule')}}</td> + <td>{{Spec2('CSSOM')}}</td> + <td>No change from {{SpecName('DOM2 Style')}}.</td> + </tr> + <tr> + <td>{{SpecName('DOM2 Style', 'css.html#CSS-CSSStyleSheet-insertRule', 'CSSStyleSheet.insertRule')}}</td> + <td>{{Spec2('DOM2 Style')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>9</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td><code>index</code> is optional</td> + <td>{{CompatChrome(60)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatOpera(47)}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android Webview</th> + <th>Chrome for Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td><code>index</code> is optional</td> + <td>{{CompatChrome(60)}}</td> + <td>{{CompatChrome(60)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatOperaMobile(47)}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h3 id="Legacy_browser_support">Legacy browser support</h3> + +<ul> + <li> + <h4 id="Internet_Explorer_-_pre_v9"><strong>Internet Explorer - pre v9</strong></h4> + <strong>addRule</strong>(<var>selector</var>, <var>rule</var> [, <var>index</var>]); -- Example:<code> addRule('pre', 'font: 14px verdana'); // add rule at end</code><br> + <br> + <em>Also note the non-standard <code><a class="external" href="http://www.quirksmode.org/dom/w3c_css.html#change">removeRule()</a> </code> and <code><a class="external" href="http://www.quirksmode.org/dom/w3c_css.html#access">.rules</a> </code> instead of {{domxref("CSSStyleSheet.deleteRule","deleteRule()")}} and {{domxref("CSSStyleSheet",".cssRules")}} respectively.</em></li> +</ul> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{domxref("CSSStyleSheet.deleteRule")}}</li> + <li><a class="external" href="http://www-archive.mozilla.org/docs/web-developer/css1technote/css1tojs.html#priority">Cross-Browser CSS-rules ordering (CSS1)</a></li> + <li><a class="external" href="http://www.quirksmode.org/dom/w3c_css.html">Quirksmode - CSS</a></li> +</ul> |
