如果我使用本地 jquery.js 文件,jQuery 代码将无法工作,为什么?
比方说,这是我的页面。
<!doctype html>
<html>
<head>
</head>
<body>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
</script>
<div id="div1"></div>
</body>
</html>
如您所见,我正在使用本地 jQuery 文件 (jquery.js
),因此每次我编写 jQuery 代码时,知道我的两个页面都无法工作和 jquery.js
在同一级别..
但是当我在线使用 jQuery 文件时,像这样
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
一切正常..
我还尝试将 在
中,但没有任何
效果我下载了在线使用的相同文件并在本地使用。
Let's say this my page for example ..
<!doctype html>
<html>
<head>
</head>
<body>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
</script>
<div id="div1"></div>
</body>
</html>
As you see I'm using a local jQuery file (jquery.js
) so every time I write jQuery code it doesn't work knowing that both of my page and jquery.js
in same level ..
But when I'm using a jQuery file online, like this for example
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
everything works fine..
Also I tried to put <script></script>
in the <head>
but nothing works
P.s. I downloaded the same file I'm using online and using it locally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个编码问题。您的
page.html
文件采用 UTF-16 格式(尽管它包含错误的meta
标记,表明它使用 UTF-8),但是jquery.js< /code> 文件不是(它要么是 ASCII、Windows-1252/ISO-8859-1,要么是 UTF-8——哪一个并不重要,它会坚持所有字符有共同点)。
我建议更正
page.html
的编码(使其实际上为 UTF-8)。这可能会澄清它。但如果不是,或者如果您确实想要 UTF-16(例如,您主要使用非西方脚本制作页面),您可以使用script
元素上的charset
属性,告诉浏览器期望什么获取脚本时。我是这样实现的:当我访问您发布的链接时,我在控制台中的
jquery.js
中收到“非法令牌”错误(在 Chrome 和 Firebug 中),并且显示的jquery.js
文件内容是乱码,主要显示在东亚字符集。如果我直接请求资源,就不会有这个问题。这立即让我想到“编码问题”并查看page.html
。快速浏览一下,发现它是 UTF-16 格式的。仔细检查jquery.js
文件是否也是 UTF-16 格式(可能是这样,尽管我永远不会以这种方式编码 JavaScript,但这会非常浪费),并发现它位于与 ASCII 兼容的编码(例如,不是 UTF-16)。如果您不能 100% 确定您理解我所说的
page.html
采用 UTF-16 而jquery.js
采用 UTF-8 或类似格式,我建议阅读每个软件开发人员绝对、肯定必须了解 Unicode 和字符的绝对最低限度套装(无抱歉!),作者:Joel Spolsky,还有 unicode.org 上的各种常见问题解答,特别是 主要部分和讨论各种 UTF 和物料清单。It's an encoding problem. Your
page.html
file is in UTF-16 (although it contains an erroneousmeta
tag saying that it's using UTF-8), but thejquery.js
file isn't (it's either in ASCII, Windows-1252/ISO-8859-1, or UTF-8 — doesn't really matter which, it sticks to the characters all of them have in common).I suggest correcting the encoding of
page.html
(making it actually UTF-8). That will probably clear it up. But if not, or if you really want UTF-16 (e.g., you're doing a page mostly in non-Western script) you can use thecharset
attribute on thescript
element to tell the browser what to expect when fetching the script.Here's how I got there: When I visit the link you posted, I get an "illegal token" error in
jquery.js
in the console (on Chrome and in Firebug) and thejquery.js
file content shown is garbled, showing mostly in an east asian character set. If I request the resource directly, I don't have that problem. That immediately made me think "encoding problem" and go look atpage.html
. A quick glance revealed it to be in UTF-16. Double-checked that thejquery.js
file wasn't also in UTF-16 (it could be, though I would never encode JavaScript that way, it would be very wasteful) and found it to be in an ASCII-compatible encoding (e.g., not UTF-16).If you're not 100% certain you understand what I'm saying about
page.html
being in UTF-16 butjquery.js
being in UTF-8 or similar, I'd recommend reading The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky and also the various FAQs on unicode.org, in particular the main one and the one discussing the various UTFs and the BOM.浏览器不应该关心文件是否是本地的[网络服务器本地,大概......?]。
您本地的文件副本有问题。确保路径正确并使用 Firebug 查看其加载发生了什么情况。
编辑 对于您的
demo_local
,我的 Firebug 安装抱怨 jquery.js 文件中存在外来字符。当我查看“脚本”选项卡时,它似乎采用 Unicode,但以 ASCII 形式提供。The browser should not care whether the file is local [local to the webserver, presumably...?].
Something is wrong with the copy of the file that you have locally. Ensure that the path is correct and use Firebug to see what happened to its loading.
Edit For your
demo_local
, my Firebug installation complains of foreign characters in the jquery.js file. When I look on the Script tab, it appears to be in Unicode but is served as ASCII.sam
你的 jquery 中有一些奇怪的代码。
看起来本地的 jquery.js 是错误的,
请从这里下载它
http://code.jquery。 com/jquery-1.6.min.js
您可以尝试以下
标准文档类型
并将脚本更改为
sam
You have some wierd code in that jquery.
looks like jquery.js locally is wrong
please download it from here
http://code.jquery.com/jquery-1.6.min.js
can you try the following
a standard doc type
and change script to
我可以看到您正在使用 Dropbox 来托管文件,这自动意味着您无法对文件进行本地引用。尝试包含完整的保管箱链接并查看它是否有效
I can see you are using dropbox to host your files, which automatically means you cannot make local reference to your files. Try including the full dropbox link and see if it works