简体   繁体   English

使用javascript将超链接添加到幻灯片中

[英]Adding hyperlinks to a slide show using javascript

I've made a simple slide show using Javascript, but now I want to add links to the images as they slide through, so that a pdf opens in a separate window. 我已经使用Javascript制作了一个简单的幻灯片放映,但是现在我想在图像滑过时添加指向图像的链接,以便在单独的窗口中打开pdf。 Can anyone tell me how to do this? 谁能告诉我该怎么做? Here's the script I have so far. 这是我到目前为止的脚本。 Thanks! 谢谢! Julia 朱莉亚

              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);

I think the best you can do is adding an anchor element to your HTML, like 我认为您能做的最好的就是将锚元素添加到HTML中,例如

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

and change its href (and perhaps its title ) when changing the image: 并在更改图片时更改其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;
}

Also, you're testing for the presence of myImage twice. 另外,您还要两次测试myImage的存在。 First here: 首先在这里:

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

and then here: 然后在这里:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM