我在面板中有圖像和按鈕。單擊按鈕時更改圖片
當點擊按鈕時,我希望基於存儲的圖像數組隨機替換另一張圖像。
我堅持在按鈕內實現更改圖像功能。
援助將不勝感激。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Handler for .ready() called.
var imageList = ['one.png', 'two.png', 'three.png', 'four.png'];
$('#btn').click(function(){
var imgName = imageList[Math.floor(Math.random()*imageList.length)];
$('#image').attr('src', imgName);
});
});
</script>
<button id="btn">Click Me</button>
<img id="image" src="someimage.png" />
1因爲jQuery是真棒。 :) – Nathan