简体   繁体   English

IE不适用于动态加载的CSS

[英]IE doesn't apply dynamically loaded CSS

It appears as though IE (older versions at least) does not apply CSS that is loaded dynamically. 似乎IE(至少旧版本)不应用动态加载的CSS。 This can be a pain point if you load a page containing CSS via ajax into a "lightbox" or "colorbox". 如果您通过ajax将包含CSS的页面加载到“lightbox”或“colorbox”中,这可能是一个痛点。

For example, say your HTML page has a div named "taco": 例如,假设您的HTML页面有一个名为“taco”的div:

<style>#taco {color:green;}</style>
<div id="taco">Hola Mundo!</div>

"Hola Mundo!" “Hola Mundo!” will be green since the CSS was included in the original HTML page. 由于CSS包含在原始HTML页面中,因此将为绿色。 Then some Javascript happens and appends this to "taco": 然后一些Javascript发生并将其附加到“taco”:

<style>#taco {color:green;}</style>
<div id="taco">
  Hola Mundo!
  <style>#burrito {color:red;}</style>
  <span id="burrito">mmmm burrito</span>
</div>

In all browsers except IE, burrito's font will be red. 在除IE之外的所有浏览器中,burrito的字体将为红色。

So is there a way to do this in IE? 那么有没有办法在IE中这样做? It seems as though there is not. 好像没有。

The style tag is only allowed in the head section. style标记仅允许在head部分中使用。 Placing it somewhere else is simply invalid and that has nothing to do with IE. 将它放在其他地方根本就是无效的,这与IE无关。

More information . 更多信息

By the way , to solve your problem if you can´t put the styles in a global style-sheet, you can use the 'style' attribute to modify elements: 顺便说一下 ,如果你不能将样式放在一个全局样式表中来解决你的问题,你可以使用'style'属性来修改元素:

<p style="...">

Or you can use an iframe but then you'd have to serve a whole page instead of just a few tags. 或者你可以使用iframe但是你必须提供整个页面而不是几个标签。

You might want to start using jQuery's .CSS methed for dynamic style changes like that. 您可能希望开始使用jQuery的.CSS methed进行动态样式更改。

$("#jane").css('color', '#0F0');

Or just plain jane Javascript: 或者只是简单的jane Javascript:

document.getElementById['sally'].style.color = '#0F0';

EDIT: 编辑:

Have your ajax inject this: 你的ajax注入了这个:

<div id="jane">        
    <div id="sally">Hi, I'm Sally!</div>
    <script>document.getElementById['sally'].style.color = '#0F0';</script>
</div>

Or Why not just inject elements with inline styles computed server side?: 或者为什么不直接使用内联样式计算服务器端注入元素?:

<div id="jane">        
    <div id="sally" style="color:#0F0">Hi, I'm Sally!</div>
</div>

If there is no way to do this, and you don't want to change your server-side code, here is a way for very simple style elements: 如果没有办法做到这一点,并且您不想更改服务器端代码,则可以使用以下非常简单的样式元素:

// In the callback function, let's assume you're using jQuery
success: function( data ) {

    // Create a dummy DOM element
    var el = document.createElement( 'div' );

    // Add the html received to this dummy element
    el.innerHTML = data;

    // so that you can select its html:
    var s = $( 'style', el ).text();

    // Delegate to another function, it's going to get messy otherwise
    addRules( s );
}

function addRules( s ) {
    // First, separate your strings for each line
    var lines = s.split( '\n' ),

    // Declare some temporary variables
        id,
        rule,
        rules;

    // Then, loop through each line to handle it
    $.each( lines, function() {
        id = $( this ).split( ' ' )[ 0 ];

        // Get the rules inside the brackets, thanks @Esailija
        rules = /\{\s*([^}]*?)\s*\}/.exec( $( this ) )[ 1 ];

        // Split the rules
        rules = rules.split( ';' );

        $.each( rules, function() {
            rule = $( this ).split( ':' );

            // Apply each rule to the id
            $( id ).css( $.trim( rule[ 0 ] ), $.trim( rule[ 1 ] ) );
        } );
    } );
}

So, yeah, basically I'm making a CSS parser. 所以,是的,基本上我正在制作一个CSS解析器。

This is a very basic parser however. 这是一个非常基本的解析器。

It will parse the following rules only: 它将仅解析以下规则:

#some-id { some: rule; another: rule; }
#other-id { some: rule; yet: another; rule: baby; }

If you load a linked stylesheet dynamically (via AJAX) into a webpage, IE < 8 does not even recognize the LINK tag. 如果将动态链接样式表(通过AJAX)加载到网页中,IE <8甚至无法识别LINK标记。

If you load a SCRIPT tag dynamically IE < 8 will not parse it. 如果动态加载SCRIPT标记,IE <8将不会解析它。

Jeron is correct, the only way to dynamically load HTML and have it styled is via iframe, but I am testing the idea of reflowing the container . Jeron是正确的,动态加载HTML并设置样式的唯一方法是通过iframe,但我正在测试回流容器的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM