I have a navigation bar that changes the src iframe, how can I change the color of the active link?

My website has a navigation bar that, when clicked, points to an iframe. I would like the “active” link color to be changed.

PS cannot use jQuery, only JS and CSS.

my idea was to have an identifier named ACTIVE and activate the active link with a click after removing the identifier from the rest.

  • although I have no idea how to do this.

on the other hand, the other option is to do the same only using the ACTIVE class, my problem is that each link has an allready class assigned by id, and I don't know how to remove one class from two.

+3
source share
1 answer

, . jQuery, JavaScript - :

document.getElementById("someID").className += " newClass";

regex:

document.getElementById("someID").className = document.getElementById("someID").className.replace(/\bnewClass\b/,'');

, :

onclick="changeColor(this)"

:

var activeLink;

function changeColor(elem){
    if(activeLink)
        activeLink.className = activeLink.className.replace(/\bnewClass\b/,'');
    elem.className += " newClass";
    activeLink = elem;
}
0

Source: https://habr.com/ru/post/1795740/


All Articles