将变量javascript添加到链接以旋转图像

时间:2015-02-03 22:49:37

标签: javascript html

我有4张不同的图片。这些图像旋转。在每个图像上,我需要插入一个window.open选项。我想也许可以通过id找到它并改变onclick会起作用。只是指向我所在页面的链接。以下是我的尝试。

<script type = "text/javascript">
          function displayNextImage() {
              x = (x === images.length - 1) ? 0 : x + 1;
              document.getElementById("img").src = images[x];
              //document.getElementById("mylink").href=links[x];
              document.getElementById("mylink").onclick=links[x];

          }

          function displayPreviousImage() {
              x = (x <= 0) ? images.length - 1 : x - 1;
              document.getElementById("img").src = images[x];
          }

          function startTimer() {
              setInterval(displayNextImage, 5000);
          }

          var images = [], x = -1;
          images[0] = "/img/bakedline.png";
          images[1] = "/img/cornchipline.png";
          images[2] = "/img/friedline.png";
          images[3] = "/img/tortillaline.png";

          var links = new Array();
          links[0] = "window.open('/foodprocessor/cornchipline', 'my_new_window','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=625, height=400');";
          links[1] = "/foodprocessor/cornchipline";
          links[2] = "/foodprocessor/friedline";
          links[3] = "/foodprocessor/tortillachipline";


      </script>

HTML

<div id="imgs">
                            <a href="" id="mylink" onclick=""  ><img id="img" src="/img/bakedline.png"  /></a>
                        </div>

2 个答案:

答案 0 :(得分:0)

您需要为元素的onclick属性分配一个函数,而不仅仅是要打开的url。像这样的东西;

document.getElementById("mylink").onclick=function() { window.open(links[x]); };

请参阅GlobalEventHandlers.onclick

答案 1 :(得分:0)

在您打开的窗口中打开

文件夹

links[0] = "window.open('/foodprocessor/cornchipline' blah blah

links[0] = "window.open('/foodprocessor/cornchipline/index.php' 

或该文件的名称