我正在编写一个简单的
if/else
语句来显示下一个或上一个链接,但不知何故我的检查不起作用。这是代码
var newPosition = currPosition;
$(".imageNavLink").click(function(event){
event.preventDefault();
newPosition = (this.rel == "next") ? newPosition + 1 : newPosition - 1;
var newImageLink = this.title;
var clickAction = this.rel;
var newImage = new Image();
$(newImage).load(function(){
newWidth = this.width, newHeight = this.height+35;
if(newPosition == totalItems){
[.. Show 'previous' link ..]
}else if(newPosition == 1){
[.. Show 'next' link ..]
}else{
[.. Show 'previous' and 'next' link ..]
}
});
newImage.src = this.title;
});
currPosition
保存元素的当前位置(数字)以与计数保持一致totalItems
是关联图像总数如果我点击第一张图片,我会不断地看到上一个链接,当我从第二张图片开始时,我会不断地看到两个链接,而当我从最后一张图片开始时,我会不断地看到上一个链接..
if(newPosition == totalItems){
[.. Show 'previous' link ..]
}else if(newPosition == 1){
[.. Show 'next' link ..]
}else{
[.. Show 'previous' and 'next' link ..]
}
重写为:
if(newPosition>1 && newPosition<totalItems){
[.. Show 'previous' and 'next' link ..]
}else if(newPosition == 1){
[.. Show 'next' link ..]
}else if(newPosition == totalItems){
[.. Show 'previous' link..]
}
仅当
newPosition
位于 1
和 totalItems
之间时,才会同时显示 prev
和 next
我不知道是不是这个,但是:
newWidth = this.width, newHeight = this.height+35;
,
后面的this.width
不应该是;
吗?