我想每 5 秒从链接标签中更改带有“href”的标签的 src。
这是代码:
<div id="bgStretch"><img id="bgStretchimg" src="images/picture1.jpg" alt=""></div> <!-- here is the image src i want to change-->
<nav class="bgNav">
<ul>
<li class="active"><a href="images/picture1.jpg"></a></li>
<li><a href="images/picture2.jpg"></a></li>
</ul>
</nav>
<!--these are the links so i hope you guys to gimme a solution in javascript-->
这是我试过的jQuery:
<script>
$(document).ready(function() {
var items = $("nav.bgNav ul li");
var index = -1;
function next() {
// if not the very first iteration,
// remove the active class from prev item
if (index !== -1) {
var urlI=$(this).("a").attr(href);
items.eq(index).removeClass("active");
}
// go to next item
++index;
// if past end, wrap to beginning
if (index >= items.length) {
index = 0;
}
// add active class to next item
items.eq(index).addClass("active");
$('#bgStretchimg').attr('src', urlI);
// schedule next iteration
setTimeout(next, 1000);
}
next();
});
</script>
它不适合我,所以我问你们,谢谢。