因此,總體來說,我試圖在有人在他們身上放置鼠標時使盒子變色。顏色必須是隨機的。我知道我錯過了我的功能之間的連接點,但我不知道它是什麼。隨機顏色爲<tr>
<!DOCTYPE html>
<html onmousedown='event.preventDefault();'
onmouseenter = "colorize();"
>
<head>
<title> Boxes </title>
<meta charset='utf-8'>
<style>
table {
border-spacing: 6px;
border: 1px rgb(#CCC);
margin-top: .5in;
margin-left: 1in;
}
td {
width: 40px; height: 40px;
border: 1px solid black;
cursor: pointer;
}
</style>
<script>
創建傳遞的元對象作爲其 參數,並設置使用 RGB(R,G,B)方法設定每個R,G和B的元素的背景顏色樣式屬性的函數稱爲彩色化)隨機數介於0和255
function colorize() {
var
r = ('0'+(Math.random()*255|0).toString(16)).slice(-2),
g = ('0'+(Math.random()*255|0).toString(16)).slice(-2),
b = ('0'+(Math.random()*255|0).toString(16)).slice(-2);
return '#' +r+g+b;
}
function colorize(co) {
document.body.style.background = co;
}
</script>
</head>
<body>
<table>
<tbody>
<script type="text/javascript">
使用文件撰寫(和for循環填寫表中創建一個16×16盒表。對於每個td元素,創建一個onmouseenter調用來着色,並將其傳遞給元素本身(this)。
var row = 16;
var cols = 16;
for(var r=0;r<row;r++){
document.write("</tr>");
for(var c=0;c<cols;c++){
document.write("<td></td>");
}
document.write("</tr>");
}
</script>
</tbody>
</table>
</body>
</html>
請解決您的格式使你的代碼是清晰的。 – Carcigenicate
希望能幫到你! – HTMLnoobcs17001
如果您的文字字體顏色與背景顏色相同,該怎麼辦? – TeaCode