我寫了一個代碼來改變背景,因爲每天的時間。如果我將它應用於整個身體,它的工作正常,但是當我試圖將它放入一個div中時,它不起作用。請參閱我的代碼並指導我解決錯誤。document.elementbyid函數不能在div中工作
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<style>
/*reset*/ h1,h4 {margin:0;}
/* basic styles */
h1 { margin-bottom: 10px; }
div {
width: 680px;
padding: 10px 25px;
margin: 50px auto;
border-radius: 7px;
background: rgba(255, 255, 255, 0.4);
color: #1F1F1F;
}
/* backgrounds */
.day { background: url('http://imgs.mi9.com/uploads/photography/4480/white-clouds-and- blue-sky_1600x1200_78556.jpg'); }
.sunset { background: url('http://www.naturewallpaper.eu/desktopwallpapers/sky/1366x768/after-sunset-sky- 1366x768.jpg'); }
.night { background: url('http://images2.layoutsparks.com/1/159946/moon-girl-night- sky.jpg'); }
#ship{width:60%; height:100px; border:#30C 1px solid;}
</style>
</head>
<body>
<div id="ship">
<script>
$(document).ready(function(){
var d = new Date();
var n = d.getHours();
if (n > 19 || n < 6)
// If time is after 7PM or before 6AM, apply night theme to ‘body’
document.elementById('ship').style.backgroundImage.className = "nighta";
else if (n > 16 && n < 19)
// If time is between 4PM – 7PM sunset theme to ‘body’
document.elementById('ship').style.backgroundImage.className = "sunset";
else
// Else use ‘day’ theme
document.elementById('ship').style.backgroundImage.className = "day";
});
</script>
</div>
</body>
</html>
使用'document.getElementById'不'document.elementById' –
爲什麼不使用jQuery庫,因爲它是故意的嗎? '$(「#ship」)' –
_什麼是錯誤?此外,這不是「引導你度過錯誤」的地方。你需要一個會話場所,比如聊天室或討論區。堆棧溢出是一個問答,更像是一個常見問題解答。 –