javascript - JavaScript 图片库中的下一个和上一个链接不起作用

标签 javascript navigation image-gallery

我需要在同一页面上有多个画廊(带标题),并且一直在尝试实现此 javascript,但我无法获取下一个 |之前的链接适用于任一画廊。 JavaScript 新手 - 有什么建议吗?

这是 fiddle :http://jsfiddle.net/astHh/11/

HTML:

<div style="text-align: center">
  <!--  Place the first image here  -->
  <img src="http://www.cool-smileys.com/images/out11.jpg" id="mypic" name="mypic" alt="information" border="0" height="150" width="200">
  <br>
  <!--  Place the text for the first image here  --><div id="burns">Caption one</div>
  <p>
  <a href="#" onclick="S2.UpDown(-1); return false;">&laquo; Previous</a> |
  <a href="#" onclick="S2.UpDown(1); return false;"> Next &raquo;</a>
</div>

<p>

<div style="text-align: center">
  <!--  Place the first image here  -->
  <img src="http://www.cool-smileys.com/images/out13.jpg" id="mypic2" name="mypic2" alt="information" border="0" height="150" width="200">
  <br>
  <!--  Place the text for the first image here  --> <div id="burns2">Caption two</div>
  <p>
  <a href="#" onclick="S2.UpDown(-1); return false;">&laquo; Previous</a> |
  <a href="#" onclick="S2.UpDown(1); return false;"> Next &raquo;</a>
</div>

Javascript:

<script type="text/javascript">


function SimpleSlideShow(o){
 this.img=document.getElementById(o.ImageID);
 this.txt=document.getElementById(o.TextID);
 this.ary=o.Array||[];
 this.cnt=0;
}

SimpleSlideShow.prototype.UpDown=function(ud){
  this.cnt+=ud;
  this.cnt=this.cnt<0?this.ary.length-1:this.cnt==this.ary.length?0:this.cnt;
  if (this.ary[this.cnt]){
   if (this.img&&this.ary[this.cnt][0]){
    this.img.src=this.ary[this.cnt][0];
    this.img.alt=this.ary[this.cnt][1];
   }
   if (this.txt){
    this.txt.innerHTML=this.ary[this.cnt][2]||'';
   }
  }
 }

S1=new SimpleSlideShow({
 ImageID:'mypic',
 TextID:'burns',
 Array:[
  ['http://www.cool-smileys.com/images/out11.jpg,' 'Caption one', 'The beautiful mountains'],
  ['http://www.cool-smileys.com/images/out10.jpg','Caption two','The crystal clear lake'],

]
});

S2=new SimpleSlideShow({
 ImageID:'mypic2',
 TextID:'burns2',
 Array:[
  ['http://www.cool-smileys.com/images/out13.jpg','caption one', 'The beautiful mountains'],
  ['http://www.cool-smileys.com/images/out12.jpg','caption two','The lonesome, barren tree']
]
});


</script>

最佳答案

您的 JavaScript 中有多个语法错误:

从脚本 Pane 中删除:

<script type="text/javascript">

</script>

在“S1”的定义中,您缺少一个逗号(就在“标题一”之前):

S1=new SimpleSlideShow({
    ImageID:'mypic',
    TextID:'burns',
    Array:[
        ['http://www.cool-smileys.com/images/out11.jpg,','Caption one', 'The beautiful mountains'],
        ['http://www.cool-smileys.com/images/out10.jpg','Caption two','The crystal clear lake'],
    ]
});

然后在 HTML Pane 中,您应该将第一组上一个/下一个链接更新为目标 S1 而不是 S2。

更新的 fiddle :http://jsfiddle.net/astHh/12/

关于javascript - JavaScript 图片库中的下一个和上一个链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18242868/

相关文章:

javascript - {"errors": ["Invalid image URL"]} with aviary integration

xcode - 当键盘可见/不可见时显示/隐藏 navigationBarItems | SwiftUI

jquery - 滚动 HTML 画廊,可能有很多图像,需要延迟加载/卸载?

javascript - imagepicker jquery插件: image in list item does not get updated after changing data-img-src

javascript - 通过组合这些函数来防止 javascript 中的代码复制

javascript - 使用 @unform 和 react-datepicker 破坏编辑屏幕的 Datepicker 组件

windows - 导航到页面会增加内存使用量 Windows Universal 8.1

ios - 如何将 Microsoft Bing map 导航功能集成到 iPhone 应用程序中?

jquery - 调整图库中图像的大小以适应行,但保持每个图像的纵横比

jQuery 无限缩略图库