์ƒˆ์†Œ์‹

Languages/Java Script

[Js] ๋ฌธ์ž์—ด๋กœ ๋…ธ๋“œ ์ œ์–ด

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ [์ƒํ™œ์ฝ”๋”ฉ] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์˜์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "egoing Lee"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 

์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋†“๋Š” ๋ชฉ์ ์œผ๋กœ ํฌ์ŠคํŒ… ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

www.inflearn.com/course/javascript-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EA%B0%95%EC%A2%8C#

 

์›น๋ธŒ๋ผ์šฐ์ € Javascript (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ) - ์ธํ”„๋Ÿฐ

์ƒํ™œ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ ์ž…๋‹ˆ๋‹ค. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ณต๋ถ€ํ•ด ๋ด…๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด JavaScript ์˜จ๋ผ์ธ ๊ฐ•์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ

www.inflearn.com


์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ๊ฐ•์˜์˜ "Node๊ฐ์ฒด" ์ค‘ ๋ฌธ์ž์—ด๋กœ ๋…ธ๋“œ ์ œ์–ด ๊ฐ•์˜๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.



๋ฌธ์ž์—ด๋กœ ๋…ธ๋“œ ์ œ์–ด

๋…ธ๋“œ ๋ณ€๊ฒฝ API์—์„œ๋Š” ์—ฌ๋Ÿฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋…ธ๋“œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฐฉ์‹์€ ๋ณต์žกํ•˜๊ณ  ์žฅํ™ฉํ•˜๋‹ค. ์ข€ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ๋…ธ๋“œ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

 

innerHTML

innerHTML๋Š” ๋ฌธ์ž์—ด๋กœ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ๋˜ํ•œ ์ž์‹ ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ฝ์–ด์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

์ฝ๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ํƒ€๊ฒŸ์ด ๋˜๋Š” ์—˜๋ฆฌ๋จผํŠธ์˜ ํ•˜์œ„ ์—˜๋ฆฌ๋จผํŠธ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ฌธ์ž๋ฅผ ํ†ตํ•ด ์ง€์ •์„ ํ• . ์ˆ˜ ์žˆ๋‹ค.

<ul id="target">
    <li>HTML</li>
    <li>CSS</li>
</ul>
<input type="button" onclick="get();" value="get" />
<input type="button" onclick="set();" value="set" />
<script>
    function get(){
        var target = document.getElementById('target');
        alert(target.innerHTML);
    }
    function set(){
        var target = document.getElementById('target');
        target.innerHTML = "<li>JavaScript Core</li><li>BOM</li><li>DOM</li>";
    }
</script>

 

outerHTML

outerHTML์€ ์„ ํƒํ•œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํฌํ•จํ•ด์„œ ์ฒ˜๋ฆฌ๋œ๋‹ค. ์ž๊ธฐ์ž์‹ ์„ ํฌํ•จํ•œ ์ „์ฒด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ์ฆ‰, target์ด ์œ„์น˜ํ•˜๋Š” ulํƒœ๊ทธ๋ถ€ํ„ฐ ์ญ‰ ์ถœ๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

์œ„์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ innerHTMLํƒœ๊ทธ๋Š” ์ž์‹ ์„ ์ œ์™ธํ•œ ํ•˜์œ„ ์—˜๋ฆฌ๋จผํŠธ๋งŒ ๋ณด์—ฌ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ ์—์„œ ๋‘๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

<ul id="target">
    <li>HTML</li>
    <li>CSS</li>
</ul>
<input type="button" onclick="get();" value="get" />
<input type="button" onclick="set();" value="set" />
<script>
    function get(){
        var target = document.getElementById('target');
        alert(target.outerHTML);
    }
    function set(){
        var target = document.getElementById('target');
        target.outerHTML = "<ol><li>JavaScript Core</li><li>BOM</li><li>DOM</li></ol>";
    }
</script>

 

 

innerText, outerText

innerHtml, outerHTML๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ด API๋“ค์€ ๊ฐ’์„ ์ฝ์„ ๋•Œ๋Š” HTML ์ฝ”๋“œ๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๊ณ , ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” HTML์˜ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

์ฆ‰, ์‰ฝ๊ฒŒ ๋งํ•˜์ž๋ฉด ์œ„์˜ ๋ฉ”์†Œ๋“œ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ํƒœ๊ทธ๋ฅผ ์ œ์™ธํ•˜๊ณ  ํ…์ŠคํŠธ๋งŒ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

<ul id="target">
    <li>HTML</li>
    <li>CSS</li>
</ul>
<input type="button" onclick="get();" value="get" />
<input type="button" onclick="set();" value="set" />
<script>
    function get(){
        var target = document.getElementById('target');
        alert(target.innerText);
    }
    function set(){
        var target = document.getElementById('target');
        target.innerText = "<li>JavaScript Core</li><li>BOM</li><li>DOM</li>";
    }
</script>

 

 

insertAdjacentHTML()

์ข€ ๋” ์ •๊ตํ•˜๊ฒŒ ๋ฌธ์ž์—ด์„ ์ด์šฉํ•ด์„œ ๋…ธ๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

<ul id="target">
    <li>CSS</li>
</ul>
<input type="button" onclick="beforebegin();" value="beforebegin" />
<input type="button" onclick="afterbegin();" value="afterbegin" />
<input type="button" onclick="beforeend();" value="beforeend" />
<input type="button" onclick="afterend();" value="afterend" />
<script>
    function beforebegin(){
        var target = document.getElementById('target');
        target.insertAdjacentHTML('beforebegin','<h1>Client Side</h1>');
    }
    function afterbegin(){
        var target = document.getElementById('target');
        target.insertAdjacentHTML('afterbegin','<li>HTML</li>');
    }
    function beforeend(){
        var target = document.getElementById('target');
        target.insertAdjacentHTML('beforeend','<li>JavaScript</li>');
    }
    function afterend(){
        var target = document.getElementById('target');
        target.insertAdjacentHTML('afterend','<h1>Server Side</h1>');
    }
</script>
//beforebegin์ด ์œ„์น˜ํ•  ์ž๋ฆฌ
<ul id="target">
//afterbegin์ด ์œ„์น˜ํ•  ์ž๋ฆฌ    
<li>CSS</li>
//beforeend๊ฐ€ ์œ„์น˜ํ•  ์ž๋ฆฌ
</ul>
//afterend๊ฐ€ ์œ„์น˜ํ•  ์ž๋ฆฌ

 

 

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.