作为作业任务的一部分,我需要使用 XHTML 1.0 过渡来显示 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 元素合并到代码中?
因为你使用的是XHTML,所以你需要用CDATA包装你的JavaScript:
<!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,这是必需的。