'JQuery' 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/339314/
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 08:59:35  来源:igfitidea点击:

'JQuery' is undefined

jqueryasp.net-mvciis-7

提问by Raja

I am working on a ASP.net project created with local file system settings. I am using MVC and Jquery. Jquery is working fine when I run the application in debug mode i.e. in ASP.net Development server. I am trying to host the application in IIS 7. In hosted mode, it does not recognize Jquery and gives scripting error 'Jquery is undefined'. The locations of the script files is unchanged in both modes. Can anybody have any clue what can be the reason and how to solve this.

我正在使用本地文件系统设置创建一个 ASP.net 项目。我正在使用 MVC 和 Jquery。当我在调试模式下运行应用程序时,Jquery 工作正常,即在 ASP.net 开发服务器中。我试图在 IIS 7 中托管应用程序。在托管模式下,它无法识别 Jquery 并给出脚本错误“Jquery 未定义”。脚本文件的位置在两种模式下都没有改变。任何人都可以有任何线索可能是什么原因以及如何解决这个问题。

My code look like this;

我的代码看起来像这样;

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>

<script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>

<!-- YUI Styles -->
<link href="../../Content/reset.css" rel="stylesheet" type="text/css" />
<link href="../../Content/fonts.css" rel="stylesheet" type="text/css" />
<link href="../../Content/grids.css" rel="stylesheet" type="text/css" />
<!-- /YUI Styles -->
<link href="../../Content/knowledgebase.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
    //this hides the javascript warning if javascript is enabled
    (function($) {
        $(document).ready(function() {
            $('#jswarning').hide();
        });
    })(jQuery);
</script>

<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />

....

....

采纳答案by Andrey Shchekin

Try using Firebug 'Net' tab to see if the file gets loaded.

尝试使用 Firebug 'Net' 选项卡查看文件是否已加载。

You can also configure Firebug to fail on all errors and see when the error happens as compared to when jQuery gets loaded.

您还可以将 Firebug 配置为在所有错误上失败,并查看与加载 jQuery 时相比何时发生错误。

回答by

0 I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.

0 我不知道你们是否找到了解决问题的方法。我遇到了同样的问题,并且疯狂地想知道为什么我使用的插件会出现“jQuery 未定义”错误。我尝试了从互联网上获得的所有解决方案,但根本没有运气。

But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.

但是,突然间我想到了一些可能是脚本文件应该按顺序排列的东西。所以,我将 jquery 引用移动到第一个位置,一切都开始像魅力一样工作。

Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.

请记住,如果您在 jquery 中使用任何插件,请确保使用以下设置对这些字段的引用的顺序。

  1. reference to the jquery library
  2. reference to the other subsequent plug-in (dependant) libraries and so on...
  1. 对 jquery 库的引用
  2. 其他后续插件(依赖)库的引用等等...

e.g.:

例如:

  1. "script src="js/jquery-1.3.2.min.js" type="text/javascript"...
  2. "script src="js/jqDnR.min.js" type="text/javascript"...
  3. "script src="js/jquery.jqpopup.min.js" type="text/javascript"...
  4. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...
  1. “脚本源代码=“js/jquery-1.3.2.min.js”类型=“文本/javascript”...
  2. “脚本源代码=“js/jqDnR.min.js”类型=“文本/javascript”...
  3. “脚本源代码=“js/jquery.jqpopup.min.js”类型=“文本/javascript”...
  4. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...

Always make sure you must put the jquery reference to first and then the subsequent libraries.

始终确保您必须首先将 jquery 引用放在后面的库中。

Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.

希望,这可以解决您的问题,尤其是当您使用 MasterPages 时。很奇怪,当您不使用 MasterPages 时,无论您使用什么顺序,它都可以工作,但是当您使用时,它会以某种方式需要正确的顺序。

Good luck and happy coding,

祝你好运和快乐编码,

Vincent D'Souza

文森特·德索萨

回答by Raja

After Using the FIrebug, I found out the files were not found as the relative path for the files doesn;t work. TO resolve the problem, we should use the Url.Content Method to reslove the path. For example:

使用 FIrebug 后,我发现找不到文件,因为文件的相对路径不起作用。要解决这个问题,我们应该使用 Url.Content 方法来重新设置路径。例如:

<script src="<%= Url.Content("~/Scripts/jquery-1.2.6.js")%>" type="text/javascript"></script>

回答by Kip

You could also try letting Google host jQuery for you:

您也可以尝试让 Google 为您托管 jQuery:

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

This will avoid having to worry about the relative path to the script, avoid load on your server, and will be faster for users (who probably already have it in their browser's cache somewhere).

这将避免担心脚本的相对路径,避免服务器上的负载,并且对于用户来说会更快(他们可能已经在浏览器的缓存中的某处拥有它)。

回答by Stephen Hosking

I got the message by having a mismatch between the version of JavaScript in the page and the version in the project.

我通过页面中的 JavaScript 版本与项目中的版本不匹配而收到消息。

On the page: <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>

页面上:<script src="@Url.Content("~/Scripts/ jquery-1.4.4.min.js")" type="text/javascript"></script>

Actual version in the project: jquery-1.4.1.min.js

项目中实际版本:jquery-1.4.1.min.js

Diagnosed by using Firebug, and setting it show all errors, as described in the accepted answer.

通过使用 Firebug 进行诊断,并将其设置为显示所有错误,如已接受的答案中所述。

回答by Dan Heberden

To elaborate on the "text/javascript" answer, "application/javascript" will work in firefox and load jQuery just fine. It won't, however, load the file in IE. Use "text/javascript":

为了详细说明“text/javascript”答案,“application/javascript”将在 Firefox 中工作并加载 jQuery 就好了。但是,它不会在 IE 中加载文件。使用“文本/javascript”:

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

回答by Jonathan Lonowski

Jquery?!
You mean jQuery?

Jquery?!
你的意思是jQuery

Javascript is case-sensitive. Jqueryis not the same as jQuery.

Javascript 区分大小写。Jquery不一样jQuery

回答by Michel van Engelen

Raja is right, URL.Content works.

Raja 是对的,URL.Content 有效。

<script src="<%= Url.Content("~/Scripts/jquery-1.2.6.js")%>" type="text/javascript"></script>

Further, to prevent yourself from getting surprised at the moment you are migrating your application to an IIS server, don't use web development server but use IIS when developing.

此外,为了防止您在将应用程序迁移到 IIS 服务器时感到惊讶,请不要使用 Web 开发服务器,而是在开发时使用 IIS。

You can use IIS directly from visual studio by editing the the web properties:

您可以通过编辑 Web 属性直接从 Visual Studio 使用 IIS:

  • Go to the tab 'Web'
  • Check 'Use Local IIS Web server'
  • Click 'Create Virtual Directory'
  • Go to the IIS Manager to disable anonymous access in the created virtual directory if applicable.
  • 转到“Web”选项卡
  • 选中“使用本地 IIS Web 服务器”
  • 点击“创建虚拟目录”
  • 如果适用,请转到 IIS 管理器以在创建的虚拟目录中禁用匿名访问。

回答by Georg Begerow

One additional reason for this error I found out occurs if you are loading jQuery from a cdn and a local script from your server. They load independently, so if the local script finishing before the jQuery it will also be executed before, which gives us the lovely error. This is especialy an issue if jQuery is not in the browsercache so you might see this error occur on the first time the scripts loads and vanish later.

如果您从 CDN 加载 jQuery,并从您的服务器加载本地脚本,则会发生此错误的另一个原因。它们是独立加载的,所以如果本地脚本在 jQuery 之前完成,它也会在之前执行,这给我们带来了可爱的错误。如果 jQuery 不在浏览器缓存中,这尤其是一个问题,因此您可能会在第一次加载脚本时看到此错误,然后消失。

回答by Roy Astro

Common mistakes is that other Jquery Scripts are included first(Jquery.validation.js) before the Jquery.js

常见的错误是在 Jquery.js 之前先包含其他 Jquery 脚本(Jquery.validation.js)