2010-09-01 97 views
0

Internet Explorer中的CSS修改有點問題。當我在<head>中添加新的CSS樣式時,IE不會重新加載注入了新CSS的頁面。但是,當我更改元素的CSS屬性時,它的工作原理!此代碼完美適用於Firefox,所以我不明白爲什麼它在IE中不起作用。Internet Explorer 8上的DOM Javascript代碼:

你有什麼想法做這個頭部修改工作嗎?

if(document.createStyleSheet) { 
    document.createStyleSheet('http://www.xxxx.com/style.css'); 
} else { 
    newnode=document.createElement('link'); 
    newnode.id='newStyle'; 
    newnode.media="all"; 
    newnode.rel="stylesheet"; 
    newnode.type="text/css"; 

    newnode.href='http://www.xxxx.com/style.css'; 
    document.getElementsByTagName('head')[0].readOnly=false; 
    document.getElementsByTagName('head')[0].appendChild(newnode); 
} 

回答

0

你可以通過使用

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; 
    } 
}; 
0

Quirksmode.org asserts的createStyleSheet()方法是唯一的IE瀏覽器,它匹配我的經驗。下面的代碼在IE 9,但不是FF 4,鉻11,或Safari 5

<html> 
    <head> 
     <script type="text/javascript"> 
      if(document.createStyleSheet()) 
      { 
       document.createStyleSheet("external.css"); 
      } 
     </script> 
    </head> 
    <body>  
     <p id="important">This is an important paragraph.</p> 
    </body> 
</html> 

凡external.css包含以下規則:

#important 
{ 
    font-family: Arial; 
} 

而且它同樣運作良好,無論我將劇本放在頭上,在段前放在體內,或放在段後放在體內。