$(function(){ var wx_arr = ['gb3055','gb6365']; var storage = window.localStorage; function random_wx(up,low) { //Math.random()*(上限-下限+1)+下限 var i = parseInt(Math.random() * (up - low + 1) + low); return i; } function check_random(){ var random_num = random_wx(wx_arr.length-1,0); if(typeof storage["cur_wx"] !== undefined){ var store_num = parseInt(storage.getItem("cur_wx")); if(store_num == random_num){ return check_random(); }else{ return random_num; } }else{ return random_num; } } if(!window.localStorage){ console.log("浏览器支持localstorage"); var rand_num_1 = random_wx(wx_arr.length-1,0); $(".wx_num").html(wx_arr[rand_num_1]); }else{ var rand_num = check_random(); storage.setItem("cur_wx", rand_num); $(".wx_num").html(wx_arr[rand_num]); } });
第二种:随机数生成
if (!document.getElementsByClassName) { document.getElementsByClassName = function (className, element) {
var children = (element || document).getElementsByTagName('*'); var elements = new Array(); for (var i = 0; i < children.length; i++) { var child = children[i]; var classNames = child.className.split(' '); for (var j = 0; j < classNames.length; j++) { if (classNames[j] == className) { elements.push(child);
break; } } } return elements; }; }
var Arr =["微信1","微信2","微信3","微信4","微信5","微信6","微信7","微信8","微信9","微信10"]; var n = Math.floor(Math.random() * Arr.length); if(Arr[n]){ for(i=0;i<20;i++){ document.getElementsByClassName('weixinid')[i].innerHTML = Arr[n]; } }