<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • js如何動態(tài)批量設置隨機顏色

    程序猿 2021-03-14 18:37:32 2508瀏覽 加載中

    費話不多說,直接上代碼,其它自己腦補。

    // html代碼 有一堆的span
    <span class="me">1</span><span class="me">2</span><span class="me">3</span>
    <span class="me">4</span><span class="me">5</span><span class="me">9</span>
    <span class="me">6</span><span class="me">7</span><span class="me">8</span>

    使用JS給上面的span設置隨機顏色

    $(".me").each(function (){
        $(this).attr("style","color:" + getColor());
        console.log(getColor());
    })
    // 返回隨機的RGB顏色
    function getColor() {
        let r = Math.floor(Math.random() * 256)
        let g = Math.floor(Math.random() * 256)
        let b = Math.floor(Math.random() * 256)
        return "rgb("+r+","+g+","+b+")";
    
    }

    趕緊去測試下吧。

    最后修改:2025-09-13 04:43:57

    非特殊說明,本博所有文章均為博主原創(chuàng)。

    主站蜘蛛池模板: 泰来县| 平乡县| 新乡市| 白朗县| 宁阳县| 白水县| 保康县| 自治县| 隆子县| 雷山县| 会理县| 交城县| 中阳县| 凭祥市| 梅河口市| 天水市| 安陆市| 双流县| 泰州市| 河东区| 海城市| 耒阳市| 宝坻区| 通榆县| 钦州市| 巩留县| 南宁市| 阳泉市| 闻喜县| 乌拉特前旗| 望奎县| 富源县| 仙游县| 吉水县| 夏邑县| 重庆市| 盐城市| 龙胜| 灵丘县| 商水县| 庄浪县|