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

IE 7中Ajax调用后的CSS样式消失

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

    在Ajax调用之后,我的样式没有被应用,这让我有了一个问题。我的样式不在页面的<head>部分,只有在初始页面加载时,IE才能识别它们。

    如果你知道其他解决这个问题的方法,把它们贴在这里。

    这更是一个参考,希望这能帮助一些人。

    2 回复  |  直到 12 年前
        1
  •  2
  •   roman m    15 年前

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

        2
  •  0
  •   Fraser Harris eschwartz    14 年前

    您还可以从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