2010-01-20 50 views
0

我有一個菜單,我需要當前頁面的鏈接,我將突出顯示不同。我現在做了一個比較字符串的非常扭曲的方式,但我確定有一個更聰明的方法?Javascript的比較頁面您正在通過鏈接

jQuery(document).ready (function(){ 
     jQuery(".filteroptions .option").each (function (index,ele) 
     { 
       link=jQuery(ele).find("a").attr("href").toString(); 
       p=jQuery(window).attr("location").toString().search(link) 
       if (p!=-1){ 
         jQuery(ele).find(".link").addClass("current") 
       } 
     }); 
    }); 

回答

1

像這樣的工作...

$('a').each(function(){ 
    if ($(this).attr('href') == window.location.href) { 
     $(this).addClass('current'); 
    } 
}); 

或者,如果你不使用你的鏈接完整路徑網址...

$('a').each(function(){ 
    if (window.location.href.indexOf($(this).attr('href')) !== -1) { 
     $(this).addClass('current'); 
    } 
});