โจ ๊ณผ์ . ์ง๋์๊ฐ์ ๋ง๋ค์๋ script๋ฅผ querySelector ์ querySelectorAll์ ์ฌ์ฉํด์ ๋ฐ๊ฟ๋ณด์.
<script>
document.querySelector('.navbar-toggler').addEventListener('click', function(){
//1. querySelector
document.querySelector('#list1').classList.toggle('show');
//2. querySelectorAll
document.querySelectorAll('.list-group')[0].classList.toggle('show');
});
</script>
HTML ์กฐ์๋ฌธ๋ฒ์ ์ฝ๊ฒ ๋ฐ๊ฟ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค
jQuery , React, Vue, Angular
๊ทธ ์ค์ ์ฌ์ด jQuery๋ฅผ ์ฌ์ฉํด๋ณด์.
์์ javascript ์ฝ๋๋ฅผ jQuery๋ก ๋ณํํ ์์
<script>
$('.navbar-toggler').on('click', function(){
$('.list-group').toggleClass('show');
$('.list-group').fadeToggle('show');
$('.list-group-item').html('items');
$('.list-group-item').css('color', 'rgb(255,0,220)');
});
</script>
๐jQuery ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ๋ฐฉ๋ฒ
jquery cdn ๊ฒ์
๋ณต์ฌ ํ ์ฝ๋๋ฅผ head์ ์ฝ์
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
</head>
๐์ค์ ๋จ์ถํค๋ Ctrl + Alt + S
โจ ๊ณผ์ . ๋ฒํผ ๋๋ฅด๋ฉด modal ์ฐฝ ๋์ฐ๊ธฐ (jQuery ์ฌ์ฉํ๊ธฐ)
<div class="black-bg">
<div class="white-bg">
<h4>๋ก๊ทธ์ธํ์ธ์</h4>
<button class="btn btn-danger" id="close">๋ซ๊ธฐ</button>
</div>
</div>
.black-bg {
width : 100%;
height : 100%;
position : fixed;
background : rgba(0,0,0,0.5);
z-index : 5;
padding: 30px;
}
.white-bg {
background: white;
border-radius: 5px;
padding: 30px;
}