JQuery: Stato attivo: addclass a un link che contiene il documento.href come href
-
14-11-2019 - |
Domanda
Mi sto chiedendo se è possibile aggiungere ad un link, che ha lo stesso href come documento.href?
Ho provato, ma senza fortuna.
if ($("a").attr("href") == document.location.href) {
$(this).addClass("active");
}
.
non è possibile ??
Soluzione
$("a").filter(function() {
return this.href === document.location.href;
}).addClass("active");
Should work.
Altri suggerimenti
$("a[href=" + document.location.href + "]").addClass("active");
(not tested)
Have you tried window.location.href instead of document.location.href?
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow