0

I have a list of pictures that need to be displayed. The list of pictures is very large (500+) and I did not want to load each by individually naming them in the html code for the href attribute of the a tag. Hence, I used javascript to generate filenames incremented by 1, which produces the correct filepath. Here is the javascript:

function bigpicture()
    {
        var filepath = "\"images/print/latest/" ;
        var ext = ".png\"";
        var total = filepath + num + ext;
        document.write(total)
        num = num + 1;
    }

When I call bigpicture() I get the correct filepath and name, and all multiple calls it increments as well. My problem is getting this string to display after the href attribute of my a tag, as seen here:

HTML

<div class = "big">
        <ul>    
            <li>
                <a href=<script>bigpicture()</script> rel="lightbox" title=""><img class="floated_img" img src="images/print/latest/small/1.png" /></a>
            </li>

        </ul>
</div>

I assumed (incorrectly) that I could call the function like this, but it does not work. Could anyone tell me how to execute the script in a way that the sting appears after href? Thank you.

4

2 回答 2

1

您可以将函数放在onclick处理程序中。

onclick处理程序会将变量绑定到您的thisa 节点。

也许您需要在onclick喜欢之前与灯箱结合使用处理程序onmousedown

如何通过javascript更改按钮单击时<a>标签的href

于 2013-05-29T07:36:30.500 回答
0

您这样做的方式将不起作用,因为函数调用未绑定到您的 html 代码中的任何事件,例如 onload 或 onsubmit,因此不会执行。

我会使用 jQuery 附加到您ulbigdiv 中。您的调用bigpicture()可能如下所示:

<script>

  function bigpicture()
  {
    var filepath = "./images/print/latest/" ;
    var ext = ".png\"";
    for(var i = 0; i < numOfPictures; i++){
        $('#big ul').append('<li>' + filepath + i + ext + '</li>');
      }

  }

  $(function(){     //this is jQuery shorthand for document ready
      bigpicture(); //anything in this anonymous function will execute
    })              //after HTML document has been loaded

</script>

文档就绪信息来自这里

于 2013-05-29T07:47:36.597 回答