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

          常用鏈接

          統(tǒng)計(jì)

          IT技術(shù)鏈接

          保險(xiǎn)相關(guān)

          友情鏈接

          基金知識(shí)

          生活相關(guān)

          最新評(píng)論

          JScript中的隨機(jī)顏色

          一,其實(shí)隨機(jī)色就是RGB分別取三個(gè)0-256的隨機(jī)數(shù),
          然后把你想要改的對(duì)象的樣式設(shè)置成這種顏色就行了。

          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()函數(shù),定時(shí)給某一個(gè)控件(如:span)的更換字體顏色樣式。
          在javascript中,element.style.color屬性允許使用rgb(x,y,z)函數(shù),這樣我們就可以生成三個(gè)隨機(jī)數(shù)填充,以達(dá)到隨機(jī) ??? 變顏色的效果。我們可以用Math.random()來(lái)獲得隨機(jī)數(shù),然后再利用Math.round()來(lái)得到一個(gè)小于當(dāng)前值的最大整數(shù)。
          RGB的值的隨機(jī)范圍是0-255,所以每個(gè)隨機(jī)值是: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 鴻雁 閱讀(412) 評(píng)論(0)  編輯  收藏

          主站蜘蛛池模板: 新干县| 会泽县| 玛多县| 沙雅县| 清苑县| 化隆| 乌鲁木齐县| 乌鲁木齐市| 洮南市| 巨野县| 尼勒克县| 桓台县| 西城区| 怀远县| 凤凰县| 绵阳市| 合水县| 香格里拉县| 厦门市| 通榆县| 霍林郭勒市| 沐川县| 怀化市| 三门县| 汉川市| 双鸭山市| 宁化县| 义乌市| 石景山区| 乐都县| 高密市| 乐东| 浦县| 和平区| 兴化市| 夹江县| 巴青县| 阿城市| 常宁市| 甘孜县| 山阳县|