解决方案 »

  1.   

    主体代码没有问题,调用的时候有问题。
    demo here.<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>test</title>
    </head>
    <body>
    <div id="div1">我是单独的div</div>
    <div>【埃森哲(中国)有限公司】Java高级开发工程师(电商项目)</div>
    <div>【上海贝锐信息科技有限公司】web测试工程师</div>
    <div>【en-japan(en申才)】Android & iOS攻城狮子和狮王</div>
    <div>【南宁数海网络公司】广西南宁招C#.net程序员【安投融(北京)网络科技有限公司】文案</div>
    <script type="text/javascript">
        function randomVal(val) {
            return Math.floor(Math.random() * (val + 1));
        }    function randomcolor() {
            var r = randomVal(255).toString(16);
            if (r.length < 2) r = "0" + r;
            var g = randomVal(255).toString(16);
            if (g.length < 2) g = "0" + g;
            var b = randomVal(255).toString(16);
            if (b.length < 2) b = "0" + b;
            return "#" + r + g + b;
        }    document.getElementById('div1').style.color = randomcolor();
        var div = document.getElementsByTagName('div')
        for(var i=0; i < div.length; i++){
            div[i].style.color = randomcolor();
        }</script>
    </body>
    </html>
      

  2.   

    你给font设个id,在事件(比如初始化)中,对这个id赋颜色。
      

  3.   

    <font color="randomcolor()">aaaaa</font>因為 randomcolor()  不是色碼
      

  4.   

    <script>
    function randomcolor(){
      return 'rgb(' + (Math.random()*256) + ',' + (Math.random()*256) + ',' +(Math.random()*256) + ')';
    }
    window.onload = function() {
      document.body.style.color = innerHTML = randomcolor();
    }</script>
    <body>
    小弟想给文字产生随机的颜色,在外面调用函数,但是没有效果啊,去掉randomcolor()的引号也不行,
    请问为什么?<br>
    这是因为 js 代码并没有被执行
    </body>
      

  5.   

    也可以<script>
    function randomVal(val) {
      var c = '00' + Math.floor(Math.random() * (val + 1)).toString(16);
      return c.replace(/.+(..)$/, '$1');
    }
     
    function randomcolor() {
      return '#' + randomVal(255) + randomVal(255) + randomVal(255);
    }
     
    window.onload = function() {
      document.body.innerHTML = document.body.innerHTML.fontcolor(randomcolor());
    }</script>
    <body>
    小弟想给文字产生随机的颜色,在外面调用函数,但是没有效果啊,去掉randomcolor()的引号也不行,
    请问为什么?<br>
    这是因为 js 代码并没有被执行
    </body>
      

  6.   

    产生随机颜色也可以这样写;
     function getColor(){
         return '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6);
        }
      

  7.   


    这个要把.style.color=变成.style.background-color=... 为什么就不对了呢?
      

  8.   


    这个要把.style.color=变成.style.background-color=... 为什么就不对了呢?

    style.backgroundColor
    中间有线的后一个字母一律要换成大写字母,比如fontSize, fontWeight。
      

  9.   

    rgb(r,g,b)
    是 css 颜色表示之一
    浏览器都支持的