JavaScript issue with type attribute of <script> eleme

JavaScript issue with type attribute of <script> element

本文关键字:lt gt eleme script of issue with type attribute JavaScript      更新时间:2023-09-26

只要不设置type属性,就可以让脚本运行。但是,如果没有type属性,我无法让页面验证XHTML。一旦我设置了它,我的错误消息就会消失,但是随后脚本就无法运行了。下面是代码片段:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>John Whiteker's Home Page</title>
    <link rel="stylesheet" type="text/css" href="project.css" />
    <script><!-- This is my error saying that I need to set a type attribute -->
    function myFunction()
    {
        alert("Hello! I am an alert box!");
    }
    </script>
</head>
<body>

HTML5是允许你省略type属性并假设"text/javascript"的规范。对于其他版本的HTML, type属性是必需的。

您正在使用XHTML Doctype。如果你想使用HTML5,把这个Doctype放在顶部:

<!DOCTYPE html>

来自MDN: "如果这个属性不存在,脚本被视为JavaScript.",指的是HTML5。

引用: