示例链接:http://localhost/test/page.php
我有一段 JavaScript 代码,它将放置一个active
导航栏的类(如果该导航栏的 url)href ==== current_url
.
当前的 JavaScript(仅将活动类放入侧边栏)
<script type="text/javascript">
jQuery(function($) {
var path = window.location.href; // because the 'href' property of the DOM element is the absolute path
$('ul a').each(function() {
if (this.href === path) {
$(this).addClass('sub-menu active');
$(this).parent().closest("li").addClass('active');
$(this).parent().parent().closest("li").addClass('active');
}
});
});
</script>
如果链接有一个,我也想让它工作page.php?success
。还会有一个活跃的班级,只要page.php
是否存在,无论之后是什么?
.
我已经尝试了下面的脚本来提取基本名称,但现在它根本不起作用,无论有或没有页面?
.
尝试过的脚本(即使网址有 page.php,也应该将活动类放在侧边栏)?
成功在其中。
<script type="text/javascript">
jQuery(function($) {
var patharray = window.location.pathname.split( '/' );
var reallink = patharray[2];
$('ul a').each(function() {
if (this.href === reallink) {
$(this).addClass('sub-menu active');
$(this).parent().closest("li").addClass('active');
$(this).parent().parent().closest("li").addClass('active');
}
});
});
</script>
通过上面的示例链接。脚本返回page.php
还有href
导航栏内的只是page1.php
, page2.php
等等...所以我知道它应该可以工作,因为检索到reallink
等于href
导航栏的。
我的侧边栏
<li class="sub-menu"> // Sidebar with no submenu
<a class="" href="page1.php">
<i class="icon-calendar"></i>
<span>This is page 1</span>
</a>
</li>
<li class="sub-menu"> // Sidebar with a submenu
<a href="javascript:;" class="">
<i class="icon-group"></i>
<span>This has sub pages</span>
<span class="arrow"></span>
</a>
<ul class="sub">
<li><a class="" href="page2.php">This is page 2</a></li>
<li><a class="" href="page3.php">This is page 3</a></li>
</ul>
</li>
因此,如果满足 href = url,第一个 JavaScript 就会向父级和子级放置一个活动类。但对于第二个脚本,没有任何作用。