我很难应用这样的风格!重要的我尝试过:
$("#elem").css("width", "100px !important");
这没有任何作用;不应用任何宽度样式。有没有类似jQuery的方法来应用这样的样式而不必覆盖cssText(这意味着我需要先解析它,等等)?
编辑:我应该补充一下,我有一个带有!我试图用!重要的样式内联,因此使用.width()等不会起作用,因为它会被我的外部重写!重要的风格。
此外,将覆盖上一个值的值是计算出来的,因此我不能简单地创建另一个外部样式。
我很难应用这样的风格!重要的我尝试过:
$("#elem").css("width", "100px !important");
这没有任何作用;不应用任何宽度样式。有没有类似jQuery的方法来应用这样的样式而不必覆盖cssText(这意味着我需要先解析它,等等)?
编辑:我应该补充一下,我有一个带有!我试图用!重要的样式内联,因此使用.width()等不会起作用,因为它会被我的外部重写!重要的风格。
此外,将覆盖上一个值的值是计算出来的,因此我不能简单地创建另一个外部样式。
当前回答
我想我找到了解决办法。我把它变成了一个新功能:
jQuery.style(名称、值、优先级);
您可以使用它像.css('name')一样使用.style('name`)获取值,使用.style()获取CSSStyleDeclaration,还可以设置值,并可以将优先级指定为“重要”。看看这个。
实例
var div = $('someDiv');
console.log(div.style('color'));
div.style('color', 'red');
console.log(div.style('color'));
div.style('color', 'blue', 'important');
console.log(div.style('color'));
console.log(div.style().getPropertyPriority('color'));
示例输出:
null
red
blue
important
功能
(function($) {
if ($.fn.style) {
return;
}
// Escape regex chars with \
var escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// For those who need them (< IE 9), add support for CSS functions
var isStyleFuncSupported = !!CSSStyleDeclaration.prototype.getPropertyValue;
if (!isStyleFuncSupported) {
CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
return this.getAttribute(a);
};
CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
this.setAttribute(styleName, value);
var priority = typeof priority != 'undefined' ? priority : '';
if (priority != '') {
// Add priority manually
var rule = new RegExp(escape(styleName) + '\\s*:\\s*' + escape(value) +
'(\\s*;)?', 'gmi');
this.cssText =
this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
}
};
CSSStyleDeclaration.prototype.removeProperty = function(a) {
return this.removeAttribute(a);
};
CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
var rule = new RegExp(escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?',
'gmi');
return rule.test(this.cssText) ? 'important' : '';
}
}
// The style function
$.fn.style = function(styleName, value, priority) {
// DOM node
var node = this.get(0);
// Ensure we have a DOM node
if (typeof node == 'undefined') {
return this;
}
// CSSStyleDeclaration
var style = this.get(0).style;
// Getter/Setter
if (typeof styleName != 'undefined') {
if (typeof value != 'undefined') {
// Set style property
priority = typeof priority != 'undefined' ? priority : '';
style.setProperty(styleName, value, priority);
return this;
} else {
// Get style property
return style.getPropertyValue(styleName);
}
} else {
// Get CSSStyleDeclaration
return style;
}
};
})(jQuery);
有关如何读取和设置CSS值的示例,请参阅本文。我的问题是我已经设定好了!我的CSS中的宽度对于避免与其他主题CSS冲突很重要,但我在jQuery中对宽度所做的任何更改都不会受到影响,因为它们将被添加到style属性中。
兼容性
对于使用setProperty函数设置优先级,本文表示支持IE9+和所有其他浏览器。我尝试过IE 8,但它失败了,这就是为什么我在功能中构建了对它的支持(见上文)。它可以在所有其他使用setProperty的浏览器上工作,但它需要我的自定义代码才能在<IE9中工作。
其他回答
我想我找到了解决办法。我把它变成了一个新功能:
jQuery.style(名称、值、优先级);
您可以使用它像.css('name')一样使用.style('name`)获取值,使用.style()获取CSSStyleDeclaration,还可以设置值,并可以将优先级指定为“重要”。看看这个。
实例
var div = $('someDiv');
console.log(div.style('color'));
div.style('color', 'red');
console.log(div.style('color'));
div.style('color', 'blue', 'important');
console.log(div.style('color'));
console.log(div.style().getPropertyPriority('color'));
示例输出:
null
red
blue
important
功能
(function($) {
if ($.fn.style) {
return;
}
// Escape regex chars with \
var escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// For those who need them (< IE 9), add support for CSS functions
var isStyleFuncSupported = !!CSSStyleDeclaration.prototype.getPropertyValue;
if (!isStyleFuncSupported) {
CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
return this.getAttribute(a);
};
CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
this.setAttribute(styleName, value);
var priority = typeof priority != 'undefined' ? priority : '';
if (priority != '') {
// Add priority manually
var rule = new RegExp(escape(styleName) + '\\s*:\\s*' + escape(value) +
'(\\s*;)?', 'gmi');
this.cssText =
this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
}
};
CSSStyleDeclaration.prototype.removeProperty = function(a) {
return this.removeAttribute(a);
};
CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
var rule = new RegExp(escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?',
'gmi');
return rule.test(this.cssText) ? 'important' : '';
}
}
// The style function
$.fn.style = function(styleName, value, priority) {
// DOM node
var node = this.get(0);
// Ensure we have a DOM node
if (typeof node == 'undefined') {
return this;
}
// CSSStyleDeclaration
var style = this.get(0).style;
// Getter/Setter
if (typeof styleName != 'undefined') {
if (typeof value != 'undefined') {
// Set style property
priority = typeof priority != 'undefined' ? priority : '';
style.setProperty(styleName, value, priority);
return this;
} else {
// Get style property
return style.getPropertyValue(styleName);
}
} else {
// Get CSSStyleDeclaration
return style;
}
};
})(jQuery);
有关如何读取和设置CSS值的示例,请参阅本文。我的问题是我已经设定好了!我的CSS中的宽度对于避免与其他主题CSS冲突很重要,但我在jQuery中对宽度所做的任何更改都不会受到影响,因为它们将被添加到style属性中。
兼容性
对于使用setProperty函数设置优先级,本文表示支持IE9+和所有其他浏览器。我尝试过IE 8,但它失败了,这就是为什么我在功能中构建了对它的支持(见上文)。它可以在所有其他使用setProperty的浏览器上工作,但它需要我的自定义代码才能在<IE9中工作。
我想你没有添加就尝试了!重要的
内联CSS(这是JavaScript添加样式的方式)覆盖样式表CSS。我很确定,即使样式表CSS规则有这种情况!重要的
另一个问题(可能是一个愚蠢的问题,但必须要问。):您正在尝试的元素是否显示:block;或显示:内联块;?
不知道你在CSS方面的专业知识。。。内联元素的行为并不总是如您所期望的那样。
当“事件”时,我尝试更改菜单项的文本颜色时遇到了同样的问题。当我遇到同样的问题时,我发现的最佳方法是:
第一步:在CSS中创建一个新的类,例如:
.colorw{ color: white !important;}
最后一步:使用addClass方法应用该类,如下所示:
$('.menu-item>a').addClass('colorw');
问题已解决。
此解决方案将保留所有计算的javascript,并将重要标记添加到元素中:可以这样做(例如,如果需要设置重要标记的宽度)
$('exampleDiv').css('width', '');
//This will remove the width of the item
var styles = $('exampleDiv').attr('style');
//This will contain all styles in your item
//ex: height:auto; display:block;
styles += 'width: 200px !important;'
//This will add the width to the previous styles
//ex: height:auto; display:block; width: 200px !important;
$('exampleDiv').attr('style', styles);
//This will add all previous styles to your item
我还发现某些元素或插件(如Bootstrap)有一些特殊的类情况,它们不能很好地使用!重要的或其他解决方法,如.addClass/.removeClass,因此您必须打开/关闭它们。
例如,如果您使用类似<table class=“table hover”>的方法,成功修改行颜色等元素的唯一方法是打开/关闭table hover类,如下所示
$(your_element)最近(“table”).tggleClass(“table hover”);
希望这项工作对某人有帮助!:)