我有一些jQuery创建幻灯片使用BX滑块为未知数量的画廊。标记是由PHP CMS输出的,因此我们不能确定一个页面上需要多少个图库。
$(function () {
// Use a class rather than an ID
var $slider = $('.gallery');
// For each result
$slider.each(function (index, element) {
// If there is more than 1 image
if ($(element).find('img').length > 1) {
// Initialise a slider using the current index value
$slider.eq(index).bxSlider({
auto: false,
pager: false,
duration: 500
});
}
});
});为了使用公共函数,我需要将BX滑块的每个实例定义为一个变量。
因此,var slider = $('#gallery').bxSlider();将成为标准。如何在这条each语句中动态生成这些变量?
发布于 2012-02-17 00:37:04
你在找这样的东西吗?
$(function () {
var bxSliderInstances = {};
// Use a class rather than an ID
var $slider = $('.gallery');
// For each result
$slider.each(function (index, element) {
// If there is more than 1 image
if ($(element).find('img').length > 1) {
// Initialise a slider using the current index value
bxSliderInstances["slider" + index] = $slider.eq(index).bxSlider({
auto: false,
pager: false,
duration: 500
});
}
});
});bxSliderInstances将包含所有滑块实例作为一个关联数组,您可以根据需要使用它。
例如bxSliderInstances.slider1或`bxSliderInstances["slider1"]将为您提供第一个bxSlider实例。
https://stackoverflow.com/questions/9315051
复制相似问题