javascript 未捕获的 ReferenceError $ 未定义

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

Uncaught ReferenceError $ is not defined

javascriptjqueryhtml

提问by Lostsoul

I'm very new to JavaScript (just started a few hours ago and trying to get a script working). I went through a few tutorials on W3 and the 'hello world'code works when I paste it directly into my HTML but I'm having a problem with a script (I've had problems with other scripts as well but I am not sure what I'm doing wrong).

我对 JavaScript 非常陌生(几个小时前刚刚开始并试图让脚本工作)。我浏览了一些关于 W3 的教程,当我将“hello world”代码直接粘贴到我的 HTML 中时,它可以工作,但是我的脚本有问题(我也有其他脚本的问题,但我不确定我做错了什么)。

I have this codethat I want to test in my HTML, I copied the HTML in and it looks the same then I made a file in my static folder called edit.jsand copied the JavaScript into it (exactly as shown). It didn't work no errors on the page but when I click it nothing happens. I tried to paste a W3 'hello world'code in and that worked but this script does not.

我有这个代码,我想在我的 HTML 中测试,我复制了 HTML,它看起来一样,然后我在我的静态文件夹中创建了一个文件,edit.js并将 JavaScript 复制到其中(如图所示)。它在页面上没有任何错误,但当我点击它时没有任何反应。我尝试将 W3 'hello world'代码粘贴进去,但是这个脚本没有。

I tried to inspect the code in Chrome and that's where I see the above error (under the resources tab). I can open the js file using Chrome which makes me think the js file is accessible and pointing correctly but I'm not sure how to get it working. I'm using Jinja2 as my template engine to render the HTML and in my header I have:

我试图检查 Chrome 中的代码,这就是我看到上述错误的地方(在资源选项卡下)。我可以使用 Chrome 打开 js 文件,这让我认为 js 文件可以访问并且指向正确,但我不确定如何让它工作。我使用 Jinja2 作为我的模板引擎来呈现 HTML,在我的标题中我有:

<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>

and in my main template (the one that gets rendered on all pages) I have:

在我的主模板(在所有页面上呈现的模板)中,我有:

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

edit.js:

编辑.js:

(even putting it within the script tag directly on the page I want to use it on doesn't work)

(即使将它直接放在我想使用它的页面上的脚本标签中也不起作用)

$('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
$('#savevalue').click(function(e){
   var showNew = $('#changevalue').val();
   $('#altervalue').hide();
   $('#storedvalue').show();
   $('#storedvalue span').text(showNew);
});?

HTML:

HTML:

(it's embedded in a larger page)

(它嵌入在更大的页面中)

    <head>
        <script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script language="JavaScript" type="text/javascript" src="static/edit.js"></script>
    </head>
... my html code..
        <div id="wrapper">
           <div id="container">
              <div id="storedvalue"><span>Hello</span> [<a href="javascript:void(0);" id="editvalue">edit</a>]</div>
              <div id="altervalue" style="display:none;"><input type="text" name="changevalue" id="changevalue" value="Hello"> [<a href="javascript:void(0);" id="savevalue">save</a>]</div>
           </div>
        </div>

I have never been able to successfully run a JavaScript that wasn't on W3 yet. I get the same problem with other scripts even though I see people online saying they work fine for them. Do I need to do anything extra to make this work?

我从来没有能够成功运行 W3 还没有的 JavaScript。尽管我在网上看到有人说他们对他们工作得很好,但我在使用其他脚本时也遇到了同样的问题。我需要做任何额外的事情来完成这项工作吗?

My two questions are:

我的两个问题是:

  • What am I doing wrong?
  • Because Javascript seems to just not work when there's a problem, is there a way to get errors or information on what's actually wrong?
  • 我究竟做错了什么?
  • 因为出现问题时 Javascript 似乎无法正常工作,所以有没有办法获取错误或有关实际错误的信息?

I read Uncaught ReferenceError: $ is not defined?and have been trying to figure this out for the last hour and can't see my problem.

我读到Uncaught ReferenceError: $ is not defined? 并且在过去的一个小时里一直试图解决这个问题,但看不到我的问题。

采纳答案by marteljn

First you need to place the jQuery script tag first.

首先,您需要先放置 jQuery 脚本标记。

Second, you need to do one of the following things:

其次,您需要执行以下操作之一:

  1. Put your code within this function:

    $(document).ready(function(){/*CODE HERE*/});
    
  2. Or like this:

    $(function(){
        /*CODE HERE*/
    });
    
  1. 将您的代码放在此函数中:

    $(document).ready(function(){/*CODE HERE*/});
    
  2. 或者像这样:

    $(function(){
        /*CODE HERE*/
    });
    

The DOM needs to be ready before you can use it. Placing your code within anonymous functions that are executed on the ready event of the DOM is how you can do this.

DOM 需要准备好才能使用它。将您的代码放在在 DOM 的就绪事件上执行的匿名函数中是您可以做到这一点的方法。

Edit:

编辑:

$(function(){
   $('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
   $('#savevalue').click(function(e){
     var showNew = $('#changevalue').val();
     $('#altervalue').hide();
     $('#storedvalue').show();
     $('#storedvalue span').text(showNew);
   });?
});

回答by root

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

Script tag for jQuery should come before your custom javascript.

jQuery 的脚本标签应该在你的自定义 javascript 之前。

Follow by edit.js

遵从 edit.js

<script type="text/javascript" src="static/edit.js"></script>

回答by Anirudh Rayabharam

Try removing the language attribute..sometimes work for me. It's obsolete now .. i think

尝试删除语言属性......有时对我有用。现在已经过时了..我想

回答by maxhud

You need to include jquery before you can use it.

您需要先包含 jquery,然后才能使用它。