4

我在我的 asp.net mvc 4 项目中使用了 jquery。jquery 代码在 _Layout,cshtml 中。在 IE 9 中,引发了异常。

Unhandled exception at line 14, column 9 in http://localhost:59899/
0x800a1391 - Microsoft JScript runtime error: 'jQuery' is undefined

在 Firefox 中,没有抛出错误,但 jquery 根本不起作用。

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>@ViewBag.Title</title>
   <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
   <meta name="viewport" content="width=device-width" />
   @Styles.Render("~/Content/css")
   @Scripts.Render("~/bundles/modernizr")

<script type="text/javascript">
    jQuery(document).ready(function () {
        var divone = jQuery(".main-content").height();
        var divtwo = jQuery(".sidebar").height();
        var maxdiv = Math.max(divone, divtwo);
        jQuery(".main-content").height(maxdiv);
        jQuery(".sidebar").height(maxdiv);
    });
</script>

更新:

在 App_Start 中有一个 BundleConfig.cs。

   public class BundleConfig
   {
    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    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"));
    }
}
4

1 回答 1

8

确保在 _Layout.cshtml 中包含捆绑包

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")

并确保在您的 BundleConfig.cs 中定义了捆绑包:

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-1.9.0.js",
           "~/Scripts/jquery.unobtrusive-ajax.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-1.10.0.js",
            "~/Scripts/jquery.ui.timepicker.js",
            "~/Scripts/Ascende.Common.js",
            "~/Scripts/jquery.contextMenu.js",
            "~/Scripts/jquery.blockUI.js"));

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

确保您在捆绑包中使用了正确的版本和文件名。[1] http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

在你的情况下:

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="utf-8" />
 <title>@ViewBag.Title</title>
 <link href="~/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") <!-- add this --> 

<script type="text/javascript">
    jQuery(document).ready(function () {
        var divone = jQuery(".main-content").height();
        var divtwo = jQuery(".sidebar").height();
        var maxdiv = Math.max(divone, divtwo);
        jQuery(".main-content").height(maxdiv);
        jQuery(".sidebar").height(maxdiv);
    });
</script>
于 2013-06-12T20:10:12.447 回答