0

如何为使用以下 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>
4

2 回答 2

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>
...
于 2013-06-01T20:30:25.130 回答
-1

是你想要的吗?:D

<p>
    <a href="http://idealbodyweights.com/">
        <img src="images/pentagg.jpg" width="720" height="90" name="slide" />
    </a>
</p>
于 2013-06-01T21:00:03.230 回答