【问题标题】:How to add a class number to a div with same number as cookie如何将班级编号添加到与 cookie 相同编号的 div
【发布时间】:2014-04-03 21:39:38
【问题描述】:

饼干:

$(document).ready(function(){

    //hide all divs just for the purpose of this example, 
    //you should have the divs hidden with your css

    //$('.picture.1').hide();
    //check if the cookie is already setted
    if ($.cookie("currentDiv")===undefined){
            $.cookie("currentDiv",1);
    } else{ //else..well 
            //get and increment value, let's suppose we have just 8 divs     

            var numValue = parseInt($.cookie("currentDiv"));
            numValue = numValue + 1;
            if (numValue>8){
            //restart to 1
                $.cookie("currentDiv",1);
            }
            else{ //no issues, assign the incremented value
                $.cookie("currentDiv",numValue.toString());
            }

            //show the div
            $('.picture.'+$.cookie("currentDiv")).show();
            $('.current'+$.addClass(.cookie("currentDiv")); <--
    }
});

分区:

<div class="current"></div>

然后我设置一个从 1 到 8 的样式,所以如果它是“当前 1”,它会显示数字 1 cookie 的工作原理:如果尚未设置 cookie,它会设置一个 cookie,并将其称为“currentDiv”,并​​给它一个从 1 到 8 的数字,如果访问过的 cookie 大于 8,则刷新 cookie。 但是如何使用这个 cookie/jquery 将该类添加到 div 它需要添加一个类,因为它会阻止浏览器数据,而且我有一个数字从 0 到 9 的 svg

谢谢,清酒

【问题讨论】:

标签: javascript jquery html css cookies


【解决方案1】:

要添加类,你应该使用类似这样的东西

$('.current').addClass($.cookie("currentDiv"));

但我建议你添加一些名称的类,例如

$('.current').addClass("cookie"+$.cookie("currentDiv"));

不以数字开头的名称被认为是标准的,应严格遵守,而命名为functionvariableclassfile 甚至。

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多