<ul id="mainMenu" class="main-nav">
<li class="nav-menu-dropdown-company active"><a class="company- btn" href="/company">Company</a>
<li><a href="/features">Features</a></li>
<li><a href="/try-now.php#demorequestform">Pricing</a></li>
<li><a href="/contact-us">Contact</a></li>
</ul>
$current_url = $_SERVER['REQUEST_URI'];
<script type="text/javascript">
$(document).ready(function(){
currentPageLink = '<?=$current_url?>';
$(".main-nav li").each(function(){
var listLink = $(this).children('a').attr("href");
$(this).removeClass('active');
if(currentPageLink == listLink){
$(this).addClass('active');
}
})
});
只需要弄清楚如何将活动类附加到定价页面。其他链接在点击时保持活动状态,但定价没有。看起来几乎每个答案都在Stack上,但找不到我的问题的答案。提前谢谢。
答案 0 :(得分:0)
$(document).ready(function(){
var path = "/"+location.pathname;
path = "/"+'try-now.php#demorequestform'; // this shall be deleted later !
$('a').each(function(index, element) {
console.log($(element).attr("href"));
console.log(path);
if($(element).attr("href") == path)
{
$(element).parent().addClass("active");
}else{
$(element).parent().removeClass("active");
}
});
});
&#13;
<style>
.active {
color:red;
background:green;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="mainMenu" class="main-nav">
<li class="nav-menu-dropdown-company active"><a class="company- btn" href="/company">Company</a>
<li><a href="/features">Features</a></li>
<li><a href="/try-now.php#demorequestform">Pricing</a></li>
<li><a href="/contact-us">Contact</a></li>
</ul>
&#13;
我认为这将是你的解决方案,因为测试的路径我联系运行时路径是正确的只是删除路径=&#34; / contact-us&#34;
更新:将父项添加到元素以使li成为最终目标 尝试在控制台中打印路径名
var path = "/"+location.pathname;
consloe.log(path);
了解问题是什么
仍然可以抓住#demorequestform
if(window.location.hash.substr(1); =="demorequestform" ) {
// make li active
} else {
// Fragment doesn't exist
}