javascript 为什么我会收到“未捕获的引用错误:$ 未定义(匿名函数)”

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

Why do I get an "Uncaught ReferenceError: $ is not defined (anonymous function)"

javascriptrazorknockout.js

提问by Niek de Klein

I have a cshtml page that gives these errors:

我有一个给出这些错误的 cshtml 页面:

Uncaught ReferenceError: $ is not defined               TabNotes:2
(anonymous function)                                    TabNotes:2
TabNotes:12Uncaught ReferenceError: ko is not defined   TabNotes:12
(anonymous function)                                    TabNotes:12
(anonymous function)                                    TabNotes:23

What can cause such an error? I can't find any reason why this would be. I tried wrapping the javascript function in $(document).ready(function () { but that did not work either. The code is below

什么会导致这样的错误?我找不到任何原因。我尝试将 javascript 函数包装在 $(document).ready(function () { 但这也不起作用。代码如下

@model test.Web.Framework.Areas.Administration.Models.TabNotesModel 
@using (UI.DocumentReadyScript())
{    
    if (Model.meta.id.HasValue)
    {
        UI.jQuery("#tbl" + Model.meta.modelname).flexigrid(Model.Grid);
    }
}
<form method="post" action="@Url.Action("TabNotes", new { cmd = "refresh" })" id="@Model.meta.modelname">
<div class="ui-state-highlight ui-corner-all highlight" data-bind="visible: meta.message">
    <span class="ui-icon ui-icon-info"></span><strong data-bind="text: meta.message">
    </strong>
</div>
@using (UI.BeginBlock("Administation.TabNotes", UI.Label("Notes", "Notes").ToString(), test.Web.Framework.Core.enumIcons.pencil, false, false))
{
    <table id="@("tbl" + Model.meta.modelname)">
    </table>
} 
</form>
<script type="text/javascript">
(function() {
        var viewModel=ko.mapping.fromJS(@Html.Raw(UI.JavascriptEncode(Model)));
        viewModel.getData=function() { return ko.mapping.toJSON( this  ); };   
        viewModel.setData=function(data){ 
        $('#tbl'+this.meta.modelname()).flexigrid( data.Grid);
        ko.mapping.updateFromJS(this,data); 
        };
        $('#@Model.meta.modelname').koform({viewmodel: viewModel , validate : {errorElement:'p' }  } );
        $('#@Model.meta.modelname').koform('applyBindings');
        $('#load-partial').click(function() {
            $('#partial').load('@Html.Raw(@Url.Action("CreateNote", "Entity", new {modelEntity = @Model.meta.entity, itemId = @Model.meta.id}))');
        }); 
    })(); 
</script>


<div id="partial"></div>
<button type="button" id="load-partial">Create Note</button>

edit: The jQuery gets loaded from the master page.

编辑:jQuery 从母版页加载。

回答by Demian Brecht

It means that jQuery ($) and knockout (ko) are not defined. This often comes up when a library isn't loaded by the time the script has been executed.

这意味着 jQuery ( $) 和淘汰赛 ( ko) 没有定义。当执行脚本时库未加载时,通常会出现这种情况。

Ensure that you're loading the frameworks prior to using them (i.e. your framework-related script tags are inserted prior to your page-level scripts). If that's good, check your network activity in your developer panel to see if you're getting erroneous requests when downloading the frameworks. It could be that you're loading the frameworks asynchronously, which means you'll need an async handler to begin executing your page-level script.

确保在使用框架之前加载它们(即在页面级脚本之前插入与框架相关的脚本标签)。如果没问题,请在您的开发人员面板中检查您的网络活动,看看您在下载框架时是否收到错误的请求。可能是您正在异步加载框架,这意味着您需要一个异步处理程序来开始执行您的页面级脚本。

回答by John Papa

Where in your code are you referencing KO and jQuery? Usually this error occurs because your code tries to run before the references are loaded.

您在代码中的哪个位置引用了 KO 和 jQuery?通常发生此错误是因为您的代码尝试在加载引用之前运行。

I recommend using Fiddler or another network activity tool to watch when the references are loaded (or not) and where from.

我建议使用 Fiddler 或其他网络活动工具来观察引用何时(或未)加载以及从何处加载。

回答by abstractclass1

I had problems with this error when trying to incorporate a jquery ui widget into my site. I had to update the proper path of the css file being included; it can be picky sometimes. Also had to do the same for the jquery as well. Also take note where your localhost is with regards to where your 'src="/code/blah_folder" is. Hope it helps.

尝试将 jquery ui 小部件合并到我的网站时,我遇到了此错误的问题。我必须更新包含的 css 文件的正确路径;有时它可能很挑剔。也必须对 jquery 做同样的事情。还要注意您的本地主机在哪里,以及您的 'src="/code/blah_folder" 在哪里。希望能帮助到你。