如何创建一个 jQuery 循环来更改链接的 href 属性?

发布于 2024-10-09 15:32:55 字数 229 浏览 0 评论 0原文

我使用 jQuery 从头开始​​构建了一个幻灯片画廊。它可以有任意数量的幻灯片。在幻灯片下方有一个“更多信息”按钮。我希望此链接每 5000 毫秒更改一次以反映正在显示的幻灯片。

我知道我可以使用 .attr 来更改 href 值本身,它只是以 5000 毫秒的间隔将其更改为不同的东西(循环!)完全不知所措...

帮助将不胜感激!

I've built a slideshow gallery from scratch using jQuery. It can have any number of slides. Under the slideshow I have a 'More info' button. I would like this link to change every 5000ms to reflect the slide being show.

I know I can use .attr to change the href value itself, it is just changing it at intervals of 5000ms to different things (on a loop!) that I'm completely at a loss with...

Help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-10-16 15:32:55

使用计时器

//Set Image to first picture by default.
$('#yourImageId').attr("src", imageSrcArray[0]);

var milliseconds = 5000;

//Call Function after 5 seconds to show second picture
var t=setTimeout("changeSlide();", milliseconds); 

//If you set the image's original src to your first array item, this will cause the first update in 5 seconds to display the second item.
var cnt=1; 

function changeSlide(){

    //update image src
    $('#yourImageId').attr("src", imageSrcArray[cnt]);

    t=setTimeout("changeSlide();", milliseconds);//Call Function Again after 5 seconds

    cnt++;

    //Check that cnt is within image array bounds
    if (cnt > imageSrcArray.length-1) cnt=0;

}

Use a timer.

//Set Image to first picture by default.
$('#yourImageId').attr("src", imageSrcArray[0]);

var milliseconds = 5000;

//Call Function after 5 seconds to show second picture
var t=setTimeout("changeSlide();", milliseconds); 

//If you set the image's original src to your first array item, this will cause the first update in 5 seconds to display the second item.
var cnt=1; 

function changeSlide(){

    //update image src
    $('#yourImageId').attr("src", imageSrcArray[cnt]);

    t=setTimeout("changeSlide();", milliseconds);//Call Function Again after 5 seconds

    cnt++;

    //Check that cnt is within image array bounds
    if (cnt > imageSrcArray.length-1) cnt=0;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文