Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当购物车为空时将类添加到div_Php_Css_Wordpress_Woocommerce_Cart - Fatal编程技术网

Php 当购物车为空时将类添加到div

Php 当购物车为空时将类添加到div,php,css,wordpress,woocommerce,cart,Php,Css,Wordpress,Woocommerce,Cart,我想向元素添加一个类(或直接设置样式),该元素仅在购物车为空时才包含购物车计数 下面是我目前正在使用的代码,它工作得很好。购物车计数很好,但颜色不会更新 header.php(在菜单中的购物车图标后添加购物车计数) style.css-当前css .counter { background-color: #ff0; border-radius: 20px; font-size: 10px; padding: 0 4px 0 4px; } .other-backg

我想向元素添加一个类(或直接设置样式),该元素仅在购物车为空时才包含购物车计数

下面是我目前正在使用的代码,它工作得很好。购物车计数很好,但颜色不会更新

header.php(在菜单中的购物车图标后添加购物车计数)

style.css-当前css

.counter {
    background-color: #ff0;
    border-radius: 20px;
    font-size: 10px;
    padding: 0 4px 0 4px;
}

.other-background-of-bubble{
    background-color:red!important;
}

您可以在文档就绪时调用此函数,也可以在ajax更新购物车时调用此函数

Javascript/jQuery:

function changecartbubble() {
    if ($('body').find('#cart-count').length > 0) {
        if (parseInt($('#cart-count').html()) > 0) {
            // cart has number > 0 so cart has items
            //change background class
            $('#cart-count').addClass('other-background-of-bubble');
        }
        else {
            // cart has no number so cart has no items
            //remove background class
            $('#cart-count').removeClass('other-background-of-bubble');
        }
    }
}
风格:

.other-background-of-bubble{
    background:red!important;
}

根据您的建议,我在functions.php中添加了一个wp_enque_脚本,该脚本使用您的代码调用js文件。js文件中的代码由jQuery(document).ready(函数($){//code/})包围;但我没有看到任何变化。关于如何实现的任何进一步建议?请确保在控制台中没有看到任何错误,并且在调用函数之前已成功加载jQuery。另外,请确保函数中的脚本顺序正确。phpI已经测试了上述javascript函数,并且工作正常。您在里面看到的输出是什么#在回显所有内容后的购物车计数?感谢您的跟进和帮助。我有点傻,所以我可能错过了什么。我已经用我添加的新代码更新了我原来的问题。控制台中没有错误,我看不到类被回响。购物车中项目的输出为1-如果它有助于查看实际情况,则当我运行函数changecartbubble()时,站点为我在控制台中看到错误;
.counter {
    background-color: #ff0;
    border-radius: 20px;
    font-size: 10px;
    padding: 0 4px 0 4px;
}

.other-background-of-bubble{
    background-color:red!important;
}
function changecartbubble() {
    if ($('body').find('#cart-count').length > 0) {
        if (parseInt($('#cart-count').html()) > 0) {
            // cart has number > 0 so cart has items
            //change background class
            $('#cart-count').addClass('other-background-of-bubble');
        }
        else {
            // cart has no number so cart has no items
            //remove background class
            $('#cart-count').removeClass('other-background-of-bubble');
        }
    }
}
.other-background-of-bubble{
    background:red!important;
}