注意:[ ]符号在JavaScript中定义一个数组,{ }则定义一个对象
javascript随机数生成方法
1.使用内置的随机数发生方法:
Math.random(); //该方法产生一个0到1之间的浮点数。
Math.floor(Math.random()*10+1); //1-10
Math.floor(Math.random()*24);//0-23
2.基于时间,亦可以产生随机数:
var now=new Date();
var number = now.getSeconds(); //这将产生一个基于目前时间的0到59的整数。
var now=new Date();
var number = now.getSeconds()%43; //这将产生一个基于目前时间的0到42的整数。
创建随机图片,这是利用随机数所能做的最简单的事了。此例中,我们假设有10张图片, banner1.gif.....banner10.gif,每当文档被载入时,我们希望能随机地显示这些图片。
- <script language="JavaScript"><!--
- document.write('<img src="../images/banner' + rand(10) + '.gif" width=400 height=40>');
- //--></script>
4.JS生成不重复的随机数组
- <script type="text/javascript">
- function getkfArrayItems(s, e, num) {
- var arr = new Array();
- for (i = s; i < e; i++) {
- arr[i] = i;
- }
- var temp_array = new Array();
- for (var index in arr) {
- temp_array.push(arr[index]);
- }
- var return_array = new Array();
- for (var i = 0; i < num; i++) {
- if (temp_array.length > 0) {
- var arrIndex = Math.floor(Math.random() * temp_array.length);
- return_array[i] = temp_array[arrIndex];
- temp_array.splice(arrIndex, 1);//入栈后删除 即去重
- } else {
- break;
- }
- }
- return return_array;
- }
- //得到数组的随机元素12个
- kf_array = getkfArrayItems(0, 20, 12);
- </script>
广告所有列表ads.js
- <!--
- document.write("<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://code.116b.com/htmlcode/2499.html\" target=\"_blank\" title=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/yxwz0926.jpg\" width=\"50\" height=\"50\" />英雄王座</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://tongji.wan5d.com/pubpage/13_20101209829.html \"\r\n target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/wd09221.jpg\" width=\"50\" height=\"50\" />御宠风云</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://game.51.com/act/51wt/html?from=265g_1\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/5109281.jpg\" width=\"50\" height=\"50\" />神仙道</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://game.51.com/act/51wt/html?from=265g_2\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/smzt09281.jpg\" width=\"50\" height=\"50\" />神魔遮天</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://lm.189play.com/?zj12=265\" \r\n target=\"_blank\" title=\"战将OL\">\r\n <img src=\"http://img.265g.com/images/gg/zj0803.jpg\" width=\"50\" height=\"50\" />战将OL</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://tc.zhaizu.com/shanhai/flash4/index.php?id=10240\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/zz09011.jpg\" width=\"50\" height=\"50\" />山海创世录</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://un.3737.com/count/index.php?webid=17&adid=667\"\r\n target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/37370801.jpg\" width=\"50\" height=\"50\" />西游征途</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://gg.xiyou6.com/265g.html?sid=10\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/dp0823.jpg\" width=\"50\" height=\"50\" />极品修真</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://tg.139p.com/zhfy/zh_265g.html?p=s&s=[plid]\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/zhfy0923.jpg\" width=\"50\" height=\"50\" />战魂风云</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://img.kd008.com/gg/10147.html\" target=\"_blank\">\r\n<img src=\"http://img.265g.com/images/gg/xdzz0923.jpg\" width=\"50\" height=\"50\" />现代战争2\r\n</a\r\n></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://gg.9c.com/gg/track/event?id=18.54.1&cd=ggcc&sn=33948879f52cfd4f41a4b125bdfe1da9e3154092&url=http%3A%2F%2Fimages.9c.com%2Fwztx%2F1%2F2%2F265g%2F265g_kf%2F265g_kf.html\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/wztx09261.jpg\" width=\"50\" height=\"50\" />王者之战</a></div>\r\n<div class=\"gimg_li\">\r\n <a href=\"/goto.html?url=http://p.17kuxun.com/s/1/246/126.html\" target=\"_blank\">\r\n <img src=\"http://img.265g.com/images/gg/sssg0826.jpg\" width=\"50\" height=\"50\" />盛世三国</a></div>");
- -->
随机广告列表
- <script src="jquery.js" type="text/javascript"></script>
- <div class="top_kaifu"></div>
- <div id="div_zxkf_h" style="display: none">
- <script src="ads.js" type="text/javascript"></script>
- </div>
- <script type="text/javascript">
- var kfhtml='';
- for (i = 0; i < kf_array.length; i++)
- {
- kfhtml+='<div class="gimg_li">'+$('#div_zxkf_h>div').eq(kf_array[i]).html()+'</div>';
- }
- $('.top_kaifu').append('<i>最新开服</i>'+kfhtml);
- </script>
效果
方式二.广告所有列表数据list.js,js处理,代替后端请求 http://hudeyong926.iteye.com/blog/1123139
- var jsonarr=eval([{"pic":"/pop/images/1.gif","gname":"清纯美女气质大片","link":"http://www.xiumei.com/gallery/beauty/"},
- {"pic":"/pop/images/2.gif","gname":"青春美少女诱人自拍","link":"http://www.xiumei.com/a/20110705/34392.shtml"},
- {"pic":"/pop/images/3.gif","gname":"纯情MM魅惑美图","link":"http://www.xiumei.com/a/20110630/34096.shtml"},
- {"pic":"/pop/images/4.gif","gname":"条纹控可爱制服写真","link":"http://www.xiumei.com/a/20110628/33836.shtml"},
- {"pic":"/pop/images/5.gif","gname":"女神周韦彤湿身写真","link":"http://www.xiumei.com/a/20110429/30915.shtml"},
- {"pic":"/pop/images/6.gif","gname":"夏季甜美感内衣外穿","link":"http://www.xiumei.com/a/20110715/35218.shtml"},
- {"pic":"/pop/images/7.gif","gname":"清新日系少女发型","link":"http://www.xiumei.com/a/20110816/36976.shtml"},
- {"pic":"/pop/images/8.gif","gname":"漂亮姐妹花服饰写真","link":"http://www.xiumei.com/a/20110817/37057.shtml"},
- {"pic":"/pop/images/9.gif","gname":"矮个美女妙搭变高","link":"http://www.xiumei.com/a/20110818/37073.shtml"},
- {"pic":"/pop/images/10.gif","gname":"90后MM可爱发型秀","link":"http://www.xiumei.com/a/20110817/36997.shtml"},
- {"pic":"/pop/images/11.gif","gname":"周杰伦女友时尚搭配","link":"http://www.xiumei.com/a/20110815/36845.shtml"},
- {"pic":"/pop/images/12.gif","gname":"姜妍魔鬼身材写真","link":"http://www.xiumei.com/a/20110727/35887.shtml"},
- {"pic":"/pop/images/13.gif","gname":"性感内衣诱惑他心","link":"http://www.xiumei.com/a/20110816/36956.shtml"},
- {"pic":"/pop/images/14.gif","gname":"最美温柔梨花头发型","link":"http://www.xiumei.com/a/20110815/36881.shtml"},
- {"pic":"/pop/images/15.gif","gname":"自曝私密房事的女星","link":"http://www.xiumei.com/a/20110813/36798.shtml"},
- {"pic":"/pop/images/16.gif","gname":"裸戏一夜成名的女星","link":"http://www.xiumei.com/a/20110805/36409.shtml"},
- {"pic":"/pop/images/17.gif","gname":"色@情业觊觎的女星","link":"http://www.xiumei.com/a/20110813/36790.shtml"},
- {"pic":"/pop/images/18.gif","gname":"Charming性感牛仔写真","link":"http://www.xiumei.com/a/20110715/35181.shtml"}]);
随机数组 html放在方法里面写html的 多行 效果比较自然
- <script type="text/javascript">
- function setGameList(s, e, num) {
- var arr = new Array();
- for (i = s; i < e; i++) {
- arr[i] = i;
- }
- var temp_array = new Array();
- for (var index in arr) {
- temp_array.push(arr[index]);
- }
- var return_array = new Array();
- for (var i = 0; i < num; i++) {
- if (temp_array.length > 0) {
- var arrIndex = Math.floor(Math.random() * temp_array.length);
- return_array[i] = temp_array[arrIndex];
- temp_array.splice(arrIndex, 1);
- } else {
- break;
- }
- }
- var html = '';
- for (var i = 0; i < return_array.length; i++) {
- var j = return_array[i];
- html += '<li><a href="' + jsonarr[j].link + '" title="' + jsonarr[j].gname + '" target="_blank"><img src="' + jsonarr[j].pic + '"/><h3>' + jsonarr[j].gname + '</h3></a></li>';
- }
- $('#div_gamelist').append(html);
- }
- setGameList(0, 18, 15);
- </script>
实例
方式3,随机改变图片的地址和链接的地址,页面的排版不改变
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf8">
- <base href="http://hao.kkpk.com/"/>
- <title>玩游戏送大奖!!</title>
- <style>
- .index_top:link {
- font-family: "宋体";
- color: #FFFFFF;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- text-decoration: none;
- }
- .index_top:visited {
- font-family: "宋体";
- color: #FFFFFF;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- text-decoration: none;
- }
- .index_top:hover {
- font-family: "宋体";
- color: #FFFFFF;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top:active {
- font-family: "宋体";
- color: #FFFFFF;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top1:link {
- font-family: "宋体";
- color: #FFFFFF;
- text-decoration: none;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top1:visited {
- font-family: "宋体";
- color: #FFFFFF;
- text-decoration: none;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top1:hover {
- font-family: "宋体";
- color: #FFFFFF;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top1:active {
- font-family: "宋体";
- color: #FFFFFF;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top2:link {
- font-family: "宋体";
- color: #FF5A00;
- text-decoration: none;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top2:visited {
- font-family: "宋体";
- color: #FF5A00;
- text-decoration: none;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top2:hover {
- font-family: "宋体";
- color: #FF5A00;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top2:active {
- font-family: "宋体";
- color: #FF5A00;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top3:link {
- font-family: "宋体";
- color: #666666;
- text-decoration: none;
- font-size: 12px;
- font-weight: bold;
- line-height: 18px;
- }
- .index_top3:visited {
- font-family: "宋体";
- color: #666666;
- text-decoration: none;
- font-size: 12px;
- font-weight: bold;
- line-height: 18px;
- }
- .index_top3:hover {
- font-family: "宋体";
- color: #666666;
- text-decoration: underline;
- font-size: 12px;
- font-weight: bold;
- line-height: 18px;
- }
- .index_top3:active {
- font-family: "宋体";
- color: #666666;
- text-decoration: underline;
- font-size: 12px;
- font-weight: bold;
- line-height: 18px;
- }
- .index_top4:link {
- font-family: "宋体";
- color: #666666;
- text-decoration: none;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top4:visited {
- font-family: "宋体";
- color: #666666;
- text-decoration: none;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top4:hover {
- font-family: "宋体";
- color: #666666;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- .index_top4:active {
- font-family: "宋体";
- color: #666666;
- text-decoration: underline;
- font-size: 12px;
- font-weight: normal;
- line-height: 18px;
- }
- body {
- margin-left: 0px;
- margin-top: 0px;
- margin-right: 0px;
- margin-bottom: 0px;
- }
- .style4 {font-size: 12px; color: #FF6600; }
- .style5 {color: #666666; font-size: 14px;}
- .style7 {color: #FFFFFF; font-size: 14px; }
- </style>
- <script src="jquery.js"></script>
- <script src="list.js"></script>
- <script>
- var adsize='270x200';
- location.href.replace(/\/(\d+)-(\d+)\./,function($1,$2,$3){adsize=$2+'x'+$3;});
- jQuery(document).ready(function(){
- var pic = jQuery('a img');
- var link = jQuery('a');
- var num = jsonarr.length;
- var index = Math.floor(Math.random()*num);
- link.each(function(){
- if(jQuery('img',jQuery(this)).length == 0){
- var obj = jsonarr[index%num];
- jQuery(this).attr('href',obj.url+'&FromWebId=[wid]&AdSize='+adsize); //linkurl
- index++;
- }
- })
- pic.each(function(){
- var obj = jsonarr[index%num];
- jQuery(this).attr('src',obj.pic); //image url
- jQuery(this).parent().attr('href',obj.url+'&FromWebId=[wid]&AdSize='+adsize);//linkurl
- index++;
- })
- })
- </script>
- </head>
- <body>
- <table width="270" height="200" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td valign="top" bgcolor="#D7F1C2"><table width="100%" border="0" cellspacing="1" cellpadding="1">
- <tr>
- <td><div align="center" class="index_top1"><a href="http://bbs.265g.com/tg/789/?FromWebId=%5Bwid%5D" target="_blank" class="index_top3">265游戏特辑 更多惊喜等着您</a></div></td>
- </tr>
- <tr>
- <td height="177" valign="top" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td height="3"></td>
- </tr>
- <tr>
- <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/4.gif" height="75" border="0"></a></div></td>
- <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/8.gif" height="75" border="0"></a></div></td>
- </tr>
- <tr>
- <td height="20"><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank" class="index_top4"> 丝袜MM全程陪玩哟</a></div></td>
- <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank" class="index_top4">挑战你的视觉极限</a></div></td>
- </tr>
- <tr>
- <td height="13"><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/6.gif" height="75" border="0"></a></div></td>
- <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/7.gif" height="75" border="0"></a></div></td>
- </tr>
- <tr>
- <td height="2"></td>
- </tr>
- </table></td>
- </tr>
- </table></td>
- </tr>
- </table>
- </body>
- </html>
效果如下