<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 2303瀏覽 加載中

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

    // 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-05-22 03:22:07

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

    主站蜘蛛池模板: 高密市| 辉南县| 灵丘县| 佛学| 阳泉市| 忻城县| 宁蒗| 香河县| 林西县| 额济纳旗| 西吉县| 枝江市| 广德县| 民勤县| 公主岭市| 乌拉特前旗| 太白县| 越西县| 环江| 临澧县| 万载县| 通州市| 葫芦岛市| 山东| 安仁县| 徐州市| 湾仔区| 宜黄县| 望城县| 略阳县| 专栏| 太仆寺旗| 临清市| 苍梧县| 陇西县| 香河县| 安阳县| 平远县| 湘潭市| 涡阳县| 陇川县|