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");
}

我试图删除我添加的两个css属性,但似乎不工作。 尽管吗?


你可以设置属性为空:

$(“.icha0”).css(“background-color”,“”);

或者您可以更改代码使用CSS文件中定义的类:

$(".icha0").addClass('properties'); 
$(".icha0").removeClass('properties');

你可以通过以下方法移除它们:

$(".icha0").css({ 'background-color' : '', 'opacity' : '' });

你也可以使用.css()来删除css属性,就像这样:

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

正如jquery文档中提到的:

将样式属性的值设置为空字符串-例如$('#mydiv').css('color', ") -如果该属性已经被直接应用,则从元素中删除该属性,


删除CSS内嵌属性使用:

$('.className').css({propertyName: ''});

要删除元素的整个内联样式,请使用:

$('.className').removeAttr('style');

我还发现了这个建议,从样式(单独的文件)使用中删除CSS属性:

$('.className').style.propertyName='';

但是我根本不能让它工作,所以我把它放在这里只是供参考。


我们有两种方法,要么你可以直接删除应用到DOM元素的CSS样式类,要么从元素中删除应用的CSS样式

//Remove the class associated with element

$('#ID').removeClass("cssClass");

//Remove the CSS style from DOM element

$('p').css({"color":""});

备选方案,可以解决一些问题。

给元素添加一个新类,并赋予属性值inherit !important,例如:

css

.clearCSS {
    background-color: inherit !important;
}

jquery

$(".icha0").addClass('clearCSS'); 

这实际上是一个复制粘贴从这个答案,我用它来清除CSS样式,我添加与jQuery在以前执行的函数。

删除CSS内嵌属性使用:

$('.className').css({propertyName: ''});   

要删除元素的整个内联样式,请使用:

$('.className').removeAttr('style');

或通过ID:

$('#idName').removeAttr('style');

如果你需要删除而不是更新一个特定的属性,你可以简单地使用removeProp而不是removeProperty:

$(".icha0").removeProp("background-color");
$(".icha0").removeProp("opacity");

要删除JQuery中的所有CSS属性,可以使用以下代码:

    $(".selector").removeClass().removeAttr('style');

谢谢。


我有这个问题,但我没有看到任何解决方案,在这里满足op。大多数解决方案建议摆脱整个风格属性,这是不值得的。

我使用jQuery的道具方法。

var styleObject = $('my-selector').prop('style'); 

styleObject.removeProperty('background-color');

与jquery

  $('#ID').removeAttr("style")

withoyt jquery

document.getElementById("ID").removeAttribute("style")

<script>
        require(['jquery', 'domReady!'], function($) {
            if ($('#p_method_use_reward_points').prop('checked')) {
                $('.test').show();
                console.log('working');
            }else{
                $('.test').hide();
                console.log('not working');
            }
            $('#submit_reward').on('click', function (e){
                let getPoints = $('#custom_reward_set').val();
                let checkField = $('#custom_reward_set').prop("readonly");
                // alert(getPoints);
                if (!getPoints){
                    alert('PLease add reward points');
                }
                if (getPoints) {
                    $("#custom_reward_set").prop("readonly", !$("#custom_reward_set").attr('readonly'));
                    if ($('#custom_reward_set').is('[readonly]')) {
                        $("#custom_reward_set").css({
                            "background-color": "#EBEBE4",
                            "border": "1px solid #ABADB3", "cursor": "not-allowed"
                        });
                    }else{
                        $('#custom_reward_set').removeAttr('style');
                    }
                }
               e.preventDefault();

            })
        });
    </script>