2013-06-01 88 views
0

如何为使用以下Javascript代码创建的图像添加链接?我尝试了一些变化,但没有奏效。如何使用Javascript为图像添加链接

请将以下代码更改为带有链接的代码。我想链接所有图片http://idealbodyweights.com/

<script type="text/javascript"> 
var image1 = new Image() 
image1.src =="http://idealbodyweights.com/wp-content/uploads/2013/05/1.png" 
var image2 = new Image() 
image2.src = "http://idealbodyweights.com/wp-content/uploads/2013/05/2.png" 
var image3 = new Image() 
image3.src = "http://idealbodyweights.com/wp-content/uploads/2013/05/3.png" 
var image4 = new Image() 
image4.src = "http://idealbodyweights.com/wp-content/uploads/2013/05/4.png" 
</script> 
    </head> 
    <body> 
    <p><img src="images/pentagg.jpg" width="720" height="90" name="slide" /></p> 
    <script type="text/javascript"> 
    var step=4; 
    function slideit() 
    { 

    document.images.slide.src = eval("image"+step+".src"); 
    if(step<4) 
     step++; 
    else 
     step=1; 
    setTimeout("slideit()",2500); 
} 
slideit(); 
</script> 
+0

不回答你的问题,但[你应该避免使用'eval'尽可能](http://stackoverflow.com/questions/86513/why-is-using-the-javascript- EVAL-功能-A-坏主意)。 –

回答

-1

这是你想要的吗? :d

<p> 
    <a href="http://idealbodyweights.com/"> 
     <img src="images/pentagg.jpg" width="720" height="90" name="slide" /> 
    </a> 
</p> 
0

我改写了一点点你的代码,因此它不使用eval等等<img>被取出使用document.getElementById

... 
    <script type="text/javascript"> 
var imgsrc = [ 
     'http://idealbodyweights.com/wp-content/uploads/2013/05/1.png', // 0 
     'http://idealbodyweights.com/wp-content/uploads/2013/05/2.png', // 1 
     'http://idealbodyweights.com/wp-content/uploads/2013/05/3.png', // 2 
     'http://idealbodyweights.com/wp-content/uploads/2013/05/4.png' // 3 
    ], 
    imgcache = [], i; 
for (i = 0; i < imgsrc.length; ++i) { // cache images 
    imgcache[i] = new Image(); 
    imgcache[i].src = imgsrc[i]; 
} 
    </script> 
</head> 
<body> 
    <p><img src="images/pentagg.jpg" 
      width="720" height="90" 
      name="slide" id="slide" /></p> 
    <script type="text/javascript"> 
    var step = -1,        // set so first will be 0 
     img = document.getElementById('slide'); // get the <img> 
    function slideit() { 
     step = (step + 1) % imgsrc.length; // loop over 0, 1, 2, 3 (or more) 
     img.src = imgsrc[step];   // set src by array index 
     setTimeout(slideit, 2500);   // repeat in 2500 ms 
    } 
    slideit();// start 
    </script> 
... 
+0

谢谢,但图像的链接在哪里? –

+0

只需将你想要的任何图像添加到数组中。除非我误解了你想要的内容? –

+0

抱歉误会我,但我希望当我点击此脚本中的任何图像时,请转到此链接,例如:http://idealbodyweights.com –