[英]What's wrong with this javascript?
我正在使用javascript根據時間切換表。 但我在DW中遇到錯誤。
<script type="text/JavaScript">
<!--
function changeWebsite() {
var currentTime = new Date().getHours();
if (7 <= currentTime&¤tTime < 18) {
document.write(' <table id="Table_01" width="200" height="400" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_VC01.png" width="200" height="45" alt=""></td>
</tr>
<tr>
<td>
<a href="http://www.itsnotch.com"
onmouseover="window.status='Visit My Biography Website'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_VC02.png" width="200" height="75" border="0" alt="ItsNotch.com"></a></td>
</tr>
<tr>
<td>
<a href="http://www.notchtheguru.com"
onmouseover="window.status='My Tumblr'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/NotchTheGuru.comVC.png" width="200" height="119" border="0" alt="Tumblr"></a></td>
</tr>
<tr>
<td>
<a href="http://www.bignotch.com"
onmouseover="window.status='Welcome to My Music Website'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_VC04.png" width="200" height="161" border="0" alt="NotchTheGuru.com"></a></td>
</tr>
</table>
<br>
');
}
else {
document.write(' <table id="Table_01" width="200" height="400" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_01.png" width="200" height="45" alt=""></td>
</tr>
<tr>
<td>
<a href="http://www.itsnotch.com"
onmouseover="window.status='Visit My Biography Website'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_02.png" width="200" height="75" border="0" alt="ItsNotch.com"></a></td>
</tr>
<tr>
<td>
<a href="http://www.notchtheguru.com"
onmouseover="window.status='My Tumblr'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/NotchTheGuru.com.png" width="200" height="119" border="0" alt="Tumblr"></a></td>
</tr>
<tr>
<td>
<a href="http://www.bignotch.com"
onmouseover="window.status='Welcome to My Music Website'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_04.png" width="200" height="161" border="0" alt="NotchTheGuru.com"></a></td>
</tr>
</table>
<br>
');
}
}
changeWebsite();
-->
錯誤在第7行'document.write('
你在字符串里面有未轉義的單引號(用單引號分隔)。
例如,您必須更改:
onmouseover="window.status='Visit My Biography Website';
至:
onmouseover="window.status=\\'Visit My Biography Website\\';
有關詳細信息,請通過JSLint運行代碼。
警告:JSLint會傷害你的感情。
你不應該從你的JavaScript編寫這么多的HTML代碼。 如果你經常這樣做,你的代碼很快就會變得難以理解,而且這也是一場噩夢般的表現。
您編寫的幾乎所有HTML代碼都是相同的,您最好這樣做:
HTML代碼:
<table id="Table_01" width="200" height="400" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_01.png" width="200" height="45" alt="" id="image1"></td>
</tr>
<tr>
<td>
<a href="http://www.itsnotch.com"
onmouseover="window.status='Visit My Biography Website'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_02.png" width="200" height="75" border="0" alt="ItsNotch.com" id="image2"></a></td>
</tr>
<tr>
<td>
<a href="http://www.notchtheguru.com"
onmouseover="window.status='My Tumblr'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/NotchTheGuru.com.png" width="200" height="119" border="0" alt="Tumblr" id="image3"></a></td>
</tr>
<tr>
<td>
<a href="http://www.bignotch.com"
onmouseover="window.status='Welcome to My Music Website'; return true;"
onmouseout="window.status=''; return true;">
<img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_04.png" width="200" height="161" border="0" alt="NotchTheGuru.com" id="image4"></a></td>
</tr>
</table>
<br>
Javascript代碼:
<script type="text/JavaScript">
function changeWebsite() {
var currentTime = new Date().getHours();
if (7 <= currentTime&¤tTime < 18) {
var baseURL = "http://itsnotch.com/tumblr/images/";
document.getElementById("image1").src = baseURL + "websitelist_tumblr_VC01.png";
document.getElementById("image2").src = baseURL + "websitelist_tumblr_VC02.png";
document.getElementById("image3").src = baseURL + "NotchTheGuru.comVC.png";
document.getElementById("image4").src = baseURL + "websitelist_tumblr_VC04.png";
}
changeWebsite()
</script>
作為一個小建議,當有人將mouseover
在您的鏈接上時,您不應該自己更改狀態欄,這真的會讓一些人失望,例如我。
你混合單引號和雙引號,有錯誤,你需要逃避它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.