首页 > 解决方案 > 我想使用位置功能向#cart-total-remove 添加一个类我尝试了很多方法但没有奏效

问题描述

这是购物车成功功能

success: function(json) {
                // Need to set timeout otherwise it wont update the total
                setTimeout(function () {
                    $('#cart > button').html('<span id="cart-total-remove"><img src="http://localhost/nerv/image/catalog/icons/bag.png"/> ' + json['total'] + '</span>');
                }, 100);

                if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
                    location = 'index.php?route=checkout/cart';
                } else {
                    $('#cart > ul').load('index.php?route=common/cart/info ul li');
                }
            },

我尝试添加类更改删除以稍后对其进行样式设置

$(document).ready(function() {
    
    if ( window.location.href == 'http://localhost/nerv/index.php?route=common/home' ){
        $("#cart #cart-total-remove img").removeClass("change-remove");
    } else {
        $("#cart #cart-total-remove img").addClass("change-remove");
        console.log(window.location.href);
    }

});

这是我的 CSS 代码

#cart #cart-total-remove img {
    filter: invert(1);
}
#cart #cart-total-remove img.change-remove{
    filter: none;
}

任何帮助,将不胜感激

标签: javascripthtmlcssjsonajax

解决方案


在这里检查你的两个条件是否有效

$(document).ready(function() {
    
    if ( window.location.href == 'http://localhost/nerv/index.php?route=common/home' ){
        $("#cart-total-remove img").removeClass("change-remove");
    } else {
        $("#cart-total-remove img").addClass("change-remove");
        console.log(window.location.href);
    }

});

推荐阅读