将 JQuery 链接到我的 html 中

Linking JQuery into my html

本文关键字:html 我的 JQuery 链接      更新时间:2023-09-26

我正在尝试将外部jQuery文件包含在我的html中(第一次),但它不起作用,只是在没有jquery的情况下执行html。(CSS工作正常)

这是 html 代码:

<!doctype html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css" />
</head>
<body>
  <script type="text/javascript" src="<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" />
  <script type="text/javascript" src="script.js"></script>
  <h1>Hello World!</h1>`
</body>
</html>

这是脚本中的jquery代码.js:

$(document).ready(function(){
   alert("it works");
});

是的,它们都在同一个文件夹中(实际上是保管箱文件夹,但我认为保管箱不会影响任何东西)

<script type="text/javascript" src="<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" />
<script type="text/javascript" src="script.js"></script>

首先,删除 <script> 标记内的额外src="<script。 还用</script>标签"关闭"它......

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>

其次,将脚本包含放在<body>部分的末尾,紧挨着</body>标记之前:

    ....
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
</body>

或者,在<head>部分内的任何位置:

    ....
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
    ....
</head>

最后,确保 URL 路径适合您的版本:

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

链接到完整版本总是明智的。 否则,如果 URL 中的代码更新,您的网站可能会突然中断,而不会发出警告。


如果您如上所述进行更改并在头部部分添加<title>元素......

<head>
    <title>Title</title>
    ....

。然后,您的代码将通过 HTML 验证。

http://validator.w3.org/check

这是错别字吗?你的 src=" 中有一个额外的<script"

<script type="text/javascript" src="<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" />

应该是

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

将其更改为:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

你在 src 中有完整的脚本,这是错误的。