如何删除添加了.css()函数的样式?

本文翻译自:How can I remove a style added with .css() function?

I'm changing CSS with jQuery and I wish to remove the styling I'm adding based on the input value: 我正在用jQuery更改CSS,我希望根据输入值删除我添加的样式:

if(color != '000000') $("body").css("background-color", color); else // remove style ?

How can I do this? 我怎样才能做到这一点? Note that the line above runs whenever a color is selected using a color picker (ie. when mouse moves over a color wheel). 请注意,只要使用颜色选择器选择颜色(即鼠标在色轮上移动时),就会运行上面的一行。

2nd note: I can't do this with css("background-color", "none") because it will remove the default styling from the css files. 第二个注意事项:我无法用css("background-color", "none")执行此操作,因为它将从css文件中删除默认样式。 I just want to remove the background-color inline style added by jQuery. 我只想删除jQuery添加的background-color内联样式。


#1楼

参考:https://stackoom.com/question/GwAb/如何删除添加了-css-函数的样式


#2楼

How about something like: 怎么样的:

var myCss = $(element).attr('css');
myCss = myCss.replace('background-color: '+$(element).css('background-color')+';', '');
if(myCss == '') {
  $(element).removeAttr('css');
} else {
  $(element).attr('css', myCss);
}

#3楼

There are several ways to remove a CSS property using jQuery: 有几种方法可以使用jQuery删除CSS属性:

1. Setting the CSS property to its default (initial) value 1.将CSS属性设置为其默认(初始)值

.css("background-color", "transparent")

See the initial value for the CSS property at MDN . 查看MDN上CSS属性初始值 Here the default value is transparent . 这里的默认值是transparent You can also use inherit for several CSS properties to inherite the attribute from its parent. 您还可以对几个CSS属性使用inherit来从其父级继承该属性。 In CSS3/CSS4, you may also use initial , revert or unset but these keywords may have limited browser support. 在CSS3 / CSS4中,您也可以使用initialrevertunset但这些关键字可能具有有限的浏览器支持。

2. Removing the CSS property 2.删除CSS属性

An empty string removes the CSS property, ie 空字符串删除CSS属性,即

扫描二维码关注公众号,回复: 10620619 查看本文章
.css("background-color","")

But beware, as specified in jQuery .css() documentation , this removes the property but it has compatibilty issues with IE8 for certain CSS shorthand properties, including background . 但请注意,正如jQuery .css()文档中所指定的,这会删除该属性,但它与IE8的某些CSS速记属性(包括背景)存在兼容性问题。

Setting the value of a style property to an empty string — eg $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. 将style属性的值设置为空字符串 - 例如$('#mydiv')。css('color','') - 如果已经直接应用了元素,则从元素中删除该属性,无论是HTML样式属性,通过jQuery的.css()方法,或通过样式属性的直接DOM操作。 It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element. 但是,它不会删除样式表或元素中使用CSS规则应用的样式。 Warning: one notable exception is that, for IE 8 and below, removing a shorthand property such as border or background will remove that style entirely from the element, regardless of what is set in a stylesheet or element . 警告: 一个值得注意的例外是,对于IE 8及更低版本,删除边框或背景等速记属性将完全从元素中删除该样式,无论样式表或元素中设置了什么

3. Removing the whole style of the element 3.删除元素的整个样式

.removeAttr("style")

#4楼

If you use CSS style, you can use: 如果使用CSS样式,则可以使用:

$("#element").css("background-color","none"); 

and then replace with: 然后替换为:

$("#element").css("background-color", color);

If you don't use CSS style and you have attribute in HTML element, you can use: 如果您不使用CSS样式并且HTML元素中有属性,则可以使用:

$("#element").attr("style.background-color",color);

#5楼

Use my Plugin : 使用我的插件:

$.fn.removeCss=function(all){
        if(all===true){
            $(this).removeAttr('class');
        }
        return $(this).removeAttr('style')
    }

For your case ,Use it as following : 对于您的情况,请按以下方式使用它:

$(<mySelector>).removeCss();

or 要么

$(<mySelector>).removeCss(false);

if you want to remove also CSS defined in its classes : 如果你想删除其类中定义的CSS:

$(<mySelector>).removeCss(true);

#6楼

either of these jQuery functions should work: 这些jQuery函数中的任何一个都应该工作:

$("#element").removeAttr("style");
$("#element").removeAttr("background-color") 
发布了0 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/p15097962069/article/details/105384640
今日推荐