使用javascript添加到幻灯片放映的超链接

时间:2010-08-16 11:00:55

标签: javascript hyperlink

我使用Javascript做了一个简单的幻灯片放映,但现在我想在图片滑过时添加图片链接,以便在单独的窗口中打开pdf。谁能告诉我怎么做?这是我到目前为止的脚本。谢谢!朱莉娅

              var dimages=new Array();
    var numImages=3;
    for (i=0; i<numImages; i++)
    {
      dimages[i]=new Image();
      dimages[i].src="images/image"+(i+1)+".jpg";
    }
    var curImage=-1;

    function swapPicture()
    {
      if (document.images)
      {
        var nextImage=curImage+1;
        if (nextImage>=numImages)
          nextImage=0;
        if (dimages[nextImage] && dimages[nextImage].complete)
        {
          var target=0;
          if (document.images.myImage)
            target=document.images.myImage;
          if (document.all && document.getElementById("myImage"))
            target=document.getElementById("myImage");

          // make sure target is valid.  It might not be valid
          //   if the page has not finished loading
          if (target)
          {
            target.src=dimages[nextImage].src;
            curImage=nextImage;
          }

          setTimeout("swapPicture()", 5000);

        }
        else
        {
          setTimeout("swapPicture()", 500);
        }
      }
    }

    setTimeout("swapPicture()", 5000);

1 个答案:

答案 0 :(得分:0)

我认为您可以做的最好的事情是在HTML中添加一个锚元素,例如

<a id="imagelink"><img id="myImage" …></a>

并在更改图片时更改其href(可能还有title):

// make sure target is valid.  It might not be valid
//   if the page has not finished loading
if (target)
{
  target.src=dimages[nextImage].src;
  targetlink = document.getElementById("imagelink");
  targetlink.href = add-link-here;
  targetlink.title = "title message";
  curImage=nextImage;
}

此外,您正在测试myImage两次的存在。首先来到这里:

if (document.all && document.getElementById("myImage"))

然后在这里:

target=document.getElementById("myImage");
…
if (target)