【问题标题】:jQuery only seems to be working in Layout.cshtmljQuery 似乎只在 Layout.cshtml 中工作
【发布时间】:2021-12-04 09:21:19
【问题描述】:

我目前正在研究运行以下内容的 .NET C# Razor 页面应用程序的基础知识:

  • 剃刀页面
  • .NET 5/C#
  • jQuery v3.5.1
  • 引导程序 v4.3.1

我当前的问题是 jQuery(和纯 JS)只在 Layout.cshtml 文件中工作,我尝试了以下方法:

  1. _layout.cshtml 头部的脚本标签
  2. _layout.cshtml 正文底部的脚本标签
  3. 将 jQuery 封装在 @section 脚本中{}

在这个阶段我很确定这只是一个案例,它需要在 .NET5 中以非常特殊的方式进行设置,但我真的没有更多线索

_Layout.cshtml 的底部

    </header>
    <div class="container-fluid">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Head", required: false);
    @RenderSection("Scripts", required: false);

</body>
</html>

index.cshtml 文件的底部

@section scripts {
    <script>
        $(document).ready(function () {
            $(".btn-reply").click(function (event) {
                alert("it works");
            });
        });
    </script>
}

【问题讨论】:

  • 如果将 添加到 head 部分,它将正常工作..
  • @Kaan 这是我尝试的第一件事,我见过一些类似的人说他们认为脚本标签应该放在头上
  • 不需要需要(或一定想要)&lt;head&gt; 中的脚本标签。对此有很多讨论,但只是将其移到头脑中并不会突然使其他一切正常工作。

标签: javascript c# jquery .net-5


【解决方案1】:

结果令人沮丧的是,这根本与 JS 无关,实际上这归结为主 div,行类的 z-index 为 -1,不知道这是怎么回事,我不知道'不记得设置了这个,但如果我这样做了,我向所有花时间阅读本文的人道歉。

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多