The important thing in life is to have a great aim , and the determination

常用链接

统计

IT技术链接

保险相关

友情链接

基金知识

生活相关

最新评论

JScript中的随机颜色

一,其实随机色就是RGB分别取三个0-256的随机数,
然后把你想要改的对象的样式设置成这种颜色就行了。

var  rndColor  =   " rgb( "   +  Math.round(Math.random() * 256 +   " , "   +  Math.round(Math.random() * 256 +   " , "   +  Math.round(Math.random() * 256 +   " ) " ;
document.getElementById(
" myData " ).style.color  =  rndColor;

二,利用javascript中的setInterval()函数,定时给某一个控件(如:span)的更换字体颜色样式。
在javascript中,element.style.color属性允许使用rgb(x,y,z)函数,这样我们就可以生成三个随机数填充,以达到随机     变颜色的效果。我们可以用Math.random()来获得随机数,然后再利用Math.round()来得到一个小于当前值的最大整数。
RGB的值的随机范围是0-255,所以每个随机值是:Math.round(Math.random()*256)。 

<span id="myData">Hello world</span>
<script language="javascript">
window.onload = function(){
 setInterval( rndMyData, 1000 );
}
function rndMyData(){
 var rndColor = "rgb(" + Math.round(Math.random()*256) +  "," + Math.round(Math.random()*256) + "," + Math.round(Math.random()*256) + ")";
 document.getElementById( "myData" ).style.color = rndColor;
}
</script>


   

posted on 2007-03-23 10:50 鸿雁 阅读(405) 评论(0)  编辑  收藏