所以我有一個實踐的檢驗,要求我改變與黑色背景和白色文字標識「第四」的一段段的背景顏色和文字顏色反之亦然和反向每最好使用if/else語句30秒。但由於某些原因,我的if語句(也可能是我的else語句)不工作。使用JavaScript的HTML元素的替代背景顏色
代碼我到目前爲止是這樣的:
HTML
<html>
<head>
<link href="teststyle.css" rel="stylesheet" type="text/css"/>
<script src="flash.js" type="text/javascript"></script>
</head>
<body>
<div id="first">
mares eat oats
</div>
<h1 id="second">
and does eat oats
</h1>
<p id="third">
and little lambs eat ivy
</p>
<p id="fourth">
Mirthipan Karunakaran
</p>
</body>
</html>
CSS
#first {
text-align: center;
}
#second {
color: green;
text-align: left;
}
#third {
color: orange;
text-align: right;
}
#fourth {
color: white;
background-color: black;
}
JAVASCRIPT
function updatePage(){
var name = document.getElementById("fourth");
if(name.style.backgroundColor == "black") {
name.style.backgroundColor = "pink";
} else {
name.style.backgroundColor = "purple";
}
}
function startUpdate(){
updatePage();
window.setInterval(updatePage,10 * 1000);
}
window.onload=startUpdate;
我怎麼會去使這項工作?這裏
'console.log(name.style.backgroundColor)' – epascarello