2014-12-06 78 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,这是必要的。