我需要使用XHTML 1.0 Transitional显示简单使用confirm()和prompt()函数作为我的赋值任务的一部分。所有代码都需要验证。
当我尝试验证代码时,收到以下错误:
第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 :(得分: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,这是必要的。