How to add a simple hyperlink to images using JQuery Wrapper

纵然是瞬间 提交于 2019-12-24 01:25:30

问题


I'm not a coder and I need a simple solution to adding a hyperlink in an html document to jQuery images ..

the following does not work so I guess it's more complex that the following ...

<div class="slider-wrapper">
                    <div class="slider">
                        <ul class="items">
                          <li> <a href="http://url/index.php"><img src="images/slider-img-name.jpg" alt="name" /></a>
                          </li> 

Is there an easy way to make this work?


回答1:


This should work, you just need to add as many links as you want but bear in mind that for each img you must have a corresponding link, otherwise the code will fail

<script>
    var links = new Array();
    links[0] = "http://www.google.com";
    links[1] = "http://www.yahoo.com";
    links[2] = "http://www.bing.com";

    $(document).ready(function(){
        $('.items a').each(function(){
            index = $(this).parent().index();
            $(this).attr("href",links[index]);
        });
    });
</script>

<ul class="items">
    <li> <a href=""><img src="images/slider-img-name.jpg" alt="name" /></a></li>
    <li> <a href=""><img src="images/slider-img-name.jpg" alt="name" /></a></li>
    <li> <a href=""><img src="images/slider-img-name.jpg" alt="name" /></a></li>
</ul>


来源:https://stackoverflow.com/questions/9376559/how-to-add-a-simple-hyperlink-to-images-using-jquery-wrapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!