如何在 JQuery 中删除 css 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9405689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove css property in JQuery
提问by SooIn Nam
if(prev_clicked)
{
$("#accordion li a.category").css('background-image', 'url("img/off_all_channel.png")');
$("#accordion li a.comment").css('background-image', 'url("img/on_all_online.png")');
$(".icha0").removeProperty("background-color");
$(".icha0").removeProperty("opacity");
}
else
{
$(".icha0").css("background-color","#D5D5D2");
$(".icha0").css("opacity","0.70");
}
I am trying to remove two css properties that I added but It seems not working. Any though?
我正在尝试删除我添加的两个 css 属性,但它似乎不起作用。有吗?
回答by Nevin
You can remove them by:
您可以通过以下方式删除它们:
$(".icha0").css({ 'background-color' : '', 'opacity' : '' });
回答by Mahmoud Gamal
You can use .css()
to remove css property as well, like this:
您也可以使用.css()
删除 css 属性,如下所示:
$(".icha0").css("background-color","");
$(".icha0").css("opacity","");
As mentioned in the jquery documentation:
如 jquery 文档中所述:
Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied,
将样式属性的值设置为空字符串 - 例如 $('#mydiv').css('color', '') - 如果该属性已被直接应用,则会从元素中删除该属性,
回答by 3Gee
To remove the in-line CSS property use:
要删除内联 CSS 属性,请使用:
$('.className').css({propertyName: ''});
To remove the whole in-line style of an element use:
要删除元素的整个内联样式,请使用:
$('.className').removeAttr('style');
I've also found this suggestion to remove the CSS property from styles (separate file) use:
我还发现了从样式(单独文件)使用中删除 CSS 属性的建议:
$('.className').style.propertyName = '';
$('.className').style.propertyName = '';
BUTI couldn't get it to work at all, so I'm putting it here just FYI.
但是我根本无法让它工作,所以我把它放在这里仅供参考。
回答by Jivings
Either you can set the properties back to blank:
您可以将属性设置回空白:
$(".icha0").css("background-color","");
$(".icha0").css("background-color","");
Or you can change the code to use classes defined in a CSS file:
或者您可以更改代码以使用 CSS 文件中定义的类:
$(".icha0").addClass('properties');
$(".icha0").removeClass('properties');
回答by NaxBuda
I was having this problem but I have not seen any solution here that satisfies the OP. Most of the solutions suggest getting rid of the entire style attribute which is not worth it.
我遇到了这个问题,但我在这里没有看到任何满足 OP 的解决方案。大多数解决方案建议摆脱整个不值得的样式属性。
I used jQuery's propmethod.
我使用了 jQuery 的prop方法。
var styleObject = $('my-selector').prop('style');
styleObject.removeProperty('background-color');
回答by Rajeev
We have two ways, either you can directly remove the applied CSS style class which is applied to DOM element or remove the applied CSS style from element
我们有两种方法,一种是直接移除应用于 DOM 元素的已应用 CSS 样式类,另一种是从元素中移除已应用的 CSS 样式
//Remove the class associated with element
$('#ID').removeClass("cssClass");
//Remove the CSS style from DOM element
$('p').css({"color":""});
回答by Andrew
Alternate less than ideal option, but might workaround various problems. Add a new class to the element with value inherit:
替代不太理想的选项,但可能会解决各种问题。向具有值inherit的元素添加一个新类:
css
css
.clearCSS {
background-color: inherit !important;
}
jquery
查询
$(".icha0").addClass('clearCSS');
回答by Jortega
This is literally a copy paste from this answer, and I use it to clear CSS style that I added with jQuery in a previously executed function.
这实际上是这个答案的复制粘贴,我用它来清除我在先前执行的函数中使用 jQuery 添加的 CSS 样式。
To remove the in-line CSS property use:
要删除内联 CSS 属性,请使用:
$('.className').css({propertyName: ''});
To remove the whole in-line style of an element use:
要删除元素的整个内联样式,请使用:
$('.className').removeAttr('style');
OR by ID:
或通过 ID:
$('#idName').removeAttr('style');
回答by Azzabi Haythem
if you need to removeand not updatea specific property you can simply use removePropand not removeProperty:
如果您需要删除而不是更新特定属性,您可以简单地使用 removeProp而不是removeProperty:
$(".icha0").removeProp("background-color");
$(".icha0").removeProp("opacity");
回答by Kawsik Roy
To remove all CSS property in JQuery the following code can be use:
要删除 JQuery 中的所有 CSS 属性,可以使用以下代码:
$(".selector").removeClass().removeAttr('style');
Thanks.
谢谢。