变量问题

问题描述 投票:0回答:2

我正在编写一个简单的

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
    是关联图像总数
  • 的数值

如果我点击第一张图片,我会不断地看到上一个链接,当我从第二张图片开始时,我会不断地看到两个链接,而当我从最后一张图片开始时,我会不断地看到上一个链接..

jquery
2个回答
0
投票
 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


0
投票

我不知道是不是这个,但是:

newWidth = this.width, newHeight = this.height+35;

,
后面的
this.width
不应该是
;
吗?

© www.soinside.com 2019 - 2024. All rights reserved.