【问题标题】:JQuery.js tag placement best practice MVC .netJQuery.js 标签放置最佳实践 MVC .net
【发布时间】:2021-10-18 23:33:43
【问题描述】:

我应该将我的<script src="~/lib/jquery/dist/jquery.js"></script> 放在_Layout.cshtml 页面的什么位置?在底部或顶部?现在我已经完成了搜索,将 jquery.js 放在布局页面的底部将提供页面加载性能。但是,当@RenderBody() 时,我遇到了一个问题/错误,正文需要在加载脚本之前使用jQuery,这导致$ 未定义。

link - Layout in ASP.NET Core

我的页面看起来和上面的链接一模一样。 任何解决该问题的提示和技巧将不胜感激。

【问题讨论】:

    标签: jquery model-view-controller


    【解决方案1】:

    您可以通过在页面末尾添加 jQuery 的页面后面有一个页面脚本部分来解决此问题。

    因此,在Layout 中的所有其他库脚本下方创建一个新部分(例如 MyScripts):

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

    并且在视图中:

    @section MyScripts {
        <script type="text/javascript">
            write your jquery script...
        </script>
    }
    

    我强烈推荐阅读这篇关于 Razor 部分的文章:https://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-04
      • 2021-03-25
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多