请问我是jquery和javascript的初学者。我正在尝试创建一个我将在一个页面上重复使用的函数,该函数基本上会循环放置在容器中的子元素。代码看起来没问题,但是当我在调用函数时传递一个全局变量作为参数时,它无法按预期工作。我知道有更好的方法可以做到这一点,我想知道如何,我在哪里弄错了或者我应该使用插件代替。谢谢。
//THE JAVASCRIPT
var prevIndex = 0, nextIndex = 1;//these are the global variables
var imgBox = $("#img-box");//the parent container
function changeSlides(parentName, a1, b1, classInit, classFinal){
var parentName,
classInit,
classFinal,
allChildren = parentName.children(),
prevSlide = allChildren.eq(a1),
nextSlide = allChildren.eq(b1),
allChildrenNum = allChildren.length,
lastSlideIndex = allChildrenNum - 1,
lastSlideElem = allChildren.eq(lastSlideIndex),
firstSlideElem = allChildren.eq(0);
if(b1<=lastSlideIndex){
changeClass(prevSlide,classFinal,classInit);//this is function changes the class of the selected element from classFinal to classInit and vice versa
changeClass(nextSlide,classInit,classFinal);
a1++;
b1++;
}else if(b1>lastSlideIndex){
a1 = 0;
b1 = a1 + 1;
changeClass(firstSlideElem, classInit, classFinal);
changeClass(lastSlideElem, classFinal, classInit);
};
};
function changeClass(elemName, class1, class2){
var class1,
class2;
if(elemName.hasClass(class1)){
elemName.removeClass(class1).addClass(class2);
};
};
$("button").click(function(){
changeSlides(imgBox,prevIndex,nextIndex,"show","hide");
});
//THE HTML
<html>
<body>
<div id="img-box">
<img src="img1.jpg" class="show">
<img src="img2.jpg" class="hide">
<img src="img3.jpg" class="hide">
</div>
<button></button>
</body>
</html>
//THE CSS
.show{opacity: 1;}
.hide{opacity: 0;}
答案 0 :(得分:0)
您将全局变量作为函数参数传递并在函数中修改它们。这只会更改函数参数值,而不是全局变量,因为函数参数被视为局部变量。对于常量值show
和hide
也是如此,您不需要通过函数参数传递它们,只需在需要时使用它们。
使用全局参数时,不必将它们传递给函数。这是全球参数背后的全部理念,它们是全球性的。
其次,您需要使用jQuery
(或$
)来访问您的元素。所以,既然我不确定,你想要实现什么,你可以从以下代码开始:
var prevIndex, nextIndex, imgBox;
prevIndex = 0;
nextIndex = 1;
imgBox = jQuery("#img-box");
function changeSlides() {
var allChildren, prevSlide, nextSlide, allChildrenNum, lastSlideIndex, lastSlideElem, firstSlideElem;
allChildren = imgBox.children();
prevSlide = jQuery(allChildren).eq(prevIndex);
nextSlide = jQuery(allChildren).eq(nextIndex);
allChildrenNum = allChildren.length;
lastSlideIndex = allChildrenNum - 1;
lastSlideElem = jQuery(allChildren).eq(lastSlideIndex);
firstSlideElem = jQuery(allChildren).eq(0);
if (nextIndex <= lastSlideIndex) {
changeClass(prevSlide);
changeClass(nextSlide);
prevIndex++;
nextIndex++;
} else if (nextIndex > lastSlideIndex) {
prevIndex = 0;
nextIndex = 1;
changeClass(firstSlideElem);
changeClass(lastSlideElem);
};
};
function changeClass(element) {
var elementIsShown = jQuery(element).hasClass("show");
if (elementIsShown) {
jQuery(element).removeClass("show").addClass("hide");
} else {
jQuery(element).removeClass("hide").addClass("show");
};
};
$("button").click(function () {
changeSlides();
});
.show {
opacity: 1;
}
.hide {
opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
<div id="img-box">
<img src="img1.jpg" class="show">
<img src="img2.jpg" class="hide">
<img src="img3.jpg" class="hide">
</div>
<button></button>
</body>
答案 1 :(得分:0)
如果要在页面上使用多个容器,可以尝试使用类。请尝试以下代码:
function ChangeSlidesClass(boxName, btnName) {
this.prevIndex = 0;
this.nextIndex = 1;
this.imgBox = jQuery('#' + boxName);
this.btnName = btnName;
this.setButtonCallback();
}
ChangeSlidesClass.prototype.setButtonCallback = function() {
var obj = this;
jQuery("#" + this.btnName).click(function () {
obj.changeSlides();
});
};
ChangeSlidesClass.prototype.changeSlides = function() {
var allChildren, prevSlide, nextSlide, allChildrenNum, lastSlideIndex, lastSlideElem, firstSlideElem;
allChildren = jQuery(this.imgBox).children();
prevSlide = jQuery(allChildren).eq(this.prevIndex);
nextSlide = jQuery(allChildren).eq(this.nextIndex);
allChildrenNum = allChildren.length;
lastSlideIndex = allChildrenNum - 1;
lastSlideElem = jQuery(allChildren).eq(lastSlideIndex);
firstSlideElem = jQuery(allChildren).eq(0);
if (this.nextIndex <= lastSlideIndex) {
this.changeClass(prevSlide);
this.changeClass(nextSlide);
this.prevIndex++;
this.nextIndex++;
} else if (this.nextIndex > lastSlideIndex) {
this.prevIndex = 0;
this.nextIndex = 1;
this.changeClass(firstSlideElem);
this.changeClass(lastSlideElem);
};
};
ChangeSlidesClass.prototype.changeClass = function(element) {
var elementIsShown = jQuery(element).hasClass("show");
if (elementIsShown) {
jQuery(element).removeClass("show").addClass("hide");
} else {
jQuery(element).removeClass("hide").addClass("show");
};
};
var changeSlides1 = new ChangeSlidesClass('img-box1', 'btn1');
var changeSlides2 = new ChangeSlidesClass('img-box2', 'btn2');
&#13;
.show {
opacity: 1;
}
.hide {
opacity: 0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
<div id="img-box1">
<img src="img1.jpg" class="show">
<img src="img2.jpg" class="hide">
<img src="img3.jpg" class="hide">
</div>
<button id="btn1"></button>
<div id="img-box2">
<img src="img1.jpg" class="show">
<img src="img2.jpg" class="hide">
<img src="img3.jpg" class="hide">
</div>
<button id="btn2"></button>
</body>
&#13;
现在我在页面上定义了两组图像框和按钮。然后我用一些(公共)方法实现一个javascript类,并为页面上设置的每个图像框/按钮生成该类的新实例。绑定click
- 事件是在类的构造函数中完成的。
您可以通过使用<div class="myImageChanger">...</div>
和var imageChangers = new ChangesSlidesClass('myImageChanger')
之类的图像框/按钮组合来自动初始化网页上的每个实例,从而改进这一点。您只需要修改类构造函数(function ChangeSlidesClass
)即可。