你可以通過使用
document.createStyleSheet
。在google doctype wiki這表明你如何使用他們的library添加樣式您在IE中:
/**
* Installs the styles string into the window that contains opt_element. If
* opt_element is null, the main window is used.
* @param {String} stylesString The style string to install.
* @param {Element} opt_element Element who's parent document should have the
* styles installed.
* @return {Element} The style element created.
*/
goog.style.installStyles = function(stylesString, opt_element) {
var dh = goog.dom.getDomHelper(opt_element);
var styleSheet = null;
if (goog.userAgent.IE) {
styleSheet = dh.getDocument().createStyleSheet();
} else {
var head = dh.$$('head')[0];
// In opera documents are not guaranteed to have a head element, thus we
// have to make sure one exists before using it.
if (!head) {
var body = dh.$$('body')[0];
head = dh.createDom('head');
body.parentNode.insertBefore(head, body);
}
styleSheet = dh.createDom('style');
dh.appendChild(head, styleSheet);
}
goog.style.setStyles(styleSheet, stylesString);
return styleSheet;
};
/**
* Sets the content of a style element. The style element can be any valid
* style element. This element will have its content completely replaced by
* the new stylesString.
* @param {Element} element A stylesheet element as returned by installStyles
* @param {String} stylesString The new content of the stylesheet
*/
goog.style.setStyles = function(element, stylesString) {
if (goog.userAgent.IE) {
// Adding the selectors individually caused the browser to hang if the
// selector was invalid or there were CSS comments. Setting the cssText of
// the style node works fine and ignores CSS that IE doesn't understand
element.cssText = stylesString;
} else {
var propToSet = goog.userAgent.SAFARI ? 'innerText' : 'innerHTML';
element[propToSet] = stylesString;
}
};