Programing Language/Javascript

[Javascript] classList ์ •์˜์™€ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

JHeaon ใƒป 2023. 3. 21. 02:35


๐Ÿ“š classList ๋ž€?

javascript์—์„œ classList๋Š” element์˜ ์ฝ๊ธฐ์ „์šฉ ์†์„ฑ์œผ๋กœ css class์˜ ํ˜„์žฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. classList์„ ํ†ตํ•ด์„œ element์— css class ์ถ”๊ฐ€, ์ œ๊ฑฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๐Ÿ“’ ์‚ฌ์šฉ ์˜ˆ์‹œ

const classes = element.classList;

 

 

 

 

๐Ÿ“š classList class ์ถ”๊ฐ€, ์‚ญ์ œ

element์˜ class ๋ชฉ๋ก์— ํ•˜๋‚˜ ์ด์ƒ์˜ css class๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด classList์˜ add() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์‚ญ์ œ๋Š” remove() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 

๐Ÿ“’ ์‚ฌ์šฉ ์˜ˆ์‹œ

let div = document.querySelector('#content');
div.classList.add('info');

let div = document.querySelector('#content');
div.classList.remove('visible');

 

 

 

๐Ÿ“š classList class toggle()

toggle()์„ ํ†ตํ•ด์„œ css์„ ์Šค์œ„์น˜ ์ฒ˜๋Ÿผ ๊ป๋‹ค, ์ผฐ๋‹ค ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

let div = document.querySelector('#content');

div.addEventListner("click", function(){
	div.classList.add('info');;
});

'Programing Language/Javascript'์˜ ๋‹ค๋ฅธ๊ธ€

  • ํ˜„์žฌ๊ธ€ [Javascript] classList ์ •์˜์™€ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

๊ด€๋ จ๊ธ€