JavaScript随机数
JavaScript #随机数2012-05-18 23:53
利用随机数Math对象下的random方法。看下面的例子。
01 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
02 | < html xmlns = "http://www.w3.org/1999/xhtml" > |
03 | < head > |
04 | < title ></ title > |
05 | < style type = "text/css" > |
06 | .de{ width:450px; height:450px; border:4px solid #ccc; margin:0 auto; margin-top:50px;} |
07 | .we{ width:450px; height:450px;} |
08 | </ style > |
09 | < script type = "text/javascript" > |
10 | function de() { |
11 | var we = document.getElementById("we"); |
12 | var img = ["XMJ.jpg", "XMJX.jpg", "psb.jpg"]; //url 链接数组 |
13 | var allNumber = img.length; // 保存img的个数 |
14 | var randomImg = parseInt(allNumber * Math.random()); //Math.random随机数 |
15 | we.setAttribute("src",img[randomImg]); |
16 | } |
17 | window.onload = function () { |
18 | de(); |
19 | } |
20 | </ script > |
21 | </ head > |
22 | < body > |
23 | < div class = "de" > |
24 | < img src = "psb.jpg" alt = "" title = "" class = "we" id = "we" /> |
25 | </ div > |
26 | </ body > |
27 | </ 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