一、array_rand()javascript
php代碼段:php
//在一個網站添加多個客服qq,要求用戶點擊按鈕隨機爲這個用戶匹配一個qq號 $qq = array('784561652','15479623','2258694','46479721','201689745'); $smarty->assign('qq', array_rand($qq)); $smarty->display('index.tpl');
index.tpl代碼段:html
<a href="http://wpa.qq.com/msgrd?v=3&uin={$qq}&site=qq&menu=yes" target="_blank"> <img src="/images/customer.jpg" height="36" width="36" alt=""> </a>
------------------------------------------------------------------------------------------------------------------------------------------java
二、使用JavaScript的 Math.round(Math.random()*10) 方法web
php代碼段:數組
//在一個網站添加多個客服qq,要求用戶點擊按鈕隨機爲這個用戶匹配一個qq號 $qq = array('784561652','15479623','2258694','46479721','201689745'); $smarty->assign('qq', $qq); $smarty->display('index.tpl');
index.tpl代碼段:dom
<a href="http://wpa.qq.com/msgrd?v=3&uin={$qq}&site=qq&menu=yes" target="_blank"> <img src="/images/customer.jpg" height="36" width="36" alt=""> </a>
<script type="text/javascript"> arr_qq = new Array(); {foreach $qq as $key => $item} arr_qq[] = $item {/foreach} var v = Math.round(Math.random()*10)%4 //這裏的4是上面數組裏qq的個數 var qq = arr_qq[v]; $("a").click(function(){ $(this).attr('href', 'http://wpa.qq.com/msgrd?v=3&uin='+ qq +'&site=qq&menu=yes'); }); </script>