2017-10-07 54 views
2

我想通過點擊.switch並在.nightTextNight的樣式中應用.nightText來切換兩個圖標,我的JavaScript代碼在除此之外的任何地方都可以工作。在Javascirpt中切換innerHTML

任何人都可以提出任何其他方式讓它更簡單嗎? 因爲,對於每一個小小的變化,我需要創建兩個類,並給它一個ID。

var nightBtn = document.getElementById("switch"); 
 
var body = document.getElementById("body"); 
 
var header = document.getElementById("header"); 
 
var navbar = document.getElementById("navbar"); 
 
var nightText = document.getElementById("nightText"); 
 

 
function nightMode() { 
 
\t nightBtn.classList.toggle("switchNight"); 
 
\t body.classList.toggle("night"); 
 
\t navbar.classList.toggle("navbarNight"); 
 
\t nightText.classList.toggle("nightTextNight"); 
 
\t if(nightText.className = "nightTextNight") { 
 
\t \t nightText.innerHTML = "<i class='fa fa-sun-o' aria-hidden='true'></i>"; 
 
\t } else { 
 
\t \t nightText.innerHTML = "<i class='fa fa-moon-o' aria-hidden='true'></i>"; 
 
\t }; 
 
}
body { 
 
\t background-color: white; 
 
\t transition: background-color ease 0.3s; 
 
\t padding: 0; 
 
\t margin: 0; 
 
\t font-family: sans-serif; 
 
} 
 

 
.night { 
 
\t background-color: #3f4b5e; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
.switch { 
 
\t height: 35px; 
 
\t width: 35px; 
 
\t border-radius: 50%; 
 
\t background-color: #092d30; 
 
\t border: 3px solid wheat; 
 
\t float: right; 
 
\t z-index: 4; 
 
\t transition: background-color ease 1s; 
 
\t margin-top: 12px; 
 
\t margin-right: 4px; 
 
\t cursor: pointer; 
 
\t text-align: center; 
 
\t line-height: 17.5px; 
 
\t position: relative; 
 
} 
 

 
.switchNight { 
 
\t background-color: wheat; 
 
\t border: 3px solid #092d30; 
 
\t z-index: 4; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
.textNight { 
 
\t color: white; 
 
} 
 

 
.switch:hover { 
 
\t background-color: #4d5e77; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
.switchNight:hover { 
 
\t background-color: #fff2d8; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
/* --------------------- NAV BAR ------------------ */ 
 

 
.navbar { 
 
\t width: 100%; 
 
\t height: auto; 
 
\t background: #f4f7f9; 
 
\t position: fixed; 
 
\t margin-top: 0; 
 
\t padding: 0; 
 
\t border-bottom: 3px solid #2fb3f9; 
 
} 
 

 
.navbar li { 
 
\t list-style-type: none; 
 
\t display: inline; 
 
\t height: auto; 
 
} 
 

 
.navbar li a { 
 
\t padding: 20px 25px; 
 
\t text-decoration: none; 
 
\t display: inline-block; 
 
\t font-size: 20px; 
 
\t font-weight: bolder; 
 
\t color: #516f7f; 
 
} 
 

 
.navbar li a:hover { 
 
\t color: #ff9d00; 
 
\t transition: color ease 0.3s; 
 
} 
 

 
.navbarNight { 
 
\t background-color: #556bb5; 
 
\t border-bottom: 3px solid white; 
 
} 
 

 
.navbarNight li a { 
 
\t color: white; 
 
} 
 

 
.nightText { 
 
\t position: absolute; 
 
\t color: white; 
 
\t font-weight: bolder; 
 
\t top: 9px; 
 
\t right: 12px; 
 
} 
 

 
.nightTextNight { 
 
\t color: black; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <link rel="stylesheet" href="style.css"> 
 
\t <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"> 
 
\t <title>Night Mode - TEST</title> 
 
</head> 
 
<body id="body"> 
 
\t <div id="container"> 
 
\t \t <div id="nav"> 
 
\t \t \t <ul id="navbar" class="navbar"> 
 
\t \t \t \t <li><a href="#">Home</a></li> 
 
\t \t \t \t <li><a href="#">About me</a></li> 
 
\t \t \t \t <li><div id="switch" class="switch" onclick="nightMode()"><span id="nightText" class="nightText"><i class="fa fa-moon-o" aria-hidden="true"></i></span></div></li> 
 
\t \t \t </ul> 
 
\t \t </div> 
 
\t </div> 
 
\t <script src="script.js"></script> 
 
</body> 
 
</html>

+2

'如果(nightText.className = 「nightTextNight」)'應'如果(nightText.className ===「nightTextNight」)' – MarioZ

回答

1

它不工作,因爲你有兩類:

nightText nightTextNight 

所以你需要檢查:

if(nightText.className === "nightText nightTextNight") 

var nightBtn = document.getElementById("switch"); 
 
var body = document.getElementById("body"); 
 
var header = document.getElementById("header"); 
 
var navbar = document.getElementById("navbar"); 
 
var nightText = document.getElementById("nightText"); 
 

 
function nightMode() { 
 
\t nightBtn.classList.toggle("switchNight"); 
 
\t body.classList.toggle("night"); 
 
\t navbar.classList.toggle("navbarNight"); 
 
\t nightText.classList.toggle("nightTextNight"); 
 
\t if(nightText.className === "nightText nightTextNight") { 
 
\t \t nightText.innerHTML = "<i class='fa fa-sun-o' aria-hidden='true'></i>"; 
 
\t } else { 
 
\t \t nightText.innerHTML = "<i class='fa fa-moon-o' aria-hidden='true'></i>"; 
 
\t }; 
 
    console.log(nightText.className); 
 
}
body { 
 
\t background-color: white; 
 
\t transition: background-color ease 0.3s; 
 
\t padding: 0; 
 
\t margin: 0; 
 
\t font-family: sans-serif; 
 
} 
 

 
.night { 
 
\t background-color: #3f4b5e; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
.switch { 
 
\t height: 35px; 
 
\t width: 35px; 
 
\t border-radius: 50%; 
 
\t background-color: #092d30; 
 
\t border: 3px solid wheat; 
 
\t float: right; 
 
\t z-index: 4; 
 
\t transition: background-color ease 1s; 
 
\t margin-top: 12px; 
 
\t margin-right: 4px; 
 
\t cursor: pointer; 
 
\t text-align: center; 
 
\t line-height: 17.5px; 
 
\t position: relative; 
 
} 
 

 
.switchNight { 
 
\t background-color: wheat; 
 
\t border: 3px solid #092d30; 
 
\t z-index: 4; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
.textNight { 
 
\t color: white; 
 
} 
 

 
.switch:hover { 
 
\t background-color: #4d5e77; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
.switchNight:hover { 
 
\t background-color: #fff2d8; 
 
\t transition: background-color ease 1s; 
 
} 
 

 
/* --------------------- NAV BAR ------------------ */ 
 

 
.navbar { 
 
\t width: 100%; 
 
\t height: auto; 
 
\t background: #f4f7f9; 
 
\t position: fixed; 
 
\t margin-top: 0; 
 
\t padding: 0; 
 
\t border-bottom: 3px solid #2fb3f9; 
 
} 
 

 
.navbar li { 
 
\t list-style-type: none; 
 
\t display: inline; 
 
\t height: auto; 
 
} 
 

 
.navbar li a { 
 
\t padding: 20px 25px; 
 
\t text-decoration: none; 
 
\t display: inline-block; 
 
\t font-size: 20px; 
 
\t font-weight: bolder; 
 
\t color: #516f7f; 
 
} 
 

 
.navbar li a:hover { 
 
\t color: #ff9d00; 
 
\t transition: color ease 0.3s; 
 
} 
 

 
.navbarNight { 
 
\t background-color: #556bb5; 
 
\t border-bottom: 3px solid white; 
 
} 
 

 
.navbarNight li a { 
 
\t color: white; 
 
} 
 

 
.nightText { 
 
\t position: absolute; 
 
\t color: white; 
 
\t font-weight: bolder; 
 
\t top: 9px; 
 
\t right: 12px; 
 
} 
 

 
.nightTextNight { 
 
\t color: black; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <link rel="stylesheet" href="style.css"> 
 
\t <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"> 
 
\t <title>Night Mode - TEST</title> 
 
</head> 
 
<body id="body"> 
 
\t <div id="container"> 
 
\t \t <div id="nav"> 
 
\t \t \t <ul id="navbar" class="navbar"> 
 
\t \t \t \t <li><a href="#">Home</a></li> 
 
\t \t \t \t <li><a href="#">About me</a></li> 
 
\t \t \t \t <li><div id="switch" class="switch" onclick="nightMode()"><span id="nightText" class="nightText"><i class="fa fa-moon-o" aria-hidden="true"></i></span></div></li> 
 
\t \t \t </ul> 
 
\t \t </div> 
 
\t </div> 
 
\t <script src="script.js"></script> 
 
</body> 
 
</html>

PD:我添加console.log(nightText.className);來顯示類,你可以刪除它。

1

您可以只需撥動,而不是改變整個i標籤圖標類

var nightBtn = document.getElementById("switch"); 
var nightBtnIcon = document.getElementById("switch-icon"); 
var body = document.getElementById("body"); 
var header = document.getElementById("header"); 
var navbar = document.getElementById("navbar"); 

function nightMode() { 
    nightBtn.classList.toggle("switchNight"); 
    body.classList.toggle("night"); 
    navbar.classList.toggle("navbarNight"); 
    nightBtnIcon.classList.toggle("fa-sun-o"); 
    nightBtnIcon.classList.toggle("fa-moon-o"); 
} 

HTML:

<ul id="navbar" class="navbar"> 
       <li><a href="#">Home</a></li> 
       <li><a href="#">About me</a></li> 
       <li><div id="switch" class="switch" onclick="nightMode()"><i class="icon fa fa-moon-o" aria-hidden="true" id=「switch-icon」></i></span></div></li> 
      </ul> 
+0

仍然無法正常工作,我複製並粘貼確切的東西,但仍然是同樣的問題。 –

0

什麼包裝的所有元素那可能有night樣式在div中或甚至在body中不同,然後僅將night類應用於該包裝。

然後,您可以不同的風格,以你的元素添加的線沿線的:

.night .navbar { ... just night-related style differences ... }

然後,您切換將只需要從包裝元素添加或刪除類。您仍然需要爲那些您想要更改的內容添加新類,但這隻會針對您想要更改的屬性。

0

忽略id來使用Attributeloopdata-mode 看到這個例子中,最好的方法:

\t var modes={ 
 
\t day:{ 
 
\t  switch:"switch", body:"day", navbar:"navbar",icon:"fa fa-moon-o" 
 
\t \t }, 
 
\t night:{ 
 
\t  switch:"switchNight switch", body:"night day", navbar:"navbarNight navbar",icon:"fa fa-sun-o" 
 
\t } 
 
\t } 
 
\t function changeMode(arg){ 
 
\t \t window.mode=arg; 
 
\t \t var elem=document.querySelectorAll('[data-mode]'); 
 
\t \t for (var i=0; i<elem.length; i++){ 
 
\t \t \t var key=elem[i].getAttribute("data-mode"); 
 
\t \t \t elem[i].classList=modes[arg][key]; 
 
\t \t } 
 

 
\t } 
 
\t window.mode="day";
\t body { 
 
\t \t background-color: white; 
 
\t \t transition: background-color ease 0.3s; 
 
\t \t padding: 0; 
 
\t \t margin: 0; 
 
\t \t font-family: sans-serif; 
 
\t } 
 

 
\t .night { 
 
\t \t background-color: #3f4b5e; 
 
\t \t transition: background-color ease 1s; 
 
\t } 
 

 
\t .switch { 
 
\t \t height: 35px; 
 
\t \t width: 35px; 
 
\t \t border-radius: 50%; 
 
\t \t background-color: #092d30; 
 
\t \t border: 3px solid wheat; 
 
\t \t float: right; 
 
\t \t z-index: 4; 
 
\t \t transition: background-color ease 1s; 
 
\t \t margin-top: 12px; 
 
\t \t margin-right: 4px; 
 
\t \t cursor: pointer; 
 
\t \t text-align: center; 
 
\t \t line-height: 17.5px; 
 
\t \t position: relative; 
 
\t } 
 

 
\t .switchNight { 
 
\t \t background-color: wheat; 
 
\t \t border: 3px solid #092d30; 
 
\t \t z-index: 4; 
 
\t \t transition: background-color ease 1s; 
 
\t } 
 

 
\t .textNight { 
 
\t \t color: white; 
 
\t } 
 

 
\t .switch:hover { 
 
\t \t background-color: #4d5e77; 
 
\t \t transition: background-color ease 1s; 
 
\t } 
 

 
\t .switchNight:hover { 
 
\t \t background-color: #fff2d8; 
 
\t \t transition: background-color ease 1s; 
 
\t } 
 

 
\t /* --------------------- NAV BAR ------------------ */ 
 

 
\t .navbar { 
 
\t \t width: 100%; 
 
\t \t height: auto; 
 
\t \t background: #f4f7f9; 
 
\t \t position: fixed; 
 
\t \t margin-top: 0; 
 
\t \t padding: 0; 
 
\t \t border-bottom: 3px solid #2fb3f9; 
 
\t } 
 

 
\t .navbar li { 
 
\t \t list-style-type: none; 
 
\t \t display: inline; 
 
\t \t height: auto; 
 
\t } 
 

 
\t .navbar li a { 
 
\t \t padding: 20px 25px; 
 
\t \t text-decoration: none; 
 
\t \t display: inline-block; 
 
\t \t font-size: 20px; 
 
\t \t font-weight: bolder; 
 
\t \t color: #516f7f; 
 
\t } 
 

 
\t .navbar li a:hover { 
 
\t \t color: #ff9d00; 
 
\t \t transition: color ease 0.3s; 
 
\t } 
 

 
\t .navbarNight { 
 
\t \t background-color: #556bb5; 
 
\t \t border-bottom: 3px solid white; 
 
\t } 
 

 
\t .navbarNight li a { 
 
\t \t color: white; 
 
\t } 
 

 
\t .nightText { 
 
\t \t position: absolute; 
 
\t \t color: white; 
 
\t \t font-weight: bolder; 
 
\t \t top: 9px; 
 
\t \t right: 12px; 
 
\t } 
 

 
\t .nightTextNight { 
 
\t \t color: black; 
 
\t }
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <link rel="stylesheet" href="style.css"> 
 
\t <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"> 
 
\t <title>Night Mode - TEST</title> 
 
</head> 
 
<body data-mode="body"> 
 
\t <div> 
 
\t \t <div> 
 
\t \t \t <ul class="navbar" data-mode="navbar"> 
 
\t \t \t \t <li><a href="#">Home</a></li> 
 
\t \t \t \t <li><a href="#">About me</a></li> 
 
\t \t \t \t <li><div class="switch" data-mode="switch" onclick="changeMode(mode=='night'?'day':'night');"> 
 
\t \t \t \t \t <span class="nightText"> 
 
\t \t \t \t \t \t <i class="fa fa-moon-o" data-mode="icon"></i> 
 
\t \t \t \t \t </span> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </li> 
 
\t \t \t </ul> 
 
\t \t </div> 
 
\t </div> 
 
\t <script src="script.js"></script> 
 
</body> 
 
</html>