簡單的:Div隨機顏色

#設計的內容:dom

1.原生DOM節點的獲取.
2.Math.random()的熟練運用.
3.字符串的拼接.
複製代碼
var div = document.querySelector('div');
        var r = Math.floor(Math.randon() * 255);
        var g = Math.floor(Math.random() * 255);
        var b = Math.floor(Math.random() * 255);
        div.style.background = "rgb(" + r + ',' + g + ',' + b + ")";
        
        **注意**
            Math.floor()   向下取整
            Math.random()   0~1之間的小數(0是能夠取到的【官方說法】,可是一直沒有取到過)
            另外要注意字符串的拼接不要出錯.複製代碼
相關文章
相關標籤/搜索