3 回答

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
白色矩形是您的身體。如果你添加你的 css body 類,background-color:black;你可以看到它。關(guān)于 getElementsByTagName 的其他答案也是正確的。
嘗試這個(gè):
function myFunction() {
document.getElementById("demo").style.color = "red";
}
function spookFunction() {
document.getElementsByTagName("body")[0].style.color="orange";
document.getElementsByTagName("body")[0].style.fontFamily="Bangers, cursive";
document.getElementById("welcome").style.color="red"
}
html {
background-image: linear-gradient(#0099ff, white);
min-height: 100%;
}
body {
margin-left: 10%;
margin-right: 10%;
margin-top: 10%;
font-family: 'Lobster', cursive;
color: white;
background-color: black;
}
h1{ font-size: 50px; }
p {
font-size: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spook My Page</title>
<link rel="stylesheet" type="text/css" href="Style.css">
<link href="https://fonts.googleapis.com/css2?family=Bangers&family=Lobster&display=swap"
rel="stylesheet">
</head>
<body>
<h1 id="welcome">Welcome Friend!</h1>
<p>To change the color scheme — Click the button below!</p>
<p id="demo" onclick="myFunction()">Click me to change my text color.</p>
<button type="button" onclick="spookFunction()">Click Me!
</button>
</body>
</html>

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
getElementsByTagName 返回?cái)?shù)組而不是單個(gè)元素。因此,您將必須訪問像這樣的元素 getElementsByTagName("body")[0]。希望這會(huì)有所幫助。

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
function spookFunction() {
document.getElementsByTagName("BODY")[0].style.backgroundColor="orange";
document.getElementsByTagName("BODY")[0].style.fontFamily="Bangers,cursive";
document.getElementById("welcome").style.backgroundColor="red"}
這應(yīng)該可以完成工作,您為 backgroundColor 使用了錯(cuò)誤的變量名稱并且找不到正文,因?yàn)?document.getElementsByTagName("BODY") 返回了一個(gè)標(biāo)簽數(shù)組
添加回答
舉報(bào)