--- title: CSSStyleSheet.insertRule() slug: Web/API/CSSStyleSheet/insertRule translation_of: Web/API/CSSStyleSheet/insertRule ---
CSSStyleSheet.insertRule() 方法新增一個新的 CSS 規則,到當前的樣式表,他伴隨著一些限制.
更明確的說,雖然 insertRule() 只是一個 {{domxref("CSSStyleSheet")}} 的方法, 他實際上插入這份規則到 {{domxref("CSSStyleSheet")}}.cssRules, 在 {{domxref("CSSRuleList")}} 之中。
這份規則,必須包含的內容,取決於它的類型: 對於規則集 (rule-sets),規則同時指定了選擇器和样式聲明。 對於規則 (at-rules),規則同時指定 at 標識符( at-identifier )和規則內容。
stylesheet.insertRule(rule[, index])
無符號整數,代表在 {{domxref("CSSStyleSheet")}}.cssRules 中插入的位置,其中 index-0 是第一個規則,而 index-max 就是最後一個規則,並且與 CSSStyleSheet 的長度相同。cssRules 在舊的實現中是必需的。查詢「瀏覽器兼容」取得詳細信息。 默認值為 0。
The index within the style sheet's rule-list of the newly inserted rule.
CSS 樣式表規則列表,有一些直覺的、和不是那麼直覺的限制 ,影響著規則的插入方式和位置。
違反這些可能會導致 DOM 異常 ({{domxref("DOMException")}}) 引發錯誤。
// push a new rule onto the top of my stylesheet myStyle.insertRule("#blanc { color: white }", 0);
/** * 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); } }
Specification | Status | Comment |
---|---|---|
{{SpecName('CSSOM', '#dom-cssstylesheet-insertrule', 'CSSStyleSheet.insertRule')}} | {{Spec2('CSSOM')}} | No change from {{SpecName('DOM2 Style')}}. |
{{SpecName('DOM2 Style', 'css.html#CSS-CSSStyleSheet-insertRule', 'CSSStyleSheet.insertRule')}} | {{Spec2('DOM2 Style')}} | Initial definition |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | 9 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
index is optional |
{{CompatChrome(60)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatOpera(47)}} | {{CompatUnknown}} |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
index is optional |
{{CompatChrome(60)}} | {{CompatChrome(60)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatOperaMobile(47)}} | {{CompatUnknown}} |
addRule('pre', 'font: 14px verdana'); // add rule at end
removeRule()
and .rules
instead of {{domxref("CSSStyleSheet.deleteRule","deleteRule()")}} and {{domxref("CSSStyleSheet",".cssRules")}} respectively.