问题
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