代码之家  ›  专栏  ›  技术社区  ›  roman m

IE7中Ajax调用后的CSS样式

  •  0
  • roman m  · 技术社区  · 17 年前

    如果你知道任何其他解决这个问题的方法,请在这里发布。

    2 回复  |  直到 14 年前
        1
  •  2
  •   roman m    17 年前

    在做了一些谷歌搜索后,我发现 moving my styles into the < HEAD> tag of the page fixes 问题。

        2
  •  0
  •   Fraser Harris eschwartz    16 年前

    您还可以从AJAX HTML中获取样式,并将其插入到头部。这是一些示例代码。已在IE8和Chrome中测试。

    function enable_embedded_styles(html) {
    // Grab style content, and create new style element for it
    // Works for first set of <style></style> tags in html
    // Tested in IE and Chrome
        if (typeof(html) === 'string') {
            var beg = html.indexOf('<style>'), 
                end = html.indexOf('</style>');
    
            if (beg !== -1 && end !== -1) {
                var style = html.substr(beg + 7, end - 7 - beg); // everything between style tags
                html = html.substr(end + 8); // everything after closing style tag
    
                s = document.createElement('style');
                s.setAttribute('type','text/css');
    
                // For IE
                if (s.styleSheet) {
                    s.styleSheet.cssText = style;
                } // endif
    
                // For every other browser
                else {
                    s.appendChild(document.createTextNode(style));
                } // endelse
    
                // Append stylesheet to head
                document.getElementsByTagName('head')[0].appendChild(s);
            } // endif
        } // endif
    
        return html;
    } // endfunction