从获取的cookie的函数参数 [英] Fetching argument of function from cookie

查看:165
本文介绍了从获取的cookie的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此功能来包装的div特定数量的成排

 函数rowCreation(一){
    一个= $ .cookie(COL-布局);
    如果(typeof运算(一)===未定义)一= 3;    如果($(div.gallery项目)。父()是(div.row)){
        $('div.row')。replaceWith(函数(){
            返回$('div.gallery项目',这一点);
        });
    }
    VAR的div = $(#一节画廊 - 包装div.gallery项目);
    对于(VAR I = 0; I< divs.length; I + = A){
        divs.slice(I,I + A).wrapAll(< D​​IV CLASS ='行'>< / DIV>中);
    }
}

不过,有些事情似乎出问题,我无法弄清楚究竟是什么。第一行corretly形成的,它包含了 A 元素。但是,所有剩余的div被放在一个单列,即使有比 A它们更多!


解决方案

我认为问题出在第一行:

  A = $ .cookie(COL-布局);

您函数 rowCreation 治疗 A 为一个数字,但是cookie将被赋予它当成一个字符串。所以,当你做到这一点,

 为(VAR I = 0; I< divs.length; I + = A){
    divs.slice(I,I + A).wrapAll(< D​​IV CLASS ='行'>< / DIV>中);
}


  

I = 0时,


你问阵列1和05(CONCAT 1 +A)之间进行切片,那很好,因为这些存在的。因此,没有任何问题。


  

当我= 5,


你问阵列5和15(CONCAT 1 +A)之间进行切片,所以这需要关闭所有的数组并把它封装在一个 .row 。这就是为什么它适合你有问题。

尝试解析该cookie检索COL-布局来类型的变量编号,就像这样:

  A = parseInt函数($饼干(COL-布局),10);

关于 parseInt函数

更多信息这里

I have this function to wrap a specific number of divs into a row.

function rowCreation(a) {
    a = $.cookie("col-layout");
    if(typeof(a)==="undefined") a = 3;

    if ($("div.gallery-item").parent().is("div.row")) {
        $('div.row').replaceWith(function () {
            return $('div.gallery-item', this);
        });
    }
    var divs = $("section#gallery-wrapper div.gallery-item");
    for (var i = 0; i < divs.length; i += a) {
        divs.slice(i, i + a).wrapAll("<div class='row'></div>");
    }
}

But something seems to go wrong and I can't figure out what exactly. The first row is formed corretly, it contains a elements. But all the remaining divs are put into one single row, even when there are more than a of them!

解决方案

I think the problem lies in the first line :

a = $.cookie("col-layout");

Your function rowCreation treats a as a number, but the cookie is giving it out like a string. So, when you do this,

for (var i = 0; i < divs.length; i += a) {
    divs.slice(i, i + a).wrapAll("<div class='row'></div>");
} 

When i = 0,

You're asking the array to be sliced between 1 and "05" (concat i + "a"), thats fine, because these exist. So no problems.

When i = 5,

You're asking the array to be sliced between 5 and "15" (concat i + "a"), so this takes off all the arrays and wraps it in one .row. That's why its problematic for you.

Try parsing the cookie retrieved "col-layout" to a variable of type Number, like this :

a = parseInt($.cookie("col-layout"), 10);

More info about parseInt here.

这篇关于从获取的cookie的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆