如何通过javascript覆盖内联css? [英] How to override inline css through javascript?
问题描述
我们可以通过javascript覆盖内联css吗? 与IE6兼容。
can we override inline css through javascript? with IE6 compatibility.
我发现这个纯CSS解决方案,但不适用于IE。
I found this pure css solution but not works in IE.
http://nataliejost.com/override-inline-styles- from-the-stylesheet /
http://www.sitepoint.com/blogs/2009/05/27/override-inline-css/
<div class="block">
<span style="font-weight: bold; color: red;">Hello World</span>
</div>
我们可以使用此解决方案覆盖此内联样式
we can override this inline style with this solution
.block span[style]{
font-weight: normal !important;
color: #000 !important;
}
此解决方案适用于除IE6以外的所有主流浏览器。
推荐答案
当然你可以使用jQuery的css()方法: http://docs.jquery.com/CSS/css#namevalue
Of course you can by using jQuery's css() method : http://docs.jquery.com/CSS/css#namevalue
例如,如果您有以下HTML:
So if for example you have the following HTML:
<p style="color:red;">A colored text</p>
您可以通过在jQuery中执行以下操作来更改颜色:
You can change the color by doing the following in jQuery:
$("p").css("color","blue");
它将在IE6中工作。
这篇关于如何通过javascript覆盖内联css?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!