如果我使用本地jquery.js文件,则jQuery代码不起作用,为什么? [英] jQuery code doesn't work if I'm using a local jquery.js file, why?

查看:126
本文介绍了如果我使用本地jquery.js文件,则jQuery代码不起作用,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,说这是我的页面.

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>

如您所见,我正在使用本地jQuery文件(jquery.js),所以每次我编写jQuery代码时,都知道我的页面和jquery.js处于同一级别..

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

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

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>

一切正常.

我也尝试将<script></script>放在<head>中,但是没有任何作用

Also I tried to put <script></script> in the <head> but nothing works

P.s.我下载了与在线使用的文件相同的文件,并在本地使用了该文件.

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

推荐答案

这是一个编码问题.您的page.html文件位于UTF-16中(尽管它包含一个错误的meta标记,表明它正在使用UTF-8),但jquery.js文件却不是(它是ASCII,Windows-1252/ISO- 8859-1或UTF-8并没有什么关系,它会坚持使用它们所有共同的字符.

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

我建议更正page.html的编码(使其实际上为UTF-8).那可能会清除它.但是,如果不是这样,或者如果您确实想要UTF-16(例如,您的页面大多使用非西方脚本编写),则可以使用

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.

这是到达这里的方式:当我访问您发布的链接,在控制台(在Chrome和Firebug中)的jquery.js中出现非法令牌"错误,并且显示的jquery.js文件内容显示为乱码,大部分显示为东亚字符集.如果我直接请求资源,则没有问题.那立刻让我想到了编码问题",然后去看看page.html.快速浏览后发现它位于UTF-16中.仔细检查jquery.js文件是否也不是UTF-16(可能是,尽管我永远不会那样编码JavaScript,这会非常浪费),并发现它采用的是ASCII兼容编码(例如,而不是UTF-16).

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

如果您不确定100%是否理解我所说的page.html位于UTF-16中,而jquery.js位于UTF-8或类似名称中,则建议阅读 a>和讨论各种UTF和BOM的人.

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.

这篇关于如果我使用本地jquery.js文件,则jQuery代码不起作用,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆