์ง๋ ์๊ฐ์ ์์ฑํ๋ ์ฝ๋์ ์ด์ด์ querySelector ๋ผ๋ ์ ํ์๋ฅผ ์์๋ณด์.
Copy < ul class = "list-group" id = "list1" >
< li class = "list-group-item" >An item</ li >
< li class = "list-group-item" >A second item</ li >
< li class = "list-group-item" >A third item</ li >
< li class = "list-group-item" >A fourth item</ li >
< li class = "list-group-item" >And a fifth one</ li >
</ ul >
< script >
document .querySelector ( '.list-group' )
document .querySelector ( '#list1' )
document .querySelectorAll ( '.list-group-item' )[ 4 ]
</ script >
querySelector('.list-group') ๋ผ๊ณ ํ๋ฉด class๊ฐ list-group ์ธ ์์๋ฅผ ์ฐพ์์ค๋ค.
querySelector('#list1') ๋ผ๊ณ ํ๋ฉด id๊ฐ list1 ์ธ ์์๋ฅผ ์ฐพ์์ค๋ค.
css์์ . ์ด ํด๋์ค #์ด id ๋ผ๋ ๋ป์ด๋ค.
ํ์ง๋ง querySelector๋ ์ฒซ๋ฒ์งธ ์์๋ง ์ฐพ์์ค๋ค.
Copy document .querySelector ( '.list-group-item' )
์ ์ฝ๋๋ ์ฒซ๋ฒ์งธ list-group-item ๋ง ์ฐพ์์ค๋ค.
Copy < li class = "list-group-item" >An item</ li >
๊ทธ๋์ ๋์ผํ ํด๋์ค ๋ช
์ด ์ค๋ณตํด์ ์ฌ์ฉ๋ ๋์๋
Copy document .querySelectorAll ( '.ํด๋์ค์ด๋ฆ)' [n]
์ด๋ ๊ฒ ํ๋ฉด ํด๋น ํด๋์ค ์ด๋ฆ์ n๋ฒ์งธ ์์๋ฅผ ์ฐพ์์ค๋ค.
โจ ๊ณผ์ . ์ง๋์๊ฐ์ ๋ง๋ค์๋ script๋ฅผ querySelector ์ querySelectorAll์ ์ฌ์ฉํด์ ๋ฐ๊ฟ๋ณด์.
Copy < script >
document.getElementsByClassName('navbar-toggler')[0].addEventListener('click', function() {
//๋ฒํผ ํด๋ฆญ ์ show class ์ถ๊ฐ
document .getElementsByClassName ( 'list-group' )[ 0 ]. classList .add ( 'show' );
//๋ฒํผ ํด๋ฆญ ์ show class Toggle
document .getElementsByClassName ( 'list-group' )[ 0 ]. classList .toggle ( 'show' );
})
</ script >