如何在布局MVC上调用JavaScript [英] how to call javascript on layout mvc

查看:72
本文介绍了如何在布局MVC上调用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to run a basic javascript in all my pages (before load another html code), then I add my js function to _Layout.cshtml but doesn't work (I don't how is correct way to do it). Thanks in advance.

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

推荐答案

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

必须在

    <script type="text/javascript">
        $(document).ready(function () { // JQuery not loaded here, $ is undefined
            alert("Do something");
        });
    </script>

这篇关于如何在布局MVC上调用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆