I used to give a useful function in one blog post , and it has been widely used in several of my blog posts. I have been reading a lot recently, so I wrote this blog post to sort out the new things I learned.
There is no doubt that based on the principle of separation of performance and structure, directly importing a new style sheet is the best choice, but in some cases it will not work. For example, if we make a draggable DIV, from the perspective of setting styles, it is Position it absolutely to prevent it from affecting the original document flow, and then change its top and left values little by little to achieve the effect of movement. Since dragging has a time concept, 24 frames per second, it is impossible to include everything in the style sheet. Therefore, it is very necessary to dynamically generate style rules and quickly modify style rules. W3C has done a lot of work for this. In DOM2.0, many interfaces have been expanded.
To take a step back, the separation of performance and structure is not limited to importing style sheets. You know, there are three types of styles, external styles, internal styles, and inline styles.
The newly added interfaces are mainly concentrated in external styles - I say interfaces because the corresponding implementations are provided by the browser. Arrogant guys like IE6 never ignore their existence.
In the W3C model, the link tag and style tag of type "text/css" both represent a CSSStyleSheet object. We can obtain all CSSStyleSheet objects in the current page through document.styleSheets, but this is a collection, not a simple one. array. Each CSSStyleSheet object has the following properties,
The style rule object (CSSStyleRule object) was developed by W3C in order to set the style in more detail. For example, the following thing corresponds to a style rule object:
button[type] {
padding:4px 10px 4px 7px;
line-height:17px;
}
The style rule object has the following main attributes: type, cssText, parentStyleSheet, parentRule.
Type is somewhat similar to nodeType, which subdivides style rules. It uses an integer to represent its type. The specific situation is as follows
Needless to say, cssText is a very useful attribute that directly converts strings into style rules, ignoring the differences in style attributes of each browser, such as cssFloat and styleFloat.
parentStyleSheet and parentRule are both for @import. However, @import has problems under IE, so I basically don’t use it.