我正在建立一個隨機背景顏色選擇器只是爲了好玩。隨機顏色選擇顯示十六進制
很簡單..當你點擊一個按鈕頁面刷新,並從幾種不同顏色的數組中選擇背景顏色。
現在我想要做的是我想顯示在純html中的當前顏色的十六進制,我真的是新的JS,所以我不知道如何做到這一點。任何人都可以幫我嗎?
這裏是我的代碼:
<script>
var bgcolorlist=new Array("#F70000","#B9264F","#990099","#74138C","#0000CE","#1F88A7","#4A9586","#FF2626","#D73E68","#B300B3","#8D18AB","#5B5BFF","#25A0C5","#5EAE9E","#FF5353","#DD597D","#CA00CA","#A41CC6","#7373FF","#29AFD6","#74BAAC","#FF7373","#E37795","#D900D9","#BA21E0","#8282FF","#4FBDDD","#8DC7BB","#FF8E8E","#E994AB","#FF2DFF","#CB59E8","#9191FF","#67C7E2","#A5D3CA","#FFA4A4","#EDA9BC","#F206FF","#CB59E8","#A8A8FF","#8ED6EA","#C0E0DA","#FFB5B5","#F0B9C8","#FF7DFF","#D881ED","#B7B7FF","#A6DEEE","#CFE7E2","#FFC8C8","#F4CAD6","#FFA8FF","#EFCDF8","#C6C6FF","#C0E7F3","#DCEDEA","#FFEAEA","#F8DAE2","#FFC4FF","#EFCDF8","#DBDBFF","#D8F0F8","#E7F3F1")
document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]
</script>
<form><input type=button value="click here forever" onClick="window.location.reload()"></form>
<center>
<p>This colour is: </p>
</div>
呃......新的Array()??... JS 101:絕對不要使用'new'來創建原始對象,除了'Date'。如果你做'新布爾',你將很難搞清楚你做錯了什麼。 –