aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/cssstylesheet
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/api/cssstylesheet
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/zh-tw/web/api/cssstylesheet')
-rw-r--r--files/zh-tw/web/api/cssstylesheet/index.html187
-rw-r--r--files/zh-tw/web/api/cssstylesheet/insertrule/index.html219
2 files changed, 406 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/cssstylesheet/index.html b/files/zh-tw/web/api/cssstylesheet/index.html
new file mode 100644
index 0000000000..01abf1a942
--- /dev/null
+++ b/files/zh-tw/web/api/cssstylesheet/index.html
@@ -0,0 +1,187 @@
+---
+title: CSSStyleSheet
+slug: Web/API/CSSStyleSheet
+tags:
+ - API
+ - CSSOM
+ - NeedsTranslation
+ - Reference
+ - TopicStub
+translation_of: Web/API/CSSStyleSheet
+---
+<div>{{APIRef("CSSOM")}}</div>
+
+<p>The <strong><code>CSSStyleSheet</code></strong> interface represents a single <a href="/en-US/docs/Web/CSS">CSS</a> style sheet. It inherits properties and methods from its parent, {{domxref("StyleSheet")}}.</p>
+
+<p>A style sheet consists of <em>{{domxref("CSSRule", "rules", "", 1)}}</em>, such as <em>{{domxref("CSSStyleRule", "style rules", "", 1)}}</em><em> </em>("<code>h1,h2 { font-size: 16pt }"</code>), various <em>at-rules</em> (<code>@import</code>, <code>@media</code>, ...), etc. This interface lets you inspect and modify the list of rules in the stylesheet.</p>
+
+<p>See the {{anch("Notes")}} section for the various ways a CSSStyleSheet object can be obtained.</p>
+
+<h2 id="Properties">Properties</h2>
+
+<p><em>Inherits properties from its parent, {{domxref("StyleSheet")}}.</em></p>
+
+<dl>
+ <dt id="cssRules">{{domxref("CSSStyleSheet.cssRules")}}</dt>
+ <dd>Returns a live {{domxref("CSSRuleList")}}, listing the {{domxref("CSSRule")}} objects in the style sheet.<br>
+ This is normally used to access individual rules like this:<br>
+ <code>   styleSheet.cssRules[i] // where i = 0..cssRules.length-1</code><br>
+ To add or remove items in <code>cssRules</code>, use the <code>CSSStyleSheet</code>'s <code>deleteRule()</code> and <code>insertRule()</code> methods, described below.</dd>
+ <dt id="ownerRule">{{domxref("CSSStyleSheet.ownerRule")}}</dt>
+ <dd>If this style sheet is imported into the document using an {{cssxref("@import")}} rule, the <code>ownerRule</code> property will return that {{domxref("CSSImportRule")}}, otherwise it returns <code>null</code>.</dd>
+</dl>
+
+<h2 id="Methods">Methods</h2>
+
+<p><em>Inherits methods from its parent, {{domxref("Stylesheet")}}.</em></p>
+
+<dl>
+ <dt id="deleteRule">{{domxref("CSSStyleSheet.deleteRule()")}}</dt>
+ <dd>Deletes a rule at the specified position from the style sheet.</dd>
+ <dt id="insertRule">{{domxref("CSSStyleSheet.insertRule()")}}</dt>
+ <dd>Inserts a new rule at the specified position in the style sheet, given the textual representation of the rule.</dd>
+</dl>
+
+<h2 id="Notes">Notes</h2>
+
+<p>In some browsers, if a stylesheet is loaded from a different domain, calling <code>cssRules</code> results in <code>SecurityError</code>.</p>
+
+<p>A stylesheet is associated with at most one {{domxref("Document")}}, which it applies to (unless {{domxref("StyleSheet.disabled", "disabled", "", 1)}}). A list of <code>CSSStyleSheet</code> objects for a given document can be obtained using the {{domxref("document.styleSheets")}} property. A specific style sheet can also be accessed from its <em>owner</em> object (<code>Node</code> or <code>CSSImportRule</code>), if any.</p>
+
+<p>A <code>CSSStyleSheet</code> object is created and inserted into the document's <code>styleSheets</code> list automatically by the browser, when a style sheet is loaded for a document. As the {{domxref("document.styleSheets")}} list cannot be modified directly, there's no useful way to create a new <code>CSSStyleSheet</code> object manually (although <a href="http://tabatkins.github.io/specs/construct-stylesheets/">Constructable Stylesheet Objects</a> might get added to the Web APIs at some point). To create a new stylesheet, insert a {{HTMLElement("style")}} or {{HTMLElement("link")}} element into the document.</p>
+
+<p>A (possibly incomplete) list of ways a style sheet can be associated with a document follows:</p>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Reason for the style sheet to be associated with the document</th>
+ <th scope="col">Appears in <code>document.<br>
+ styleSheets</code> list</th>
+ <th scope="col">Getting the owner element/rule given the style sheet object</th>
+ <th scope="col">The interface for the owner object</th>
+ <th scope="col">Getting the CSSStyleSheet object from the owner</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{HTMLElement("style")}} and {{HTMLElement("link")}} elements in the document</td>
+ <td>Yes</td>
+ <td>{{domxref("StyleSheet.ownerNode", ".ownerNode")}}</td>
+ <td>{{domxref("HTMLLinkElement")}},<br>
+ {{domxref("HTMLStyleElement")}},<br>
+ or {{domxref("SVGStyleElement")}}</td>
+ <td>{{domxref("LinkStyle.sheet", ".sheet")}}</td>
+ </tr>
+ <tr>
+ <td>CSS {{cssxref("@import")}} rule in other style sheets applied to the document</td>
+ <td>Yes</td>
+ <td>{{domxref("CSSStyleSheet.ownerRule", ".ownerRule")}}</td>
+ <td>{{domxref("CSSImportRule")}}</td>
+ <td>{{domxref("CSSImportRule.styleSheet", ".styleSheet")}}</td>
+ </tr>
+ <tr>
+ <td><code>&lt;?xml-stylesheet ?&gt;</code> processing instruction in the (non-HTML) document</td>
+ <td>Yes</td>
+ <td>{{domxref("StyleSheet.ownerNode", ".ownerNode")}}</td>
+ <td>{{domxref("ProcessingInstruction")}}</td>
+ <td>{{domxref("LinkStyle.sheet", ".sheet")}}</td>
+ </tr>
+ <tr>
+ <td>HTTP Link Header</td>
+ <td>Yes</td>
+ <td><em>N/A</em></td>
+ <td>N/A</td>
+ <td>N/A</td>
+ </tr>
+ <tr>
+ <td>User agent (default) style sheets</td>
+ <td>No</td>
+ <td>N/A</td>
+ <td>N/A</td>
+ <td>N/A</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th>Specification</th>
+ <th>Status</th>
+ <th>Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName("CSSOM", "#cssstylesheet", 'CSSStyleSheet')}}</td>
+ <td>{{Spec2("CSSOM")}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM2 Style", "css.html#CSS-CSSStyleSheet", "CSSStyleSheet")}}</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 (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>9.0</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Edge</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Phone</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>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/API/CSS_Object_Model/Using_dynamic_styling_information">Using dynamic styling information</a></li>
+</ul>
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 &gt; 樣式表中規則數量 (`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 &lt; 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 &lt; 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>