JQuery - $ 未定义 [英] JQuery - $ is not defined

查看:42
本文介绍了JQuery - $ 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 jquery 点击事件

I have a simple jquery click event

<script type="text/javascript">
    $(function() {
        $('#post').click(function() {
            alert("test"); 
        });
    });
</script>

以及在 site.master 中定义的 jquery 引用

and a jquery reference defined in the site.master

<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript"></script>

我已经检查过脚本是否被正确解析,我可以直接在 firebug 中看到标记和查看脚本,所以我一定是被找到了.但是,我仍然得到:

I have checked that the script is being resolved correctly, I'm able to see the markup and view the script directly in firebug, so I must be being found. However, I am still getting:

$ 未定义

并且没有一个 jquery 有效.我还尝试了诸如 $(document).ready 和 jQuery 等的各种变体.

and none of the jquery works. I've also tried the various variations of this like $(document).ready and jQuery etc.

这是 .net 3.5 上的 MVC 2 应用程序,我确定我真的很密集,谷歌上到处都说要检查文件是否被正确引用,我已经检查并再次检查,请指教!:/

It's an MVC 2 app on .net 3.5, I'm sure I'm being really dense, everywhere on google says to check the file is referenced correctly, which I have checked and checked again, please advise! :/

推荐答案

该错误只能由以下三种情况之一引起:

That error can only be caused by one of three things:

  1. 您的 JavaScript 文件没有正确加载到您的页面中
  2. 您有一个拙劣的 jQuery 版本.这可能是因为有人编辑了核心文件,或者插件可能覆盖了 $ 变量.
  3. 您在页面完全加载之前运行 JavaScript,因此,在 jQuery 完全加载之前.

首先,确保正确调用了什么脚本,它应该看起来像

First of all, ensure, what script is call properly, it should looks like

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

并且不应具有asyncdefer 属性.

and shouldn't have attributes async or defer.

然后您应该检查 Firebug net 面板以查看文件是否确实在正确加载.如果没有,它将突出显示为红色,并在旁边显示404".如果文件加载正确,这意味着问题是第 2 个问题.

Then you should check the Firebug net panel to see if the file is actually being loaded properly. If not, it will be highlighted red and will say "404" beside it. If the file is loading properly, that means that the issue is number 2.

确保所有 jQuery javascript 代码都在代码块中运行,例如:

Make sure all jQuery javascript code is being run inside a code block such as:

$(document).ready(function () {
  //your code here
});

这将确保您的代码在 jQuery 初始化之后加载.

This will ensure that your code is being loaded after jQuery has been initialized.

要检查的最后一件事是确保在加载 jQuery 之前没有加载任何插件.插件扩展了$"对象,所以如果你在加载 jQuery 核心之前加载插件,那么你会得到你描述的错误.

One final thing to check is to make sure that you are not loading any plugins before you load jQuery. Plugins extend the "$" object, so if you load a plugin before loading jQuery core, then you'll get the error you described.

注意:如果您正在加载不需要 jQuery 运行的代码,则不需要将其放置在 jQuery 就绪处理程序中.可以使用 document.readyState 分隔该代码.

Note: If you're loading code which does not require jQuery to run it does not need to be placed inside the jQuery ready handler. That code may be separated using document.readyState.

这篇关于JQuery - $ 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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