来自jQuery.com的jQuery引用从html中删除脚本

时间:2013-07-25 14:34:03

标签: jquery

我有一个包含以下内容的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源来自谷歌cdn:

,它会加载脚本
http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js

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

2 个答案:

答案 0 :(得分: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. Element Minimization and Empty Element Content

  

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

我想this answer也会帮助你。

答案 1 :(得分: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>

现在是关闭头,起始体和下一个脚本,直到关闭脚本标记都在第一个脚本标记内,并且因为这是语法错误,浏览器可能正在努力修复您的错误。