2015-12-19 87 views
2

有人可以讓我知道下面的JavaScript有什麼問題。它假設顯示時間和日期,但只顯示HTML標題。我從一本javascript書中獲得了這本書,用於學習,我認爲它現在可能會變老,而事情可能已經改變,或者我犯了一個錯誤。第一次需要javascript幫助

<html> 
 

 
<head><title>Date time</title></head> 
 

 
<body> 
 
<h1> Time and Date </h1> 
 

 
<script LANGUAGE="JavaScript" type="textjavascript"> 
 
\t now = new Date(); 
 
\t localtime = now.toString(); 
 
\t utctime = now.toGMTString(); 
 
\t document.write ("<b> local time: </b>" + localtime + "<BR>"); 
 
\t document.write ("<b> UTC time: </b>" + utctime +); 
 
\t 
 
\t document.write("Hello World!"); 
 
</script> 
 

 
</body> 
 
</html>

感謝您的幫助

+0

試試這個:'now = new Date(); localtime = now.toString(); utctime = now.toGMTString(); document.write(「當地時間:」+ localtime +「
」); 文件撰寫( 「UTC時間: 」+ utctime);'' – Rayon

+0

文件撰寫(「 UTC時間:」 + utctime +);',有一個不必要的'+'晃來晃去這裏 – Ramanlfc

+0

這並不工作無論是。我看不出你寫的東西和我已有的東西有什麼不同。我將文檔保存爲.html並上傳到我的服務器,我認爲這是正確的。 – user2669997

回答

3

問題#1。這是行不通的,因爲腳本塊不會因爲無效的類型屬性的認定爲JS:

type="textjavascript" 

應該type="text/javascript"

問題2:。一旦你解決這個問題,一定要解決這個問題太行:

+ utctime +); 
     //^---- remove this "+" 

問題3:。扔掉你讀的書。它完全過時了。原因:

  • 不使用document.write - 它用於非常特殊的情況下,你不是其中之一。
  • 不指定LANGUAGE="JavaScript"type="text/javascript"它們是多餘的。

最後,瞭解像document.querySelectorinsertAdjacentHTMLappendChildcreateTextNodecreateElement等,其中還有不少的DOM方法。

你可以重寫你的榜樣在多個方面,例如:

<html> 
 
    
 
    <head><title>Date time</title></head> 
 
    
 
    <body> 
 
     <h1> Time and Date </h1> 
 
     <div class="date"></div> 
 
    
 
    <script> 
 
    \t var now = new Date(); 
 
    \t var localtime = now.toString(); 
 
    \t var utctime = now.toGMTString(); 
 
    \t 
 
    \t var container = document.querySelector('.date'); 
 
    \t 
 
    \t container.innerHTML = 
 
    \t  "<b> local time: </b>" + localtime + "<BR>" + 
 
    \t  "<b> UTC time: </b>" + utctime; 
 
    \t  
 
     document.body.appendChild(document.createTextNode("Hello World!")); 
 
    </script> 
 
    
 
    </body> 
 
</html>

0

第一個變化,<腳本類型= 「文/ JavaScript的」>,然後

只是刪除在document.write中的utctime之後的'+',它會起作用,那個變量沒有可以追加的字符串,所以不需要'+'。

now = new Date(); 
localtime = now.toString(); 
utctime = now.toGMTString(); 
document.write ("<b> local time: </b>" + localtime + "<BR>"); 
document.write ("<b> UTC time: </b>" + utctime); 

document.write("Hello World!"); 

這會奏效。

0

您有問題在下面的行。

document.write ("<b> UTC time: </b>" + utctime +); 

YOu最後加了額外的「+」。

SO適當的工作代碼應該如下。

https://jsfiddle.net/tdc6dkn6/1/

0

在下面的語句的結束只是刪除的+號:

document.write ("<b> UTC time: </b>" + utctime +); 

它應該是:

document.write ("<b> UTC time: </b>" + utctime); 
1

有您的代碼2級的錯誤:

  • The MIME類型應設置爲text/javascript,而不是textjavascript
  • 有語法錯誤"<b> UTC time: </b>" + utctime + < - 刪除尾隨+
+0

感謝Chris,它是textjavascript中的/ missing。我以爲我用細牙梳去了這個,但仍然錯過了這個。 – user2669997

0
<html> 

<head><title>Date time</title></head> 

<body> 
<h1> Time and Date </h1> 

<script> 
    var now = new Date(); 
    var localtime = now.toString(); 
    var utctime = now.toGMTString(); 
    document.write ("<b> local time: </b>" + localtime + "<BR>"); 
    document.write ("<b> UTC time: </b>" + utctime); 

    document.write("Hello World!"); 
</script> 

0

你必須在代碼冗餘+。此外,在type中也有錯誤。看一下這個。

<script type="text/javascript"> 
    now = new Date(); 
    localtime = now.toString(); 
    utctime = now.toGMTString(); 
    document.write ("<b> local time: </b>" + localtime + "<br>"); 
    document.write ("<b> UTC time: </b>" + utctime); 

    document.write("Hello World!"); 
</script> 
0

變化type="textjavascript"type="text/javascript",並採取了在document.write ("<b> UTC time: </b>" + utctime +);額外+