<script type="text/javascript"> //第一种方法,使用数组 var i=0; function t1(){ var arr=['url(img/bgf.jpg)','url(img/bg.jpg)','url(img/4f.jpg)','url(img/5f.jpg)','url(img/6f.jpg)']; var bottom = document.getElementById('zhengti'); if(i==arr.length){ i=0; } bottom.style.backgroundImage = (arr[i++]); } /*第二种方法,if...else判断 var i = 0; function t1(){ var bottom = document.getElementById('zhengti'); if(i++%2==0){ bottom.style.backgroundImage = "url(img/bgf.jpg)"; } else{ bottom.style.backgroundImage = "url(img/bg.jpg)"; } } */ </script> </head> <body id="zhengti"> <div id="bottom"> <input type="button" value="更换背景" οnclick="t1();" /> </div> </body> </html>
转载于:https://blog.51cto.com/hbbdgyb/1844851