Programming/Html

[Html] document.getElementsByTagName() ์‚ฌ์šฉํ•˜์—ฌ css ์Šคํƒ€์ผ ์ˆ˜์ • ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๋ฐฉ๋ฒ•

yuri lee 2023. 2. 20. 22:15
๋ฐ˜์‘ํ˜•

Intro

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” document.getElementsByTagName()๋ฅผ ์‚ฌ์šฉํ•ด์„œ css styles์„ ์—…๋ฐ์ดํŠธ/์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

How to solve the problem

๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ 2๊ฐ€์ง€ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

#1

๋ชจ๋“  blockqutoe ํƒœ๊ทธ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๊ธธ์ด๋งŒํผ loop๋ฅผ ๋Œ๋ฉด์„œ ์Šคํƒ€์ผ์„ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„  margin์„ 0, borderLeft ์Šคํƒ€์ผ์„ '5px solid rgb(9 9 9)'๋กœ ์ˆ˜์ •ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

function changeBlockquote() {
    let li_tags = document.getElementsByTagName('blockquote');
    for (let i = 0; i < li_tags.length; i++) {
        li_tags[i].style.margin = String(0);
        li_tags[i].style.borderLeft = '5px solid rgb(9 9 9)';
    }
}

 

#2

๋ชจ๋“  ul ํƒœ๊ทธ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๊ธธ์ด๋งŒํผ loop๋ฅผ ๋Œ๋ฉด์„œ list-style๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค. 

function changeUl() {
    let li_tags = document.getElementsByTagName('ul');
    for (let i = 0; i < li_tags.length; i++) {
        li_tags[i].style.listStyle = 'disc';
    }
}

 


https://stackoverflow.com/questions/74391887/update-css-styles-using-getelementsbytagname

๋ฐ˜์‘ํ˜•