@id_nomer_odin1

Как поменять иконку луны на иконку солнца, при нажатии на кнопку смены темы?

<button onclick="test()" class="theme-button bx bx-moon" id="theme-button"></button>


var btn = document.getElementById("theme-button");
var link = document.getElementById("theme-link");

btn.addEventListener("click", function () { ChangeTheme(); });

function ChangeTheme()
{
    let lightTheme = "styles/light.css";
    let darkTheme = "styles/dark.css";

    var currTheme = link.getAttribute("href");
    var theme = "";

    if(currTheme == lightTheme)
    {
     currTheme = darkTheme;
     theme = "dark";
    }
    else
    {    
     currTheme = lightTheme;
     theme = "light";
    }

    link.setAttribute("href", currTheme);

}

function test() {
    if(btn.classList.contains('bx-moon')){
        btn.classList.remove('bx-moon')
        btn.classList.add('bx-sun')
        btn.classList.add('bx')
    }

    if(btn.classList.contains('bx-sun')){
        btn.classList.remove('bx-sun')
        btn.classList.add('bx-moon')
        btn.classList.add('bx')
    }
}
  • Вопрос задан
  • 122 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы