我想添加類來鏈接,如果它是當前的URL。如何將類添加到鏈接,如果它是當前的URL?
URL結構如下:http://localhost/search?date=2010&number_of_books=10
;
/search?query1&query2&query3...
我可以添加類與代碼 '當前URL鏈接':
<script>
$(document).ready(function($) {
$("a").each(function() {
var href = $(this).attr('href');
var current_url = location.pathname + location.search;
if (current_url == href) {
$(this).addClass('active');
}
});
});
</script>
如何檢查,如果當前頁面URL(搜索參數)和一個鏈接的URL(搜索參數)相同,那麼我可以添加一個類到這個鏈接?
例如,如果我在頁面上:/search?date=2010&author=Adam+Smith
,如果鏈接href是/search?author=Adam+Smith&date=2010
,那麼我想要添加類到這個鏈接。 (注意:考慮到兩個鏈接都有相同的參數)。
基本上你需要檢查鏈接的查詢參數,請參見下面的我的文章的參考。 –