如果我使用本地 jquery.js 文件,jQuery 代码将无法工作,为什么?

发布于 2024-11-05 10:07:50 字数 744 浏览 4 评论 0原文

比方说,这是我的页面。

<!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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳若烟 2024-11-12 10:07:50

这是一个编码问题。您的 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 erroneous meta tag saying that it's using UTF-8), but the jquery.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 the charset attribute on the script 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 the jquery.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 at page.html. A quick glance revealed it to be in UTF-16. Double-checked that the jquery.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 but jquery.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.

(り薆情海 2024-11-12 10:07:50

浏览器不应该关心文件是否是本地的[网络服务器本地,大概......?]。

您本地的文件副本有问题。确保路径正确并使用 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.

爱的那么颓废 2024-11-12 10:07:50

sam

你的 jquery 中有一些奇怪的代码。

看起来本地的 jquery.js 是错误的,

请从这里下载它

http://code.jquery。 com/jquery-1.6.min.js

您可以尝试以下

标准文档类型

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

并将脚本更改为

<script type="text/javascript">

</script>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

and change script to

<script type="text/javascript">

</script>
忆伤 2024-11-12 10:07:50

我可以看到您正在使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文