在javascript中只有3个图像的随机图像

时间:2014-04-01 18:26:35

标签: javascript

有没有办法随机制作一个函数RandomSwapDown(x),以便每次点击它时都显示三个图像中的一个? 谢谢。

<script>
    function RandomSwapDown(x) {                 
        x.src = '4.gif';    
    }                               
    function SwapBack(x) {       
        x.src = 'mouse_over.png';           
    }
    function SwapOut(x) {
        x.src = 'mouse_normal.png';
    }  
</script>

</html>

<div style='top: 200px; left: 175px; position: absolute;'>

<img src='mouse_normal.png'  onMouseOver='SwapBack(this)' onMouseOut= 'SwapOut(this)' onMouseDown = 'RandomSwapDown(this)' width="121" height="146" >

<img src='mouse_normal.png'  onMouseOver='SwapBack(this)' onMouseOut= 'SwapOut(this)' onMouseDown = 'RandomSwapDown(this)' width="121" height="146" >

<img src='mouse_normal.png'  onMouseOver='SwapBack(this)' onMouseOut= 'SwapOut(this)' onMouseDown = 'RandomSwapDown(this)' width="121" height="146" >

</div>

2 个答案:

答案 0 :(得分:0)

DEMO

var picArr = []; // array of images to choose from

function RandomSwapDown(x) {
    x.src = picArr[Math.floor(Math.random()*3)];
}
function SwapBack(x) {
    x.src = picArr[Math.floor(Math.random()*3)];
}
function SwapOut(x) {
    x.src = picArr[Math.floor(Math.random()*3)];
}

答案 1 :(得分:0)

嗨,我现在不能在javascript中使用它,但在java中你可以

Random generator=new Random();    
int rnd_number;    
rnd_number=generator.nextInt(MaxRandom-MinRandom)+MinRandom;