从获取的cookie的函数参数 [英] Fetching argument of function from 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(< DIV 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(< DIV 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 thana
of them!解决方案I think the problem lies in the first line :
a = $.cookie("col-layout");
Your function
rowCreation
treatsa
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 typeNumber
, like this :a = parseInt($.cookie("col-layout"), 10);
More info about
parseInt
here.这篇关于从获取的cookie的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!