<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<title>KOHARIN</title>
<style>
.dark{
background-color:black;
color:white;
}
</style>
</head>
<body>
<h2>🌞Day and 🌙Night Mode</h2><br>
<h4>Which color? Black or White??</h4>
<p>
<button type="button" class="btn btn-dark" id="dnn">Night</button>
<label>👈 click button to change mode.</label>
</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
$(function(){
$('#dnn').on('click', function(e){
$('body').toggleClass('dark'); // toggleClass: body 요소에 dark 클래스가 있으면 추가, 없으면 제거한다.
//$('button').toggleClass('btn btn-dark');
var name = $('#dnn').attr('class');
var reverse = (name === 'btn btn-dark' ? 'btn btn-light' : 'btn btn-dark');
$('#dnn').removeClass(name).addClass(reverse);
$('#dnn').text(
reverse === "btn btn-dark" ? "Night" : "Day");
});
});
</script>
</body>
</html>
생각해보자. 🤔🤔
생각 후 기능을 추가하여 점점 개선시킬 예정이다.
Fetch API 통한 Ajax 구현 #1 (0) | 2021.02.24 |
---|---|
jQuery #2 : jQuery & Ajax (XMLHttpRequest) (0) | 2021.02.21 |
jQuery #1 (0) | 2021.02.18 |
[Web 개발] Day and Night Mode #1 (jQuery, Bootstrap) (0) | 2021.02.02 |
[JavaScript] JavaScript #5: 실행 컨텍스트 (0) | 2021.02.01 |