CSSStyleSheet - Web APIs 编辑

The CSSStyleSheet interface represents a single CSS stylesheet, and lets you inspect and modify the list of rules contained in the stylesheet. It inherits properties and methods from its parent, StyleSheet.

A stylesheet consists of a collection of CSSRule objects representing each of the rules in the stylesheet. The rules are contained in a CSSRuleList, which can be obtained from the stylesheet's cssRules property.

For example, one rule might be a CSSStyleRule object containing a style such as:

h1, h2 {
  font-size: 16pt;
}

Another rule might be an at-rule such as @import or @media, and so forth.

See the Notes section for the various ways a CSSStyleSheet object can be obtained.

Properties

Inherits properties from its parent, StyleSheet.

cssRules Read only

Returns a live CSSRuleList which maintains an up-to-date list of the CSSRule objects that comprise the stylesheet.

This is normally used to access individual rules like this:

styleSheet.cssRules[i] // where i = 0..cssRules.length-1

To add or remove items in cssRules, use the CSSStyleSheet's insertRule() and deleteRule() methods.

ownerRule Read only
If this stylesheet is imported into the document using an @import rule, the ownerRule property returns the corresponding CSSImportRule; otherwise, this property's value is null.

Methods

Inherits methods from its parent, StyleSheet.

deleteRule()
Deletes the rule at the specified index into the stylesheet's rule list.
insertRule()
Inserts a new rule at the specified position in the stylesheet, given the textual representation of the rule.

Legacy properties

These properties are legacy properties first introduced by Microsoft long ago; they shouldn't be used but are not likely to be removed anytime soon.

rules Read only
The rules property is functionally identical to the standard cssRules property; it returns a live CSSRuleList which maintains an up-to-date list of all of the rules in the style sheet.

Legacy methods

These methods are legacy methods first introduced by Microsoft; they should not be used if they can be avoided, but are not in danger of being removed anytime soon.

addRule()

Adds a new rule to the stylesheet given the selector to which the style applies and the style block to apply to the matching elements.

This differs from insertRule(), which takes the textual representation of the entire rule as a single string.

removeRule()
Functionally identical to deleteRule(); removes the rule at the specified index from the stylesheet's rule list.

Notes

In some browsers, if a stylesheet is loaded from a different domain, accessing cssRules results in SecurityError.

A stylesheet is associated with at most one Document, which it applies to (unless disabled). A list of CSSStyleSheet objects for a given document can be obtained using the document.styleSheets property. A specific style sheet can also be accessed from its owner object (Node or CSSImportRule), if any.

A CSSStyleSheet object is created and inserted into the document's Document.styleSheets list automatically by the browser, when a stylesheet is loaded for a document. As the stylesheet list cannot be modified directly, there's no useful way to create a new CSSStyleSheet object manually (although Constructable Stylesheet Objects is coming to the web platform soon and is already supported in Blink). To create a new stylesheet, insert a <style> or <link> element into the document.

A (possibly incomplete) list of ways a stylesheet can be associated with a document follows:

Reason for the style sheet to be associated with the documentAppears in document.
styleSheets
list
Getting the owner element/rule given the style sheet objectThe interface for the owner objectGetting the CSSStyleSheet object from the owner
<style> and <link> elements in the documentYes.ownerNodeHTMLLinkElement,
HTMLStyleElement,
or SVGStyleElement
.sheet
CSS @import rule in other style sheets applied to the documentYes.ownerRuleCSSImportRule.styleSheet
<?xml-stylesheet ?> processing instruction in the (non-HTML) documentYes.ownerNodeProcessingInstruction.sheet
HTTP Link HeaderYesN/AN/AN/A
User agent (default) style sheetsNoN/AN/AN/A

Specifications

SpecificationStatusComment
CSS Object Model (CSSOM)
The definition of 'CSSStyleSheet' in that specification.
Working Draft
Document Object Model (DOM) Level 2 Style Specification
The definition of 'CSSStyleSheet' in that specification.
ObsoleteInitial definition

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:129 次

字数:11460

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文