如何将样式注入 IE8?
// create a style element
$("#main").html('<style id="custom_persona_css"></style>');
$("#custom_persona_css").append('#abc{ color:#000000; }');
如您所知,这在 IE8 中不起作用!
我怎样才能使它在 IE8 中运行?
将会出现错误:“意外调用方法或属性访问”,因为 IE8 无法将 html 识别为有效(abc 部分)
// 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!
How can I make it work in 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 MSIE 中设置与
http://jsfiddle.net/doktormolle/BLJUv/
更多信息:http://msdn.microsoft.com/en -us/library/ie/ms533698%28v=vs.85%29.aspx
In MSIE set the cssText-property of the styleSheet-object related to the
<style/>
-element:http://jsfiddle.net/doktormolle/BLJUv/
More Info: http://msdn.microsoft.com/en-us/library/ie/ms533698%28v=vs.85%29.aspx
我同意 jmort253 的观点,也许直接修改样式属性,或者加载 css 文件是最好的。然后,您可以使用更高效的 addClass 和 removeClass 方法。话虽这么说,样式元素应该位于头部(当然它们在正文中工作,但我记得没有
官方
支持)。所以你可以为此目的做类似的事情。http://jsfiddle.net/TCUxx/1
已更新 - 由于某种原因这不起作用。我不知道为什么。适用于 IE9。
I agree with jmort253 that perhaps directly modifying style attributes, or loading a css file is best. You can then use the more efficient addClass and removeClass methods. That being said, style elements should be in the head (they work in body of course, but aren't
officially
supported as I recall). So you can do something like this for that purpose.http://jsfiddle.net/TCUxx/1
Updated - for some reason this doesn't work. I don't know why. Works in IE9.
您正在使用 jQuery,并且 jQuery 有一个巨大的方法库,用于动态更改元素的样式:
上面只是使用 jQuery 影响选择器所标识的一个元素或一组元素的样式的两个示例。
因此,要更改 id="abc" 元素的颜色,可以使用以下方法:
此外,如果您要为一个元素或一组元素设置主题,您可以创建一个 style.css 文件,概述不同 className 的样式。然后,您可以通过向元素添加或删除 className 来简单地应用样式,如下所示:
style.css:
index.html script:
jQuery CSS 方法和属性类别 更详细地介绍了哪些属性可用。这不仅可以解决您的 IE8 问题,而且也适用于所有其他主要浏览器。
You're using jQuery, and jQuery has an immense library of methods for dynamically changing the style of elements:
Above are just two examples of usage of jQuery to affect the style of an element or group of elements identified by the selector.
Thus, to change the color of the element with id="abc", the following would work:
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. Then you can simply apply the style by adding or removing the className to the element(s) like so:
style.css:
index.html script:
jQuery CSS category of methods and properties goes into much more detail in terms of what properties are available. Not only will this solve your IE8 issue, but this will work in all of the other major browsers as well.
这有效吗:
或者
Does this work:
Or