특정 URL에서 내 메뉴의 활성 상태를 만들었습니다. 나는이 같은 URL이 :활성 상태로 만들기 위해 href 태그가있는 URL 폴더를 일치 시키십시오.
/products/other-clothing/sporting/adults-anzac-australia-polo
/products/other-clothing/sporting/adults-nz-tee
/products/bags/backpacks
내 코드가 그것은 잘 작동 등의 /
그래서 다른-의류, 스포츠 후에서 폴더를 얻는다 , 나는 단지 코드를 작성하는보다 효율적인 방법이 있다고 가정한다.
여기 내 코드입니다 :
jQuery(".product-nav li a").each(function() {
// URL url
var cat = location.pathname.split("/")[2];
var subcat = location.pathname.split("/")[3];
var c = "/products/" + cat + "/" + subcat;
// A tag url
var acat = this.href.split("/")[4];
var asubcat = this.href.split("/")[5];
var e = "/products/" + acat + "/" + asubcat;
if(e == c) {
jQuery(this).parent().addClass("active");
jQuery(this).parent().parent().parent().addClass("active");
}
});
사람이 큰 것 코드를 작성하는 청소기 방법을 제공 할 수 있습니다. 아마 "/products/" +
이 필요 없습니다.
그런 배열은 비교할 수 없습니다! :) –
맞습니다, Daniel. 나는 당신이 당신의 코멘트를 올릴 때 그것을 고치고 있었다. ;) –
이것은 더 좋고 더 짧아 보일 수 있습니다. 귀하의 의견에 감사 드리며 내일 이것을 구현하려고합니다 :-) – Luke