使用jQuery对父URL进行粗体链接(在wordpress上)

时间:2014-10-03 19:44:11

标签: javascript jquery wordpress

我已经使用了这个(见下面的代码)来加粗当前页面的链接。例如 http://somewebsite.com/parent-page/current-page(这些部分的名称用作示例)

这会将指向当前页面的所有链接加粗,但我想将其修改为粗体指向父页面的所有链接(http://somewebsite.com/parent-page):

jQuery(document).ready(function( $ ) {

       $("header a").each(function(){
               if ($(this).attr("href") == window.location.pathname){
                       $(this).addClass("current-second-menu");
               }
       });
});

1 个答案:

答案 0 :(得分:1)

只是一个小小的改变

jQuery(document).ready(function( $ ) {

       $("header a").each(function(){
               // if ($(this).attr("href") == window.location.pathname){
               if ($(this).attr("href") == window.location.pathname.slice(0, window.location.pathname.lastIndexOf('/'))){
                       $(this).addClass("current-second-menu");
               }
       });
});