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

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

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

    // 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-07-10 01:17:23

    非特殊說明,本博所有文章均為博主原創。

    主站蜘蛛池模板: 云霄县| 旺苍县| 澳门| 亳州市| 昭觉县| 荃湾区| 广灵县| 临城县| 交城县| 资阳市| 赣州市| 芮城县| 昆山市| 荔波县| 山阴县| 连江县| 上栗县| 龙游县| 韶关市| 策勒县| 名山县| 西和县| 新绛县| 辽阳县| 乃东县| 阿拉善右旗| 宜阳县| 闸北区| 邳州市| 峨山| 新丰县| 图木舒克市| 大冶市| 宣威市| 卓尼县| 措勤县| 田阳县| 贡觉县| 云梦县| 玉龙| 吴江市|