简体   繁体   English

如何在IE8中注入样式?

[英]How do I inject styles into IE8?

// create a style element
$("#main").html('<style id="custom_persona_css"></style>'); 

$("#custom_persona_css").append('#abc{ color:#000000; }');

As you can tell, this does not work in IE8! 正如你所知,这在IE8中不起作用!

How can I make it work in IE8? 如何在IE8中使其工作?

There will be an error of : 'unexpected call to method or property access' because IE8 does not recognize the html as valid (abc part) 将出现以下错误:'意外调用方法或属性访问',因为IE8无法将html识别为有效(abc部分)

I agree with jmort253 that perhaps directly modifying style attributes, or loading a css file is best. 我同意jmort253可能直接修改样式属性,或者加载css文件是最好的。 You can then use the more efficient addClass and removeClass methods. 然后,您可以使用更高效的addClass和removeClass方法。 That being said, style elements should be in the head (they work in body of course, but aren't officially supported as I recall). 话虽这么说,风格元素应该在头部(当然它们在身体中工作,但我记得并没有得到officially支持)。 So you can do something like this for that purpose. 所以你可以为此目的做这样的事情。

http://jsfiddle.net/TCUxx/1 http://jsfiddle.net/TCUxx/1

$('head').append('<style type="text/css">#abc{ color:#ff0000; }</style>');

Updated - for some reason this doesn't work. 更新 - 由于某种原因,这不起作用。 I don't know why. 我不知道为什么。 Works in IE9. 适用于IE9。

var $styleElement = $('<style />', {
    type: 'text/css',
    text: '#abc{ color:#ff0000; }'
});

$('head').append($styleElement);

In MSIE set the cssText-property of the styleSheet-object related to the <style/> -element: 在MSIE中,设置与<style/> -element相关的styleSheet-object的cssText属性:

   $('<style id="custom_persona_css"></style>').appendTo('head'); 

   if($.browser.msie)
   {
    $("#custom_persona_css").prop('styleSheet').cssText='#abc{ color:#000000; }';
   }
   else
   {
    $("#custom_persona_css").append('#abc{ color:#000000; }');
   }

http://jsfiddle.net/doktormolle/BLJUv/ http://jsfiddle.net/doktormolle/BLJUv/

More Info: http://msdn.microsoft.com/en-us/library/ie/ms533698%28v=vs.85%29.aspx 更多信息: http//msdn.microsoft.com/en-us/library/ie/ms533698%28v=vs.85%29.aspx

You're using jQuery, and jQuery has an immense library of methods for dynamically changing the style of elements: 你正在使用jQuery,jQuery有一个巨大的方法库来动态改变元素的样式:

$(selector).css(propertyName, value);

$(selector).propertyName(value);

Above are just two examples of usage of jQuery to affect the style of an element or group of elements identified by the selector. 上面只是使用jQuery来影响由选择器标识的元素或元素组的样式的两个示例。

Thus, to change the color of the element with id="abc", the following would work: 因此,要更改id =“abc”的元素的颜色,以下将起作用:

$('#abc').css('color','#000000');

Additionally, if you're looking to theme an element or group of elements, you can create a style.css file outlining styles for different classNames. 此外,如果您要查看主题元素或元素组,您可以创建一个style.css文件,概述不同类名的样式。 Then you can simply apply the style by adding or removing the className to the element(s) like so: 然后,您可以通过向元素添加或删除className来简单地应用样式,如下所示:

style.css: style.css中:

.custom_persona {
    color:#000000;
}

.some_other_custom_style {
    color:red;
    background-color: #fff;
}

index.html script: index.html脚本:

$('#abc').addClass('custom_persona');

// OR

$('#abc').addClass('.some_other_custom_style');
$('#abc').removeClass('custom_persona');

jQuery CSS category of methods and properties goes into much more detail in terms of what properties are available. jQuery CSS类别的方法和属性在可用的属性方面更详细。 Not only will this solve your IE8 issue, but this will work in all of the other major browsers as well. 这不仅可以解决您的IE8问题,而且还适用于所有其他主流浏览器。

Does this work: 这有用吗:

var $style = $("#custom_persona_css");

$style.html(
    $style.html() + '#abc{ color:#000000; }'
);

Or 要么

$("#custom_persona_css")[0].innerHTML += '#abc{ color:#000000; }'

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

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