文章摘要:在做tag标签云的时候会用到不同颜色显示,怎么用js实现标签随机颜色呢?模板社教大家一种好的js方法:
在做tag标签云的时候会用到不同颜色显示,怎么用js实现标签随机颜色呢?

模板社教大家一种好的js方法:
代码如下:
- <script type="text/javascript">
- $(function () {
- var obox=document.getElementById("tags");
- var obj=obox.getElementsByTagName("a");
- //随机方法
- function rand(num){
- return parseInt(Math.random()*num+1);
- }
- //随机颜色值
- function randomcolor(){
- var str=Math.ceil(Math.random()*16777215).toString(16);
- if(str.length<6){
- str="0"+str;
- }
- return str;
- }
- //循环
- for( len=obj.length,i=len;i--;){
- obj[i].className="color"+rand(5);
- obj[i].style.fontSize=rand(12)+12+"px";
- // obj[i].style.background="#"+randomcolor();
- obj[i].style.color="#"+randomcolor();
- obj[i].onmouseover=function(){
- this.style.background="#"+randomcolor();
- }
- obj[i].onmouseout=function(){
- this.style.background="none";
- }
- }
- });
- </script>

