2014-02-10 97 views
0

我有一个页面header.html,我用它作为每个网页的标题。标题中有菜单项。我想使用javascript/jquery动态地突出显示当前的网页菜单。谁能帮我?如何在每个页面上突出显示菜单项dynamicaly?

HTML

<ul class="navigation"> 
<li class="highlight"><a href="index.html" class="highlight">Home</a></li> 
<li><a href="subnet.html">IP Discovery and Password Management</a></li> 
<li><a href="test_management.html">Test Management</a></li> 
<li><a href="test_dut.cgi">DUT Management</a></li> 
<li><a href="test_execmain.cgi">Test Execution</a></li> 
<li><a href="result.cgi">Results</a></li> 
</ul> 
+0

请告诉我们你到目前为止尝试了些什么。 – Sgoldy

+0

你使用哪种服务器端语言?或者你在使用ajax?你如何在每个页面中实现标题?这是相当重要的信息... –

+0

@KristofFeys我使用的HTML有一些Perl集成。我打电话给其他页面。 &不使用ajax – user3217945

回答

1

首先找到使用window.location正则表达式的页面,然后

jQuery(function($){ 
    var page = window.location.href.match(/[^/]+$/)[0]; 
    $('.navigation li a[href="' + page + '"]').parent().addBack().addClass('highlight') 
}) 
1

试试这个

<script type="text/javascript"> 
    jQuery(document).ready(function($){ 
     // Get current url 
     // Select an a element that has the matching href and apply a class of 'active'. Also prepend a - to the content of the link 
     var url = window.location.href; 
     $('.menu a[href="'+url+'"]').addClass('current_page_item'); 
    }); 
</script> 

参考:http://www.paulund.co.uk/use-jquery-to-highlight-active-menu-item

相关问题