0

我有一个包含以下内容的 html 文件:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js" />
</head>
<body>
    <script type="text/javascript">
        alert("Hello");
    </script>
    <h1>Test</h1>
</body>
</html>

当我直接从文件系统在 Firefox 中打开它时,输出是

<html>
<head>
<script src="http://code.jquery.com/jquery-2.0.3.js" type="text/javascript">
    it loads the script here
</script>
</head>
<body>
<h1>Test</h1>
</body>

我想知道为什么脚本标签没有加载到正文中。

如果我的 jquery 源来自 google cdn,它会加载脚本:

http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js

我没有在任何地方托管此页面。只是尝试一些 jQuery 的东西,发现了这个问题。

4

2 回答 2

2

您必须关闭<script>

<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js"></script>

(而不是<script ... />


您的代码变为(您会看到甚至语法突出显示已更改):

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js"></script>
</head>
<body>
    <script type="text/javascript">
        alert("Hello");
    </script>
    <h1>Test</h1>
</body>
</html>

另外,请参阅文档:

C.3。元素最小化和空元素内容

给定一个内容模型不是 EMPTY 的元素的空实例(例如,空标题或段落),不要使用最小化形式(例如 use<p> </p>和 not <p />)。

我想这个答案也会对你有所帮助。

于 2013-07-25T14:36:39.483 回答
0

脚本标签不会自动关闭

改变

<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js" />

<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js"></script>

因为现在结束头、起始正文和下一个脚本,直到结束脚本标签都在第一个脚本标签内,因为这是一个语法错误,浏览器可能正在努力修复你的错误。

于 2013-07-25T14:35:33.510 回答