重写!重要的风格 [英] Overriding !important style

查看:152
本文介绍了重写!重要的风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



外部样式表具有以下代码:

  td.EvenRow a {
display:none!important;
}

我已尝试使用:


$ b b

  element.style.display =inline; 

  element.style.display =inline!important; 

但都不工作。使用javascript可以覆盖!重要的风格。



这是一个greasemonkey扩展,如果这有什么区别。

解决方案

我相信唯一的方法来添加样式作为一个新的CSS声明与'!重要'后缀。最简单的方法是将一个新的< style>元素到文档头部:

  function addNewStyle(newStyle){
var styleElement = document.getElementById('styles_js ');
if(!styleElement){
styleElement = document.createElement('style');
styleElement.type ='text / css';
styleElement.id ='styles_js';
document.getElementsByTagName('head')[0] .appendChild(styleElement);
}
styleElement.appendChild(document.createTextNode(newStyle));
}

addNewStyle('td.EvenRow a {display:inline!important;}')

使用上述方法添加的规则将(如果使用!important后缀)覆盖其他以前设置的样式。如果您不使用后缀,请务必使用具体性'。


Title pretty much sums it up.

The external style sheet has the following code:

td.EvenRow a{
  display: none !important;
}

I have tried using:

element.style.display = "inline";

and

element.style.display = "inline !important";

but neither works. Is it possible to override an !important style using javascript.

This is for a greasemonkey extension, if that makes a difference.

解决方案

I believe the only way to do this it to add the style as a new CSS declaration with the '!important' suffix. The easiest way to do this is to append a new <style> element to the head of document:

function addNewStyle(newStyle) {
    var styleElement = document.getElementById('styles_js');
    if (!styleElement) {
        styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.id = 'styles_js';
        document.getElementsByTagName('head')[0].appendChild(styleElement);
    }
    styleElement.appendChild(document.createTextNode(newStyle));
}

addNewStyle('td.EvenRow a {display:inline !important;}')

The rules added with the above method will (if you use the !important suffix) override other previously set styling. If you're not using the suffix then make sure to take concepts like 'specificity' into account.

这篇关于重写!重要的风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆