在javascript中添加指向索引数组的超链接

时间:2013-08-31 15:34:10

标签: javascript html

如何在javascript中的索引数组描述中添加链接?我的代码在下面,谢谢。我尝试使用push方法,但我认为我与showpic函数有冲突。我真的不知道,所以我很感激任何帮助。

<script type="text/javascript">
    mypic = new Array();
    mypic[0] = ["images/Lg_image1.jpg", "Large image 1 description."] //I want to add an HTML  link to a website at the end of this array
    mypic[1] = ["images/Lg_image2.jpg", "Large image 2 description."] //I want to add an HTML link to a website at the end of this array



    function showpic2(n) {
        document.getElementById("mainpic2").src = mypic[n][0]
        document.getElementById("mycaption2").innerHTML = mypic[n][1]
        count = n
    }

    count = 0;

    function next() {
        count++
        if (count == mypic2.length) {
            count = 0
        }
        document.getElementById("mainpic2").src = mypic2[count][0]
        document.getElementById("mycaption2").innerHTML = mypic2[count][1]
    }

    function prev() {
        count--
        if (count < 0) {
            count = mypic2.length - 1
        }
        document.getElementById("mainpic2").src = mypic2[count][0]
        document.getElementById("mycaption2").innerHTML = mypic2[count][1]
    }
</script>

<body>
<div id="container">
  <div class="menu1">
    <ul>
      <li><a href="#">Link 1</a> </li>
    </ul>
    <ul>
      <li><a href="#">Link 2</a> </li>
    </ul>
  </div>
  <div id="large"> <img src="images/Lg_image1.jpg" class="mainpic2" id="mainpic2"> </div>
  <div id="thumbs2"> <a href="javascript:showpic2(0)"><img src="images/sm_image1.jpg" alt="image1"/></a> <a href="javascript:showpic2(1)"><img src="images/sm_image2.jpg" alt="image2"/></a> </div>
  <div id="mycaption2">Large image default description. </div>
  <div id="logohome"> <img src="images/#.png" alt="" /> </div>
  <div id="homebox"> <img src="images/homebox1.png" alt="" /> </div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

<script type="text/javascript">
    mypic = [];
    mypic.push({address:"images/Lg_image1.jpg", desc: "Large image 1 description."});




    function showpic2(n) {
        document.getElementById("mainpic2").src = mypic['address']
        document.getElementById("mycaption2").innerHTML = mypic['desc']
        count = n
    }


</script>

类似这样的事情

答案 1 :(得分:-1)

我不知道你对.push()有什么问题,但这应该有效:

mypic[0].push("<a href='link.html'>I am a link</a>");

当然,mypic[1]也可以这样做。