简体   繁体   中英

Changing CSS when scrolling (working in console and JSFiddle but not in my HTML?)

I have some weird problem:

I want to change the text color of my menu when scrolling. It is working in JSFiddle and in the console but not in my HTML...:

But changing the background when scrolling DOES work!

The thing is, that I need to override a style in SemanticUI that uses !important and I have to use .querySelector().setAttribute() instead of .getElementById().style.color because it seems to not support adding !important to a color.

Here's the link to my project (I hope npm forever will stay online long enough...)

Is there anything I'm missing? Thank you in advance!

You have two elements with same id #activeItem

在此处输入图片说明

You need to update selector to select right item:

document.querySelector('#dMenu #activeItem').setAttribute('style', 'color: #744a84 !important');

As mentioned in the above answer by @taras-d , you have two elements with same id. If you want to change color of both elements means you need to get all elements and apply css to it.

var _activeElements = $(".nav_bar #activeItem");
_activeElements.each(function(index) {
    _activeElements[index].setAttribute('style', 'color: #744a84 !important');
});

Add some dummy class like nav_bar to upper element to select all elements

<div class="right menu nav_bar" id="rightMenu">
...
</div>

Add nav_bar class to another #activeItem parent element, then it will iterate to all elements and apply css

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM