Javascript點擊更換背景

<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="更換背景" onclick="t1();" />
	</div>
  </body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章