0

我在 JSConsole 中收到一个错误,说$未定义。但是,当我查看页面源代码时,我看到包含 jQuery,但它包含在页面末尾。

捆绑配置

 public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()
    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

看法

@model MvcWebAPI.Models.ComputerStatus

@{
    ViewBag.Title = "Index";
 }


 <script type="text/javascript">
     $(function () {
         alert('Hi');
     });
 </script>
 <h2>Index</h2>

渲染源

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>

</head>
<body>

<script type="text/javascript">
    $(function () {
        alert('Hi');
    });
</script>
<h2>Index</h2>

    <script src="/Scripts/jquery-2.0.1.js"></script>


</body>
</html>
4

3 回答 3

2

您视图中的脚本似乎没有放在脚本部分中。确保你把它们包起来

@section scripts
{
     <script>...</script>
}
于 2013-05-30T17:30:37.397 回答
0

把你的身体改成这样就能解决问题

<body>
   @Scripts.Render("~/bundles/jquery")
   @RenderBody()       
   @RenderSection("scripts", required: false)
</body>
于 2013-05-30T17:00:18.750 回答
0

确保您的 _ViewStart 指向:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

如果是这种情况,那么您可以取出页面上的脚本标签。问题可能是文件没有加载到准备好的文档中,因此您会收到错误消息。如果您没有 _ViewStart,只需确保您的视图指向 _Layout 以进行样式设置。

[编辑] 还可以尝试将您的脚本标签放在头部或除文档末尾之外的任何其他位置。加载文档时看看是否有帮助。

希望能帮助到你。

于 2013-05-30T17:04:25.503 回答