我正在使用 jQuery 插件“Owl Carousel”,当我尝试使用 javascript 变量作为选项值初始化插件时,遇到了一个奇怪的问题。
基本上我有以下内容:
// thumbnail_count is the number of images in the slider,
// already calculated elsewhere
var thumbnail_breakpoints = '';
switch (thumbnail_count) {
case 3:
thumbnail_breakpoints = '[0, 1], [219, 2], [299, 3]';
break;
case 2:
thumbnail_breakpoints = '[0, 1], [219, 2]';
break;
case 1:
thumbnail_breakpoints = '[0, 1]';
break;
}
jQuery(function($) {
$('#itemslider-zoom').data('owlCarousel').reinit({
itemsCustom: [thumbnail_breakpoints]
});
});
这不起作用,并且 itemCustom 的值未正确传递给 reinit 函数。但是,如果我手动输入 itemCustom 的值,则可以按需要完成工作。对于两个图像,例如
jQuery(function($) {
$('#itemslider-zoom').data('owlCarousel').reinit({
itemsCustom: [[0, 1], [219, 2]]
});
});
谁能帮我解决一下这是为什么吗?使用 javascript 调试我可以看到thumbnail_breakpoints 肯定设置正确。另外,如果我更改 switch case 语句,以便每个语句都直接使用所需的值进行 jQuery 函数调用,它可以正常工作,但这是大量的代码重复。
预先感谢您的帮助。
附注这是在prototypejs中,因此为什么jQuery以无冲突模式调用。
最佳答案
创建二维数组并直接传递。
使用
var thumbnail_breakpoints = [];
switch (thumbnail_count) {
case 3:
thumbnail_breakpoints = [[0, 1], [219, 2], [299, 3]];
break;
case 2:
thumbnail_breakpoints = [[0, 1], [219, 2]];
break;
case 1:
thumbnail_breakpoints = [[0, 1]];
break;
}
jQuery(function($) {
$('#itemslider-zoom').data('owlCarousel').reinit({itemsCustom: thumbnail_breakpoints});
});
关于javascript - 将 javascript 变量传递给 jQuery 插件初始化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802811/