Javascript 在Web窗体的母版页中放置$(文档).ready
我有一个脚本,我正在使用它来解决Twitter引导中的问题,即不更改活动Javascript 在Web窗体的母版页中放置$(文档).ready,javascript,webforms,master,Javascript,Webforms,Master,我有一个脚本,我正在使用它来解决Twitter引导中的问题,即不更改活动选项卡的颜色。我正在使用webforms <script type="text/javascript"> $(document).ready(function () { var url = window.location.pathname; var substr = url.split('/');
选项卡的颜色。我正在使用webforms
<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>
$(文档).ready(函数(){
var url=window.location.pathname;
var substr=url.split('/');
var urlaspx=substr[substr.length-1];
$('.nav').find('.active').removeClass('active');
$('.nav li a')。每个(函数(){
如果(this.href.indexOf(urlaspx)>=0){
$(this.parent().addClass('active');
}
});
});
现在我对javascript和asp.net都很陌生。因此,这可能是一个糟糕的问题,我提前道歉,但我想知道放在哪里合适
我最初把它放在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>
-我的ASP.NET应用程序
$(文档).ready(函数(){
var url=window.location.pathname;
var substr=url.split('/');
var urlaspx=substr[substr.length-1];
$('.nav').find('.active').removeClass('active');
$('.nav li a')。每个(函数(){
如果(this.href.indexOf(urlaspx)>=0){
$(this.parent().addClass('active');
}
});
});
您是否尝试创建一个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 --%>
您是否尝试创建一个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文件时,似乎必须仔细检查项目的id。例如,contentplaceholder上某个项目的id将更改为contentplaceholder Name_id。我不确定您的问题,但当我尝试将javascript文件合并到母版页时,似乎必须仔细检查项目的id。例如,contentplaceholder上项目的id将更改为contentplaceholder名称\u id。实际上,“custom.js”上面的id正是您所描述的。但是如果我添加了这个函数,它由于某种原因不能运行。我看到:
。。。也许这会让你更明智:)实际上上面的“custom.js”正是你所描述的。但是如果我添加了这个函数,它由于某种原因不能运行。我看到:
。。。也许这会让你更聪明:)