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

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

JScript runtime error: '$' is undefined

javascriptjqueryasp.net-mvc-4runtime-errorundefined

提问by Danielle

My project is in MVC 4. My scripts are in _Layout.cshtml when reload the page the following error: JScript runtime error: '$' is undefined

我的项目在 MVC 4 中。当重新加载页面时,我的脚本在 _Layout.cshtml 中,出现以下错误:JScript 运行时错误:'$' 未定义

_Layout.cshtml:

_Layout.cshtml:

 <head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="~/Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/jqueryui")
    <script type="text/javascript" src="~/Scripts/jquery.qtip-1.0.0-rc3.min.js"> </script>
 </head>

My Partial View:

我的部分观点:

   <script type="text/javascript">

       $('#lnkOrganizar').click(function () {
       if (($('.frozenTopC').css('display') != 'none') &&      ($('.frozenTopConteudo').css('display') != 'none')) {
           $('.frozenTopC').css('display', 'none');
           $('.frozenTopConteudo').css('display', 'none');
        }
        else {
         $('.frozenTopC').css('display', 'table-cell');
         $('.frozenTopConteudo').css('display', 'table-cell')
        }
     });

  </script>

回答by Michael Durrant

jquery isn't present.

jquery 不存在。

Look at this line: <script type="text/javascript" src="~/Scripts/jquery.qtip-1.0.0-rc3.min.js"> </script>

看看这一行: <script type="text/javascript" src="~/Scripts/jquery.qtip-1.0.0-rc3.min.js"> </script>

Check that you have that Scripts/jquery.qtip-1.0.0-rc3.min.json the server

检查您Scripts/jquery.qtip-1.0.0-rc3.min.js在服务器上是否有

It is also NOT usual to refer to your home directory there - where you have the "~". If your script is in a Scriptsdirectory then you just use Scripts/jquery.qtip-1.0.0-rc3.min.jsbecause with a web server, everything is within it's root (top level) directory.

在那里引用您的主目录也是不常见的 - 在那里您有“ ~”。如果您的脚本在一个Scripts目录中,那么您只需使用,Scripts/jquery.qtip-1.0.0-rc3.min.js因为对于 Web 服务器,所有内容都在它的根(顶级)目录中。

Also is this file the main jquery library? It's not clear to me whether this is just your script code and if so you'll need to include the main js library with something like:

这个文件也是主要的jquery库吗?我不清楚这是否只是您的脚本代码,如果是,您需要包含主 js 库,例如:

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

btw, aim to use lowercase for directory names - scriptsnot Scriptsas it will make your life easier in the long run. I would also recommend scriptnot scriptsas many directories have multiple files (their purpose after all) so most folks use singular directory. This is closer to a preference though than the ~issue.

顺便说一句,旨在用小写的目录名称-scripts不是Scripts因为它会让你的生活,从长远来看更容易。我还建议script不要scripts有多个目录有多个文件(毕竟它们的目的),所以大多数人使用单个目录。尽管这比~问题更接近于偏好。

Also, while debugging and playing around, remember that you can actually have the script in the same file, within <script>tags, not in a separate file. Not recommended long-term as a good practice but useful for seeing where the issue is.

此外,在调试和播放时,请记住,您实际上可以将脚本放在同一个文件中,在<script>标签内,而不是在单独的文件中。不推荐长期作为一种好的做法,但有助于查看问题所在。