2014-12-06 84 views
0

我需要使用XHTML 1.0 Transitional作爲我的任務任務的一部分來簡單使用confirm()和prompt()函數。所有代碼都需要驗證。document.write XHTML 1.0過渡驗證錯誤

當我嘗試驗證我收到以下錯誤代碼:

12行,列24:文檔類型不允許元素「P」這裏

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>java script demonstration</title> 
    </head> 
    <body> 
     <script type="text/javascript"> 
        var response=confirm("This box was created using Javascript - Click on OK to continue") 
      if (response) 
       {document.write("<p>You have just clicked on OK</p>")} 
      else 
       {document.write("You have just clicked on Cancel")}  
        var reply=prompt("Please enter your Name") 
      document.write("your name is " + reply +"") 
     </script> 
    </body> 
</html> 

是否有可能納入p元素加入代碼,同時仍然通過驗證器?

+0

'document.write'甚至不應該在XHTML文檔中工作:https://developer.mozilla.org/en-US/docs/Web/API/document.write – 2014-12-06 20:16:59

+0

它可以在XHTML 1.0 Transitional下工作嗎?對不起,我應該更具體。 – xianate 2014-12-06 20:28:34

回答

1

因爲你使用XHTML,你需要換你的JavaScript在CDATA:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>java script demonstration</title> 
    </head> 
    <body> 
     <script type="text/javascript"> 
      //<![CDATA[ 
      var response=confirm("This box was created using Javascript - Click on OK to continue") 
      if (response) 
       {document.write("<p>You have just clicked on OK</p>")} 
      else 
       {document.write("You have just clicked on Cancel")}  
        var reply=prompt("Please enter your Name") 
      document.write("your name is " + reply +"") 
      //]]> 
     </script> 
    </body> 
</html> 

這僅僅是一個講,什麼是包含在CDATA標記內的編譯器的方式是數據,而不是標記。如果你打算做任何內聯JS,這是必要的。