JavaScript 运行时错误:“$”未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14971394/
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
JavaScript runtime error: '$' is undefined
提问by RualStorge
So classic problem, but having a horrible time on finding the actual cause. Typically when I see this error it's because the jQuery reference is after code requiring it, or back jQuery link, or jQuery conflict, etc... so far none of those appear to be the case. Unfortunately seeking out the solution to this problem has lead me to post after post of such cases. I'm sure my problem here is equally as simple, but over an hour of hunting, still no luck...
如此经典的问题,但在寻找实际原因方面却花费了大量时间。通常,当我看到此错误时,是因为 jQuery 引用在需要它的代码之后,或返回 jQuery 链接,或 jQuery 冲突等......到目前为止,这些似乎都不是这种情况。不幸的是,寻找这个问题的解决方案让我发了一个又一个这样的案例。我确定我的问题同样简单,但经过一个多小时的狩猎,仍然没有运气......
Edit: Additional information... The solution file (which I've recreated multiple times trying to figure this out. Is a JavaScript Windows Store Blank App template and I'm doing this in Visual studio. The only references files is Windows Library for javascript 1.0, I have tried deleting this to test as well.
编辑:附加信息...解决方案文件(我已经多次重新创建试图解决这个问题。是一个 JavaScript Windows 商店空白应用程序模板,我正在 Visual Studio 中执行此操作。唯一的参考文件是 Windows 库javascript 1.0,我也尝试删除它进行测试。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>HTML5 Canvas Template</title>
<style>
/* styles here */
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="500">
<p>Canvas not supported.</p>
</canvas>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var canvas = $("#myCanvas").get(0);
var context = canvas.getContext("2d");
function renderContent()
{
// we'll do our drawing here...
}
renderContent();
});
</script>
</body>
</html>
回答by coder
It's states that JQueryreferred URL is not correct
它指出JQuery引用的 URL 不正确
Try this:
尝试这个:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
回答by RickInLosAngeles
I tried everything listed above and nothing seems to work until I put this string
我尝试了上面列出的所有内容,但在放置此字符串之前似乎没有任何效果
<script src="../scripts/jquery-2.2.3.min.js" type="text/javascript"></script>
in the head section of the HTML file. So here's how it looks:
在 HTML 文件的 head 部分。所以这是它的外观:
<!DOCTYPE html>
<html>
<head>
<!-- jQuery Reference -->
<script src="../scripts/jquery-2.2.3.min.js" type="text/javascript"></script>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<title>some title</title>
</head>
<body>
...
And the js file is located at a level below in the folder 'scripts'. Finally, the error is gone and what a relief!
并且 js 文件位于文件夹 'scripts' 下一层。最后,错误消失了,真是一种解脱!
回答by Chris Gillum
In my case, the problem was that I was rendering my page over https but I was trying to request the JQuery file over http, which is blocked by many browsers for security reasons.
就我而言,问题是我通过 https 呈现我的页面,但我试图通过 http 请求 JQuery 文件,出于安全原因,许多浏览器阻止了该文件。
My fix was to change this...
我的解决方法是改变这个......
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
...to this...
……到这……
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
This causes the browser to download JQuery using the same protocol (http or https) as the page being rendered.
这会导致浏览器使用与正在呈现的页面相同的协议(http 或 https)下载 JQuery。
回答by Pascal Klein
Some of my clients had this problem, because apparently they blocked loading Javascript from 3rd party sites. So now I always use the following code to include jQuery:
我的一些客户遇到了这个问题,因为显然他们阻止了从 3rd 方站点加载 Javascript。所以现在我总是使用以下代码来包含 jQuery:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
window.jQuery ||
document.write('<script type="text/javascript" src="/js/jquery-1.9.1.min.js">\x3C/script>')
</script>
This makes sure, that even if my client blocks loading the Javascript file from the CDN domain, the client still downloads the file from my own server, which is not blocked by the browser.
这确保,即使我的客户端阻止从 CDN 域加载 Javascript 文件,客户端仍会从我自己的服务器下载文件,浏览器不会阻止该文件。
回答by user3801205
Anover variant, in my case - I was forced to use proxy. So - IE11--> InternetOptions --> Connections-->LANSettings-Proxy Server--> UseProxyServer- should be checked. Also check awailability of jQUery script source, my worked variant in VS2012 - -just like in top example
Anover 变体,就我而言 - 我被迫使用代理。所以- IE11--> InternetOptions --> Connections-->LANSettings-Proxy Server--> UseProxyServer- 应该被选中。还要检查 jQUEry 脚本源的可用性,我在 VS2012 中的工作变体 - 就像在上面的例子中一样
回答by user6263772
I was getting this same error code:
我收到了同样的错误代码:
(Error: 'generateText' is undefined)
(错误:'generateText' 未定义)
...on the code
...在代码上
var bodyText=["The....now."]
var bodyText=["The....now."]
I discovered on my text-editor(Notepad++), when typing many lines of text in the directly above the variable bodyText, if I didn't hit return carriage(enter==>WordWrap is off) just kept typing w/o return carriageand let the editor adjust text it worked?
我在我的文本编辑器(记事本++)上发现,当在变量正上方输入多行文本时bodyText,如果我没有点击return carriage(输入==>WordWrap 关闭)只是继续输入 w/oreturn carriage并让编辑器调整文字有效吗?
Must be in the settings of Notepad++??
必须在Notepad++的设置里??

