2

我尝试在我的所有页面中运行一个基本的 javascript(在加载另一个 html 代码之前),然后我将我的 js 函数添加到 _Layout.cshtml 但不起作用(我不知道如何做正确的方法)。提前致谢。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>
        <meta name="viewport" content="width=device-width" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        @Styles.Render("~/Content/mobileCss", "~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        <script type="text/javascript">
            $(document).ready(function () {
                alert("Do something");
            });
        </script>
    </head>
    <body>

        <div data-role="page" data-theme="b">
            <div data-role="content">
                @RenderBody()
            </div>
        </div>

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

1 回答 1

5
@Scripts.Render("~/bundles/jquery", "~/bundles/jquerymobile")

必须在之前

    <script type="text/javascript">
        $(document).ready(function () { // JQuery not loaded here, $ is undefined
            alert("Do something");
        });
    </script>
于 2012-11-23T12:02:00.470 回答