我有4个函数,它们使用适当的选项调用Swiper滑块。在将它们附加到正确的div后,然后添加将它们链接在一起的代码,因此一个滑块控制另一个,反之亦然。
我收到错误" Uncaught ReferenceError:未定义swiperV2"。我认为这是因为它们在每个功能中,并且对它们的引用并没有看到'它们。
有关如何解决此问题的任何想法?
由于
$(".swiper-container-v").each(function(index, element) {
var $this = $(this);
$this.addClass("instance-" + index);
$this.find(".swiper-button-prev").addClass("btn-prev-" + index);
$this.find(".swiper-button-next").addClass("btn-next-" + index);
var swiperV = new Swiper(".swiper-container-v.instance-" + index, {
// your settings ...
pagination: '.swiper-pagination-v',
paginationClickable: true,
direction: 'vertical',
spaceBetween: 0,
mousewheelControl: false,
speed: 600,
nextButton: ".btn-next-" + index,
prevButton: ".btn-prev-" + index
});
});
$(".swiper-container-h").each(function(index, element) {
var $this = $(this);
$this.addClass("instance-" + index);
$this.find(".swiper-button-prev").addClass("btn-prev-" + index);
$this.find(".swiper-button-next").addClass("btn-next-" + index);
var swiperH = new Swiper(".swiper-container-h.instance-" + index, {
// your settings ...
pagination: '.swiper-pagination-h',
paginationClickable: true,
spaceBetween: 0,
parallax: true,
speed: 600,
nextButton: ".btn-next-" + index,
prevButton: ".btn-prev-" + index
});
});
$(".swiper-container-v2").each(function(index, element) {
var $this = $(this);
$this.addClass("instance-" + index);
$this.find(".swiper-button-prev").addClass("btn-prev-" + index);
$this.find(".swiper-button-next").addClass("btn-next-" + index);
var swiperV2 = new Swiper(".swiper-container-v2.instance-" + index, {
// your settings ...
paginationClickable: true,
direction: 'vertical',
spaceBetween: 0,
mousewheelControl: false,
speed: 600
});
});
$(".swiper-container-h2").each(function(index, element) {
var $this = $(this);
$this.addClass("instance-" + index);
$this.find(".swiper-button-prev").addClass("btn-prev-" + index);
$this.find(".swiper-button-next").addClass("btn-next-" + index);
var swiperH2 = new Swiper(".swiper-container-h2.instance-" + index, {
// your settings ...
paginationClickable: true,
spaceBetween: 0,
parallax: true,
speed: 600
});
});
swiperV2.params.control = swiperV;
swiperH2.params.control = swiperH;
swiperV.params.control = swiperV2;
swiperH.params.control = swiperH2;
答案 0 :(得分:0)
真的......这就是范围的工作原理,并且几乎无处不在所有基于类/功能的编程语言。
为了能够访问属于函数范围的变量,必须在预期对其起作用的范围内为该函数之外创建一个引用。
所以,如果您有类似的东西,
func() {
var myVar = ...
}
print(myVar)
这不起作用,因为函数范围之外的全局范围没有myVar的概念。
因此,您需要先创建对该变量的引用。
var myVar;
func() {
var myVar = ...
}
print(myVar)
现在因为myVar是在函数范围之外生成的,所以函数知道myVar。
或者你可以返回myVar的值并执行类似的操作。
func() {
return myVar = ...
}
var myFunc = func();
print(myFunc())
你基本上已经执行了一个函数赋值(即将该函数赋值给变量),在这种情况下,代理的变量存储了函数的返回值。