2010-01-20 2 views
0

나는 메뉴가 있으며 현재 페이지에 대한 링크가 필요합니다. 나는 지금 문자열을 비교하는 정말 꼬인 방법을 사용합니다. 그러나 더 똑똑한 방법이 있다고 확신합니까? 당신이 당신의 링크에서 전체 경로 URL을 사용하지 않는 경우이 같은자바 스크립트를 사용하면 다음 링크를 통해 페이지를 비교할 수 있습니다.

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'); 
    } 
}); 
관련 문제