asp.net mvc 4 _Layout.cshtml 中的 jquery 用法

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

jquery usage in asp.net mvc 4 _Layout.cshtml

jqueryasp.net-mvc-4

提问by Stephen King

I used jquery in my asp.net mvc 4 project. The jquery code is in _Layout,cshtml. In IE 9, an exception was thrown.

我在我的 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

In firefox, no error was thrown but jquery didn't work at all.

在 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>

UPDATE:

更新:

In App_Start there is a BundleConfig.cs.

在 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"));
    }
}

回答by Stephen King

Make sure you are including the bundle in the _Layout.cshtml

确保您在 _Layout.cshtml 中包含包

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

And make sure the bundles are defined in your BundleConfig.cs:

并确保捆绑包在您的 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*"));

Make sure you are using the correct version and file name in your bundle. [1] http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

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

in your case:

在你的情况下:

 <!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>