我有一个简单的jquery点击事件

<script type="text/javascript">
    $(function() {
        $('#post').click(function() {
            alert("test"); 
        });
    });
</script>

以及在site.master中定义的jquery引用

<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript"></script>

我已经检查了脚本被正确解析,我能够看到标记,并直接在firebug中查看脚本,所以我必须被发现。然而,我仍然得到:

$没有定义

jquery没有一个是有效的。我还尝试了它的各种变体,比如$(document)。ready和jQuery等。

这是一个。net 3.5上的MVC 2应用程序,我确信我真的很密集,谷歌上的所有地方都说要检查文件是否被正确引用,我已经检查了一遍又一遍,请建议!: /


当前回答

在视图和主布局中使用脚本部分。

将视图中定义的所有脚本放在视图的scripts部分中。通过这种方式,您可以让主布局在所有其他脚本加载后加载此脚本。这是启动一个新的MVC5 web项目时的默认设置。不确定早期版本。

视图/ Foo / MyView.cshtml:

// The rest of your view code above here.

@section Scripts 
{ 
    // Either render the bundle defined with same name in BundleConfig.cs...
    @Scripts.Render("~/bundles/myCustomBundle")

    // ...or hard code the HTML.
    <script src="URL-TO-CUSTOM-JS-FILE"></script>

    <script type="text/javascript">
      $(document).ready(function () {

        // Do your custom javascript for this view here. Will be run after 
        // loading all the other scripts.            
      });
    </script>
}

视图/共享/ _Layout.cshtml

<html>
<body>
    <!-- ... Rest of your layout file here ... -->

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

注意脚本部分是如何在主布局文件中最后呈现的。

其他回答

这意味着您的jQuery库还没有加载。

您可以在拖动jQuery库后移动代码。

或者你可以用这样的东西

window.onload = function(){
  // Your code here
  // $(".some-class").html("some html");
};  

我使用Url。知足而从不有问题。

<script src="<%= Url.Content ("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script>

我也遇到过类似的问题。在我的c# MVC应用程序中,JQuery没有被识别出来。我需要移动@Scripts.Render("~/bundles/jquery")从底部的我的_Layout。CSHTML文件到section的头。另外,如果你正在使用visual studio,请确保你已经将Jquery作为Nuget包!

<head>
    @Scripts.Render("~/bundles/jquery")
</head>

在视图和主布局中使用脚本部分。

将视图中定义的所有脚本放在视图的scripts部分中。通过这种方式,您可以让主布局在所有其他脚本加载后加载此脚本。这是启动一个新的MVC5 web项目时的默认设置。不确定早期版本。

视图/ Foo / MyView.cshtml:

// The rest of your view code above here.

@section Scripts 
{ 
    // Either render the bundle defined with same name in BundleConfig.cs...
    @Scripts.Render("~/bundles/myCustomBundle")

    // ...or hard code the HTML.
    <script src="URL-TO-CUSTOM-JS-FILE"></script>

    <script type="text/javascript">
      $(document).ready(function () {

        // Do your custom javascript for this view here. Will be run after 
        // loading all the other scripts.            
      });
    </script>
}

视图/共享/ _Layout.cshtml

<html>
<body>
    <!-- ... Rest of your layout file here ... -->

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

注意脚本部分是如何在主布局文件中最后呈现的。

这种错误只能由以下三种情况之一引起:

您的JavaScript文件没有正确地加载到页面中 你有一个拙劣的jQuery版本。这可能是因为有人编辑了核心文件,或者插件覆盖了$变量。 在页面完全加载之前就已经运行了JavaScript,因此,在jQuery完全加载之前就已经运行了JavaScript。

首先,确保正确调用了什么脚本,它应该是这样的

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

并且不应该有async或defer属性。

然后,您应该检查Firebug net面板,看看文件是否正在正确加载。如果没有,它将被突出显示为红色,并在旁边显示“404”。如果文件加载正常,则意味着问题是2。

确保所有jQuery javascript代码都在代码块中运行,例如:

$(document).ready(function () {
  //your code here
});

这将确保在jQuery初始化之后加载代码。

最后要检查的一件事是确保在加载jQuery之前没有加载任何插件。插件扩展了“$”对象,所以如果你在加载jQuery核心之前加载了一个插件,那么你会得到你所描述的错误。

注意:如果你加载的代码不需要jQuery运行,它不需要放在jQuery ready处理程序中。这些代码可以使用document.readyState来分隔。