2017-10-07 1 views
2

.switchをクリックして2つのアイコンの切り替えを行い、.nightTextNightのスタイルを.nightTextに適用すると、JavaScriptコードがここ以外で動作します。JavascirptでinnerHTMLを切り替える

誰も私にそれを簡単にする他の方法を提案することができますか? 小さな変更ごとに2つのクラスを作成して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

' 'if(nightText.className ===" nightTextNight ")' – MarioZ

答えて

1
次の2つのクラス持っているので、それが働いていなかった

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

何についてのすべての要素を包みますそれはを持つ可能性がありますのスタイルをdiv内、または本体内で異なっていて、nightクラスをそのラッパーに適用するだけです。

あなたは、その後の線に沿って、あなたの要素に異なるスタイルを追加することができます。

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

次に、あなたのトグルのみラッピング要素からクラスを追加または削除する必要があります。変更したいものに対しては、まだ新しいクラスを追加する必要がありますが、変更したいプロパティに対してのみクラスを追加する必要があります。

0

Attributeloopこの例を参照 data-mode等を使用するIDを無視するための最良の方法:(nightText.className = "nightTextNight")は `でなければならない場合

\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>