如果我使用本地 jquery.js 文件,jQuery 代码不起作用,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5926701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:02:00  来源:igfitidea点击:

jQuery code doesn't work if I'm using a local jquery.js file, why?

jqueryfilelocal

提问by Sam

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.jsin same level ..

如您所见,我使用的是本地 jQuery 文件 ( jquery.js),因此每次我编写 jQuery 代码时,都无法知道我的页面和jquery.js同一级别的 ..

But when I'm using a jQuery file online, like this for example

但是当我在线使用 jQuery 文件时,例如这样

<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

此外,我试图把<script></script><head>,但没有作品

P.s. I downloaded the same file I'm using online and using it locally.

Ps 我下载了我在线使用的相同文件并在本地使用它。

回答by T.J. Crowder

It's an encoding problem. Your page.htmlfile is in UTF-16 (although it contains an erroneous metatag saying that it's using UTF-8), but the jquery.jsfile 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).

是编码问题。您的page.html文件是 UTF-16(尽管它包含一个错误的meta标签,说它使用的是 UTF-8),但jquery.js文件不是(它是 ASCII、Windows-1252/ISO-8859-1 或 UTF-8 —哪个并不重要,它坚持所有角色的共同点)。

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 charsetattributeon the scriptelement to tell the browser what to expect when fetching the script.

我建议更正page.html(使其实际上是 UTF-8)的编码。这可能会清除它。但如果不是,或者如果您真的想要 UTF-16(例如,您主要使用非西方脚本编写页面),您可以使用元素上的charset属性script来告诉浏览器在获取脚本时会发生什么。

Here's how I got there: When I visit the link you posted, I get an "illegal token" error in jquery.jsin the console (on Chrome and in Firebug) and the jquery.jsfile 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.jsfile 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).

这是我如何到达那里的:当我访问您发布的链接时,我jquery.js在控制台(在 Chrome 和 Firebug 中)中收到“非法令牌”错误,并且显示的jquery.js文件内容是乱码,主要以东亚字符集显示。如果我直接请求资源,我没有那个问题。这立即让我想到“编码问题”并去查看page.html. 快速浏览一下,发现它是 UTF-16。仔细检查jquery.js文件是否也不是 UTF-16(它可能是,虽然我永远不会那样编码 JavaScript,这将是非常浪费的)并发现它是 ASCII 兼容的编码(例如,不是 UTF -16)。

If you're not 100% certain you understand what I'm saying about page.htmlbeing in UTF-16 but jquery.jsbeing 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 oneand the one discussing the various UTFs and the BOM.

如果您不是 100% 确定您理解我所说的page.html使用 UTF-16 但jquery.js使用 UTF-8 或类似的内容,我建议您阅读每个软件开发人员绝对、肯定必须了解 Unicode 和字符的绝对最小值由 Joel Spolsky 编写的Sets (No Excuses!)以及 unicode.org 上的各种常见问题解答,特别是主要问题讨论各种 UTF 和 BOM 的问题

回答by Lightness Races in Orbit

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.

您在本地拥有的文件副本有问题。确保路径正确并使用 Firebug 查看其加载发生了什么。



EditFor 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.

编辑对于您的demo_local,我的 Firebug 安装抱怨 jquery.js 文件中有外来字符。当我查看“脚本”选项卡时,它似乎是 Unicode,但作为 ASCII 提供。

回答by kobe

sam

山姆

You have some wierd code in that jquery.

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

looks like jquery.js locally is wrong

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

please download it from here

请从这里下载

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

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>

回答by boug

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

我可以看到您正在使用 dropbox 来托管您的文件,这意味着您无法对您的文件进行本地引用。尝试包含完整的保管箱链接,看看它是否有效