2015-01-21 62 views
0

我有下面的javascript代碼來將我的菜單項放到活動類中,當我在頁面上時,例如https://stackoverflow.com/questions/它有效。但我也想把菜單項放到課程上,當我在https://stackoverflow.com/questions/ask/這樣的子頁面時,我可以知道該怎麼做?從子頁面獲取父頁面的URL

<script> 
     var url = window.parent.location.href; 
     $('ul.nav a[href="'+ url +'"]').parent().addClass('active'); 

     $('ul.nav a').filter(function() { 
     return this.href == url; 
     }).parent().addClass('active'); 
    </script> 

回答

0

您可以改用String.prototype.match

<script> 
    var url = window.parent.location.href; 
    $('ul.nav a[href="'+ url +'"]').parent().addClass('active'); 

    $('ul.nav a').filter(function() { 
    return (url.match(this.href) !== null); 
    }).parent().addClass('active'); 
</script>