简体   繁体   English

使内联css比外部!important重要吗?

[英]Making an inline css to be more powerful than an external !important?

I have a slider script and a template external stylesheet, both I MUST use. 我必须使用滑台脚本和模板外部样式表。 I was instructed to just use a custom.css to override the template external stylesheet. 指示我仅使用custom.css覆盖模板外部样式表。

My problem is, the slider css in the template has an !important. 我的问题是,模板中的滑块css具有!important。 The slider script calculates the width and height of the images and inserts it through inline css. 滑块脚本计算图像的宽度和高度,并将其插入到内联css中。 I want the inline css to take more power in the cascade rather than the !important in the template stylesheet. 我希望内联css在级联中比在模板样式表中使用!important更有能力。

For example: 例如:

In html doc, after it load the slider script: 在html doc中,加载滑块脚本后:

<img src="slide1.jpg" style="width: 980.2545515px;" />

In template stylesheet: 在模板样式表中:

img {
width: 500px !important;
}

I tried using jquery such as: 我尝试使用jQuery,例如:

$("img").css("width","500px");

However, it doesn't override anything. 但是,它不会覆盖任何内容。 Note that I need the automatically generated width to override anything. 请注意,我需要自动生成的宽度来覆盖所有内容。 But I can't seem to find a way to override the width: 500px !important - width: auto/width: inherit doesn't work neither. 但是我似乎找不到一种方法来覆盖宽度:500px!important-width:auto / width:Inheritant都不起作用。

Any solutions? 有什么办法吗?

You can't really do it using the .css() method, but you can do it using the .attr() method, however this would overwrite any other inline styles you already have. 您实际上不能使用.css()方法来执行此操作,但是可以使用.attr()方法来执行此操作,但是这将覆盖您已经拥有的任何其他内联样式。

$('img').attr('style', 'width:500px !important;')

Here's a demo: http://jsfiddle.net/AGuvg/ 这是一个演示: http : //jsfiddle.net/AGuvg/

本身没有jquery,但是您可以使用setProperty如下所示:

$('img')[0].style.setProperty( 'width', '500px', 'important' );

You could generate an extra stylesheet dynamically. 您可以动态生成一个额外的样式表。 This sample works with Chrome but I have no idea of how it behaves in other browsers : http://jsfiddle.net/mj2Um/ . 该示例可与Chrome一起使用,但我不知道它在其他浏览器中的行为: http : //jsfiddle.net/mj2Um/

var stylesheet;
function setWidth(value) {
    stylesheet && stylesheet.remove();
    stylesheet = $('<style>img{width:' + value + 'px !important;}</style>');
    stylesheet.appendTo('body');
}
setWidth(150);

If you need the dynamically generated width to be the one applied, the only way is to add !important to that in the inline style. 如果您需要动态生成的宽度是应用的宽度,则唯一的方法是在内联样式中添加!important You could do it like: 您可以这样做:

var styleAttr = $(".slider-image").attr("style");

styleAttr += " !important";  
/* ^^ assumes styleAttr is just 'width: 980.2545515px' and so 
becomes 'width:   980.2545515px !important' - if the style attribute is more 
complex than this you'd need a bit more sophisticated parsing of it */

$(".slider-image").attr('style', styleAttr);

This will force the dynamic width to override the stylesheet !important one. 这将强制动态宽度覆盖样式表!important。

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

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