我正在用jQuery改变CSS,我希望删除我根据输入值添加的样式:

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

我该怎么做呢? 请注意,每当使用颜色选择器选择颜色时,上面的行就会运行。当鼠标移动到一个色轮上时)。

第二注意:我不能这样做与css("background-color", "none"),因为它将删除默认样式从css文件。 我只是想删除jQuery添加的背景色内联样式。


当前回答

只是用:

$('.tag-class').removeAttr('style');

or

$('#tag-id').removeAttr('style');

其他回答

将属性更改为空字符串似乎可以完成工作:

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

使用我的插件:

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

对于你的情况,使用它如下:

$(<mySelector>).removeCss();

or

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

如果你想删除定义在类中的CSS:

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

这两个jQuery函数都可以工作:

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

你可以使用

removeAttr('style');

这个也可以!!

$elem.attr('style','');