javascript 在php中使用jquery每5秒更改一次图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20639606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:55:44  来源:igfitidea点击:

Changing pictures every 5 seconds using jquery in php

javascriptphpjqueryhtml

提问by mai_way

Please help me I am new in jQuery. here is my code:

请帮助我,我是 jQuery 新手。这是我的代码:

<img src="default.jpg" />
<img src="a.jpg" />
<img src="b.jpg" />
<img src="c.jpg" />

I want to change the picture from default.jpg to a.jpg. then a.jpg to to b.jpg and so on, every 5 seconds. using jquery. Thanks a lot

我想将图片从default.jpg 更改为a.jpg。然后 a.jpg 到 b.jpg 等等,每 5 秒一次。使用jQuery。非常感谢

回答by Archios

<img id="thisImg" alt="img" src="images/img0.png"/>
<script type="text/javascript">
    $(function(){
        //prepare Your data array with img urls
        var dataArray=new Array();
        dataArray[0]="images/img1.png";
        dataArray[1]="images/img2.png";
        dataArray[2]="images/img3.png";
        dataArray[3]="images/img0.png";

        //start with id=0 after 5 seconds
        var thisId=0;

        window.setInterval(function(){
            $('#thisImg').attr('src',dataArray[thisId]);
            thisId++; //increment data array id
            if (thisId==3) thisId=0; //repeat from start
        },5000);        
    });
</script>

回答by Jhonatan G.

Set this in head

在头中设置这个

<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>

jquery code

查询代码

setInterval(function(){
$('#img').remove();
$('body').prepend('<img src="urlimgs" id="img">');
},5000);

I didn't understand what you want but it is so basic! You need learn more about jquery.. You can improve this code.. it is only a example

我不明白你想要什么,但它是如此基本!您需要了解有关 jquery 的更多信息.. 您可以改进此代码.. 这只是一个示例