简体   繁体   English

JavaScript 运行时错误:“$”未定义

[英]JavaScript runtime error: '$' is undefined

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.通常,当我看到此错误时,是因为 jQuery 引用在需要它的代码之后,或返回 jQuery 链接,或 jQuery 冲突等......到目前为止,这些似乎都不是这种情况。 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...我确定我的问题同样简单,但是经过一个多小时的狩猎,仍然没有运气......

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 Library for 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>

It's states that JQuery referred URL is not correct它指出JQuery引用的 URL 不正确

Try this:试试这个:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

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.在 HTML 文件的头部部分。 So here's how it looks:所以这是它的外观:

<!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'. js 文件位于文件夹 'scripts' 下一层。 Finally, the error is gone and what a relief!最后,错误消失了,真是一种解脱!

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。

Some of my clients had this problem, because apparently they blocked loading Javascript from 3rd party sites.我的一些客户遇到了这个问题,因为显然他们阻止了从 3rd 方站点加载 Javascript。 So now I always use the following code to include jQuery:所以现在我总是使用以下代码来包含 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 文件,客户端仍然从我自己的服务器下载文件,浏览器不会阻止该文件。

Anover variant, in my case - I was forced to use proxy. Anover 变体,就我而言 - 我被迫使用代理。 So - IE11--> InternetOptions --> Connections-->LANSettings-Proxy Server--> UseProxyServer - should be checked.所以- IE11--> InternetOptions --> Connections-->LANSettings-Proxy Server--> UseProxyServer - 应该被检查。 Also check awailability of jQUery script source, my worked variant in VS2012 - -just like in top example还要检查 jQUEry 脚本源的可用性,我在 VS2012 中的工作变体 - 就像在上面的例子中一样

I was getting this same error code:我收到了同样的错误代码:

(Error: 'generateText' is undefined) (错误:'generateText' 未定义)

...on the code ...在代码上

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 carriage and let the editor adjust text it worked?我在我的文本编辑器(记事本++)上发现,当在变量bodyText正上方输入多行文本时,如果我没有按return carriage (输入==>WordWrap 关闭)只是继续输入不return carriage并让编辑器调整它工作的文本?

Must be in the settings of Notepad++??必须在Notepad++的设置里??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM