标题很好地概括了它。
外部样式表有以下代码:
td.EvenRow a {
display: none !important;
}
我试过使用:
element.style.display = "inline";
and
element.style.display = "inline !important";
但两者都不奏效。是否可以使用javascript重写!important样式?
这是一个油猴扩展,如果这使差异。
标题很好地概括了它。
外部样式表有以下代码:
td.EvenRow a {
display: none !important;
}
我试过使用:
element.style.display = "inline";
and
element.style.display = "inline !important";
但两者都不奏效。是否可以使用javascript重写!important样式?
这是一个油猴扩展,如果这使差异。
当前回答
下面是使用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
其他回答
这样我们就有了从CSS中删除属性值的另一种可能性。
比如在js中使用replace方法。但是你必须确切地知道样式的ID,或者你可以写一个for循环来检测(计数页面上的样式,然后检查是否有任何“包含”或“匹配”一个!重要的值。你也可以计算它们包含了多少,或者只是简单地写一个全局的[regexp: /str/gi]替换方法)
我的很简单,但我附加了一个jsBin,例如:
https://jsbin.com/geqodeg/edit?html,css,js,output
首先,我在CSS中将主体背景设置为黄色!重要的是,然后我用JS重写为深粉色。
如果您所做的只是向页面添加css,那么我建议您使用Stylish插件,并编写用户样式而不是用户脚本,因为用户样式更有效和合适。
有关如何创建用户样式的信息,请参阅此页
https://developer.mozilla.org/en-US/docs/Web/CSS/initial
在css3中使用initial属性
<p style="color:red!important">
this text is red
<em style="color:initial">
this text is in the initial color (e.g. black)
</em>
this is red again
</p>
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)
如果你想在DOM元素样式属性中更新/添加单个样式,你可以使用这个函数:
function setCssTextStyle(el, style, value) {
var result = el.style.cssText.match(new RegExp("(?:[;\\s]|^)(" +
style.replace("-", "\\-") + "\\s*:(.*?)(;|$))")),
idx;
if (result) {
idx = result.index + result[0].indexOf(result[1]);
el.style.cssText = el.style.cssText.substring(0, idx) +
style + ": " + value + ";" +
el.style.cssText.substring(idx + result[1].length);
} else {
el.style.cssText += " " + style + ": " + value + ";";
}
}
风格。所有主流浏览器都支持cssText。
用例示例:
var elem = document.getElementById("elementId");
setCssTextStyle(elem, "margin-top", "10px !important");
这里是演示的链接