我试图将类添加到某些页面的body
。除了第2,第3和第4 if语句之外,所有类都被添加,如下所示。我想也许是因为if语句的排序?我不确定。有人可以帮助我吗?
if (window.location.href.match(/\/shop\/\?category/)) {
jQuery('body').addClass('shop-category');
} else if (window.location.href.match(/\/shop\/\?category=Chef/)) { //doesn't get added
jQuery('body').addClass('shop-category-chef');
} else if (window.location.href.match(/\/shop\/\?category=Tactical/)) { //doesn't get added
jQuery('body').addClass('shop-category-tactical');
} else if (window.location.href.match(/\/shop\/\?category=Tools/)) { //doesn't get added
jQuery('body').addClass('shop-category-tools');
} else if (window.location.href.match(new RegExp('/shop/.+')) ) {
jQuery('body').addClass('shop-item');
} else if (window.location.href.match('/shop/')) {
jQuery('body').addClass('shop');
}
答案 0 :(得分:2)
订单是问题所在。 ?category
之前的?category=Chef
匹配。只需更改订单。
if (window.location.href.match(/\/shop\/\?category=Chef/)) {
jQuery('body').addClass('shop-category-chef');
} else if (window.location.href.match(/\/shop\/\?category=Tactical/)) {
jQuery('body').addClass('shop-category-tactical');
} else if (window.location.href.match(/\/shop\/\?category=Tools/)) {
jQuery('body').addClass('shop-category-tools');
} else if (window.location.href.match(/\/shop\/\?category/)) {
jQuery('body').addClass('shop-category');
} else if (window.location.href.match(new RegExp('/shop/.+'))) {
jQuery('body').addClass('shop-item');
} else if (window.location.href.match('/shop/')) {
jQuery('body').addClass('shop');
}