javascript - 在 Webform 的母版页中将 $(document).ready 放在哪里

标签 javascript webforms master-pages

我有这个脚本,用于解决 Twitter Bootstrap 中不更改事件颜色的问题 <li>标签。我正在使用网络表单。

<script type="text/javascript">
            $(document).ready(function () {
                var url = window.location.pathname;
                var substr = url.split('/');
                var urlaspx = substr[substr.length - 1];
                $('.nav').find('.active').removeClass('active');
                $('.nav li a').each(function () {
                    if (this.href.indexOf(urlaspx) >= 0) {
                        $(this).parent().addClass('active');
                    }
                });
            });
        </script>

现在我对 javascript 和 asp.net 都很陌生。所以这可能是一个糟糕的问题,我提前道歉,但我想知道这个问题的正确位置。

我最初把它放在 <head> 中Site.Master 页面上的标签。但这没有用。另外,当我将它添加到在正文中调用的 custom.js 文件时,我创建的它也不起作用。所以我刚刚将脚本本身添加到正文中,这可行,但有些东西告诉我这不是“最佳实践”方式,并且希望有更多经验的人可以告诉我它应该去哪里以及为什么。

这是 Master.Site 页面的副本以及我尝试过的位置。

<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> - My ASP.NET Application</title>

    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>
    <webopt:BundleReference runat="server" Path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="~/Content/custom.css" rel="stylesheet" type="text/css" /> <%--Custom CSS created for site--%>

</head>
<body>
    <form runat="server">
        <asp:ScriptManager ID="ScriptManager" runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="respond" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
            </Scripts>
        </asp:ScriptManager>

        <%--Custom JS created for site--%>
        <script src="Scripts/custom.js" type="text/javascript"></script>      
        <%-- This does nt work inside the custom.js --%>
        <script type="text/javascript">
            $(document).ready(function () {
                var url = window.location.pathname;
                var substr = url.split('/');
                var urlaspx = substr[substr.length - 1];
                $('.nav').find('.active').removeClass('active');
                $('.nav li a').each(function () {
                    if (this.href.indexOf(urlaspx) >= 0) {
                        $(this).parent().addClass('active');
                    }
                });
            });
        </script>

最佳答案

您是否尝试过创建一个 js 文件,将其命名为“MyScript.js”,并将其保存在服务器上的“Scripts”文件夹中。然后将其添加到您的母版页中:

    <%--Custom JS created for site--%>
    <script src="Scripts/custom.js" type="text/javascript"></script>
    <script src="Scripts/MyScript.js" type="text/javascript"></script><%-- <<< NEW --%>    

???

关于javascript - 在 Webform 的母版页中将 $(document).ready 放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31508303/

相关文章:

javascript - 为什么一种设置 prop 的方法不起作用(jQuery)?

javascript - 如何使用 Angular 图表更改标签的颜色

javascript - 粘性菜单 - 在某些页面上隐藏但在其他页面上不隐藏

javascript - 使用表单发送数据并从 Request.Form 中读取数据

asp.net - 发送 AJAX 请求到 .aspx 页面并返回 JSON

基于访问应用程序的域的 CSS 文件

asp.net - 使用母版页在 ASP.NET 中通过 @ Page 指令设置页面标题

javascript - 关于 jQuery 更改...使用 .html();

asp.net - C#.net Web 应用程序项目中缺少 "Models"文件夹

asp.net - 内容占位符 : Repeated Content