JavaScript随机数
JavaScript #随机数2012-05-18 23:53
利用随机数Math对象下的random方法。看下面的例子。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<style type="text/css">
.de{ width:450px; height:450px; border:4px solid #ccc; margin:0 auto; margin-top:50px;}
.we{ width:450px; height:450px;}
</style>
<script type="text/javascript">
function de() {
var we = document.getElementById("we");
var img = ["XMJ.jpg", "XMJX.jpg", "psb.jpg"]; //url 链接数组
var allNumber = img.length; // 保存img的个数
var randomImg = parseInt(allNumber * Math.random()); //Math.random随机数
we.setAttribute("src",img[randomImg]);
}
window.onload = function () {
de();
}
</script>
</head>
<body>
<div class="de">
<img src="psb.jpg" alt="" title="" class="we" id="we" />
</div>
</body>
</html>相关文章
- Javascript键盘事件的组合使用 2012/05/18
- 伦敦2012奥运会倒计时代码javascript版 2012/05/12
- js全选/全不选Checkbox函数 2012/05/09
- javascript的cookie相关函数 2012/05/09
- Document对象详细介绍 2012/05/01
- js判断页面是否加载完成的方法 2012/04/30
- IE6浏览网页出现已终止操作的错误提示解决方法 2012/04/28
- js生成随机数 2012/04/25
- JavaScript base64_decode 函数 2012/04/17
- JavaScript base64_encode 函数 2012/04/17