2013-06-01 31 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 –