How to add a simple hyperlink to images using JQue

2019-07-24 19:26发布

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条回答
对你真心纯属浪费
2楼-- · 2019-07-24 20:10

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>
查看更多
登录 后发表回答