简体   繁体   中英

How can I use Toggle Function with NodeList/Array to toggle dark mode in all sections with same class?

This is the HTML Code:

<div class="page" id="page-home">
        <!-- Blueprint header -->
        <header class="header cf">
            <img class="poster" src="images/Logo_square.png" style="" alt="Logo" />
            <h1 class="header__title">Experts in .......</h1>
            <br>
            <p class="header__desc">Let's get &#x27B2; <a id="quote">Connected</a></p>
            
            <button onclick="myFunction()">Toggle dark mode</button>
        </header>

    </div>

    <div class="page" id="page-services">....</div>
    <div class="page page--inactive" id="page-aboutus">.....</div>
    <div class="page page--inactive" id="page-contactus">...</div>
    <div class="page page--inactive" id="page-projects">....</div>
    <div class="page page--inactive" id="page-history">.....</div>

I want to make the toggle button change all the classes with class=" page" and class="page page--inactive" to class=" page page_dark" and class=" page page_dark page--inactive" where I've set the dark mode colors. I've tried the below-mentioned code but it does not work

function myFunction() {
    document.body.classList.toggle("body_dark");
    var page_list = document.querySelectorAll('.page');
    var page_array = [page_list]; // converts NodeList to Array
    
    for(var i = 0 ; i < page_array.length; i++){
        console.log(page_array[i]);
        page_array[i].classList.toggle("page_dark");        
    }
}

The stylesheet goes as follows:

.body_dark{
    color: #bdbdbd;
    background: #1d1e21 !important;
}

body {
    font-family: 'Avenir Next', Avenir, 'Helvetica Neue', 'Lato', 'Segoe UI', Helvetica, Arial, sans-serif;
    margin: 0;
    color: #696969;
    background: #e7e7e7;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-y:auto;
}
.js .page.page_dark {
    background: linear-gradient(#333 -2%, #000000 1%);
}

.js .page {
    position: relative;
    z-index: 5;
    overflow: auto;
    width: 100%;
    height: 100vh;
    pointer-events: auto;
    background: linear-gradient(#eee -2%, #ffffff 1%);
    box-shadow: 0 -1px 10px rgba(0, 0, 0, 0.1);
}

If you may need to view any more details regarding code please comment below, I would be glad to share it. I have not used id as there are font colors and changes with content on other pages where updating individually with different id would be a headache.

Sir, I changed little bit.

Converting to array should be with... spread operator.

function myFunction() {
    document.body.classList.toggle("body_dark");
    var page_list = document.querySelectorAll(".page");
    var page_array = [...page_list]; // converts NodeList to Array
    page_array.forEach((page) => {
      page.classList.toggle("page_dark");
    });
  }

If you are using IE, please check this on Chrome or FF

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