我目前已经用Javascript编写了一些函数来检测我在哪个页面上并更改页面中的类(以保持html标签打开)。
某些功能(大多数)可以很好地检测页面URL,但有些功能无法正常工作。
$( document ).ready(
function() {
// Services
if (window.location.href.indexOf('?action=services') != -1)
{
document.getElementById("Primary_Navbar-Services").className = "dropdown open";
}
else if (window.location.href.indexOf('cart.php') != -1)
{
document.getElementById("Primary_Navbar-Services").className = "dropdown open";
}
else if (window.location.href.indexOf('cart.php?gid=addons') != -1)
{
document.getElementById("Primary_Navbar-Services").className = "dropdown open";
}
// Domains
else if (window.location.href.indexOf('clientarea.php?action=domains') != -1)
{
document.getElementById("Primary_Navbar-Domains").className = "dropdown open";
}
else if (window.location.href.indexOf('/cart/domain/renew') != -1)
{
document.getElementById("Primary_Navbar-Domains").className = "dropdown open";
}
// Not working
else if (window.location.href.indexOf('/cart.php?a=add&domain=register') != -1)
{
document.getElementById("Primary_Navbar-Domains").className = "dropdown open";
}
else if (window.location.href.indexOf('/cart.php?a=add&domain=transfer') != -1)
{
document.getElementById("Primary_Navbar-Domains").className = "dropdown open";
}
// Factures
else if (window.location.href.indexOf('clientarea.php?action=invoices') != -1)
{
document.getElementById("Primary_Navbar-Billing").className = "dropdown open";
}
else if (window.location.href.indexOf('clientarea.php?action=quotes') != -1)
{
document.getElementById("Primary_Navbar-Billing").className = "dropdown open";
}
else if (window.location.href.indexOf('clientarea.php') != -1)
{
document.getElementById("Primary_Navbar-Billing").className = "dropdown open";
}
else if (window.location.href.indexOf('account/paymentmethods') != -1)
{
document.getElementById("Primary_Navbar-Billing").className = "dropdown open";
}
else if (window.location.href.indexOf('account/paymentmethods') != -1)
{
document.getElementById("Primary_Navbar-Billing").className = "dropdown open";
}
else if (window.location.href.indexOf('clientarea.php?action=addfunds') != -1)
{
document.getElementById("Primary_Navbar-Billing").className = "dropdown open";
}
// Support
else if (window.location.href.indexOf('supporttickets.php') != -1)
{
document.getElementById("Primary_Navbar-Support").className = "dropdown open";
}
else if (window.location.href.indexOf('announcements') != -1)
{
document.getElementById("Primary_Navbar-Support").className = "dropdown open";
}
else if (window.location.href.indexOf('knowledgebase') != -1)
{
document.getElementById("Primary_Navbar-Support").className = "dropdown open";
}
else if (window.location.href.indexOf('download') != -1)
{
document.getElementById("Primary_Navbar-Support").className = "dropdown open";
}
else if (window.location.href.indexOf('serverstatus.php') != -1)
{
document.getElementById("Primary_Navbar-Support").className = "dropdown open";
}
});
我希望每个函数都能正确检测到我所在的页面,这样它们就可以更改我要定位的其他类。谢谢。