如何在Mvc 5 _Layout页面中调用外部Javascript文件 [英] How Do I Call External Javascript File In Mvc 5 _Layout Page

查看:81
本文介绍了如何在Mvc 5 _Layout页面中调用外部Javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我希望能够在mvc 5 _layout页面中调用外部js文件,我已经对谷歌进行了一些研究,但没有成功,下面是我在_Layout Head部分的代码:



Hello everyone,

I want to be able to call external js file in mvc 5 _layout page, i have done a few researches on google, but no success, below is my code in the _Layout Head section:

<!-- start: MAIN JAVASCRIPTS -->
        <!--[if lt IE 9]>
            <script src="~/assets/plugins/respond.min.js"></script>
            <script src="~/assets/plugins/excanvas.min.js"></script>
            <script type="text/javascript" src="~/assets/plugins/jQuery-lib/1.10.2/jquery.min.js"></script>
        <![endif]-->
        <!--[if gte IE 9]><!-->
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/jQuery-lib/2.0.3/jquery.min.js")"></script>
        <!--<![endif]-->
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-ui/jquery-ui-1.10.2.custom.min.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/bootstrap/js/bootstrap.min.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/blockUI/jquery.blockUI.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/iCheck/jquery.icheck.min.js")"></script>
        
        <!-- end: MAIN JAVASCRIPTS -->

        <script>
            jQuery(document).ready(function () {
                Main.init();
                Index.init();
            });
        </script>

        @RenderSection("JavaScript", required : false)
        @RenderSection("CSS", required : false)





_LayoutPage的底部我有:



bottom of the _LayoutPage i have this:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/assets/plugins")
@RenderSection("Scripts", required : false)





然后在我的View页面中我有这个:





Then in my View page i have this:

@section JavaScript
{

    <!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/flot/jquery.flot.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/flot/jquery.flot.pie.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/flot/jquery.flot.resize.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery.sparkline/jquery.sparkline.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-easy-pie-chart/jquery.easy-pie-chart.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-ui-touch-punch/jquery.ui.touch-punch.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/fullcalendar/fullcalendar/fullcalendar.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/js/index.js")">
        
    </script>
    <!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
}







i如果有人能纠正我做错了,谢谢。




i will appreciate if anyone could correct what i did wrong, thanks.

推荐答案

您正在尝试在定义之前使用jQuery。将您的js代码移动到包含脚本文件的位置下方,或者将该代码放在js文件中而不是放在布局上,并在包含jquery等之后包含它。您的JavaScript部分中的脚本会遇到同样的问题,在jquery之前它们也被包括在内。您的脚本按顺序加载和处理,因此它们在页面上显示的位置非常重要。
You are trying to use jQuery before it is defined. Move your js code to below where you include your script files, or put that code in a js file rather than on the layout and include it after you include jquery etc. You'll have the same issue with the scripts in your JavaScript section, they are also being included before jquery is. Your scripts are loaded and processed in order so where they appear on the page is important.


这篇关于如何在Mvc 5 _Layout页面中调用外部Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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