2025-03-23 07:00:00

重要的样式

标题很好地概括了它。

外部样式表有以下代码:

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

我试过使用:

element.style.display = "inline";

and

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

但两者都不奏效。是否可以使用javascript重写!important样式?

这是一个油猴扩展,如果这使差异。


当前回答

如果您所做的只是向页面添加css,那么我建议您使用Stylish插件,并编写用户样式而不是用户脚本,因为用户样式更有效和合适。

有关如何创建用户样式的信息,请参阅此页

其他回答

https://jsfiddle.net/xk6Ut/256/

在JavaScript中覆盖CSS类的一个选项是为样式元素使用ID,这样我们就可以更新CSS类

function writeStyles(styleName, cssText) {
    var styleElement = document.getElementById(styleName);
    if (styleElement) document.getElementsByTagName('head')[0].removeChild(
        styleElement);
    styleElement = document.createElement('style');
    styleElement.type = 'text/css';
    styleElement.id = styleName;
    styleElement.innerHTML = cssText;
    document.getElementsByTagName('head')[0].appendChild(styleElement);
}

..

   var cssText = '.testDIV{ height:' + height + 'px !important; }';
    writeStyles('styles_js', cssText)

我相信做到这一点的唯一方法是添加样式作为一个新的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后缀)将覆盖之前设置的其他样式。如果你不使用后缀,那么一定要考虑到“特异性”这样的概念。

基于@Premasagar的精彩回答;如果你不想删除所有其他的内联样式,可以使用这个

//accepts the hyphenated versions (i.e. not 'cssFloat')
addStyle(element, property, value, important) {
    //remove previously defined property
    if (element.style.setProperty)
        element.style.setProperty(property, '');
    else
        element.style.setAttribute(property, '');

    //insert the new style with all the old rules
    element.setAttribute('style', element.style.cssText +
        property + ':' + value + ((important) ? ' !important' : '') + ';');
}

不能使用removeProperty(),因为它不会删除Chrome中的重要规则。 不能使用元素。style[property] = "因为它在FireFox中只接受camelCase。

下面是使用jquery为style属性设置重要参数的代码片段。

$.fn.setFixedStyle = function(styles){
    var s = $(this).attr("style");
    s = "{"+s.replace(/;/g,",").replace(/'|"/g,"");
    s = s.substring(0,s.length-1)+"}";
    s = s.replace(/,/g,"\",\"").replace(/{/g,"{\"").replace(/}/g,"\"}").replace(/:/g,"\":\"");
    var stOb = JSON.parse(s),st;
    if(!styles){
     $.each(stOb,function(k,v){
      stOb[k] +=" !important";
     });
    }
    else{
     $.each(styles,function(k,v){
      if(v.length>0){
        stOb[k] = v+" !important";
      }else{
        stOb[k] += " !important";  
      }
     });
    }
    var ns = JSON.stringify(stOb);
    $(this).attr("style",ns.replace(/"|{|}/g,"").replace(/,/g,";"));
};

用法非常简单。只需传递一个包含您想设置为重要的所有属性的对象。

$("#i1").setFixedStyle({"width":"50px","height":""});

还有另外两个选择。

1.将重要的参数添加到已经呈现的样式属性中,传递空字符串。

2.为所有属性添加重要的参数,不要传递任何东西。它将把所有属性设置为重要属性。

这是它的现场表演。http://codepen.io/agaase/pen/nkvjr

而不是注入样式,如果你通过java脚本注入一个类(例如:'show'),它将工作。但这里你需要像下面这样的css。添加的类CSS规则应该在原始规则的下面。

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

td.EvenRow a.show{
  display: block !important;
}