์ƒˆ์†Œ์‹

Languages/Java Script

[Js] Text๊ฐ์ฒด | ๊ฐ’ API | ์กฐ์ž‘ API

  • -
๋ฐ˜์‘ํ˜•

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

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "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


์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ๊ฐ•์˜์˜ "Text๊ฐ์ฒด" ์˜ ๋ชจ๋“  ๊ฐ•์˜๋‚ด์šฉ์„ ํฌํ•จํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 


Text ๊ฐ์ฒด

์†Œ๊ฐœ

ํ…์ŠคํŠธ ๊ฐ์ฒด๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ์— ๋Œ€ํ•œ DOM ๊ฐ์ฒด๋กœ CharcterData๋ฅผ ์ƒ์† ๋ฐ›๋Š”๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด <p>haeun</p>๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ํƒœ๊ทธ๋ฅผ ์ œ์™ธํ•œ 'haeun'์ด ๋ฐ”๋กœ ํ…์ŠคํŠธ ์ด๋‹ค.

 

์•„๋ž˜๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ์˜ˆ์ œ๋‹ค. ์ฃผ๋ชฉํ•  ๊ฒƒ์€ DOM์—์„œ๋Š” ๊ณต๋ฐฑ์ด๋‚˜ ์ค„๋ฐ”๊ฟˆ๋„ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ผ๋Š” ์ ์ด๋‹ค.

<p id="target1"><span>Hello world</span></p> //๊ณต๋ฐฑ ์—”ํ„ฐ ๋“ฑ ์—†์ด ๋‹ค ๋ถ™์–ด ์žˆ์Œ.
<p id="target2">//๊ณต๋ฐฑ๊ณผ ์—”ํ„ฐ ์กด์žฌ.
    <span>Hello world</span>
</p>
<script>
var t1 = document.getElementById('target1').firstChild;
var t2 = document.getElementById('target2').firstChild;
 
console.log(t1.firstChild.nodeValue);
try{
    console.log(t2.firstChild.nodeValue);   
} catch(e){
    console.log(e);
}
console.log(t2.nextSibling.firstChild.nodeValue);
 
</script>

 

์‹คํ–‰๊ฒฐ๊ณผ 

Hello world
TypeError {stack: (...), message: "Cannot read property 'nodeValue' of null"}
Hello world

 

 

fisrstChild๋กœ ์ฒ˜์Œ ์ถœ๋ ฅํ•˜๋ฉด ๊ฐ€๋ฅดํ‚ค๋Š” ๊ฐ์ฒด ์ž์ฒด๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. 

ex) <span>Hello world</span>

์—ฌ๊ธฐ์„œ ํ•œ๋ฒˆ ๋” firstChild๋ฅผ ํ•˜๋ฉด ๊ทธ๋•Œ text๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค -> Hello world
๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๊ณต๋ฐฑ์ด ์žˆ๋‹ค๋ฉด, ๊ณต๋ฐฑ์„ ๋ฆฌํ„ดํ•œ๋‹ค.

์ฆ‰, Dom์—์„œ๋Š” ๊ณต๋ฐฑ ์กฐ์ฐจ๋„ ๊ฐ์ฒด์ด๋‹ค ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

์ฃผ์š”๊ธฐ๋Šฅ

๊ฐ’

ํ…์ŠคํŠธ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” API

  • data
  • nodeValue

์กฐ์ž‘

  • appendData()
  • deleteData()
  • insertData()
  • replaceData()
  • subStringData()

์ƒ์„ฑ

 

๋…ธ๋“œ ๋ณ€๊ฒฝ API - ์›น๋ธŒ๋ผ์šฐ์ € JavaScript

๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ๋…ธ๋“œ ์ถ”๊ฐ€ ๋…ธ๋“œ์˜ ์ถ”๊ฐ€์™€ ๊ด€๋ จ๋œ API๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”๊ฐ€ํ•  ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๊ฒƒ์€ document ๊ฐ์ฒด์˜ ๊ธฐ๋Šฅ

opentutorials.org

 


๊ฐ’ API

ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” DOM์—์„œ ์‹ค์งˆ์ ์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ฐ์ฒด์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ…์ŠคํŠธ ๋…ธ๋“œ์—๋Š” ๊ฐ’๊ณผ ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋Š”๋ฐ ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋‘๊ฐœ์˜ API๋ฅผ ์•Œ์•„๋ณธ๋‹ค.

  • nodeValue
  • data
<ul>
    <li id="target">html</li> 
    <li>css</li>
    <li>JavaScript</li>
</ul>
<script>
    var t = document.getElementById('target').firstChild;
    console.log(t.nodeValue);
    console.log(t.data);
</script>

 


์กฐ์ž‘ API

ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ์ƒ์† ๋ฐ›์€ CharacterData ๊ฐ์ฒด๋Š” ๋ฌธ์ž๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ API๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์•„๋ž˜๋Š” ์กฐ์ž‘๊ณผ ๊ด€๋ จ๋œ API๋“ค์˜ ๋ชฉ๋ก์ด๋‹ค.

 

  • appendData()
  • deleteData()
  • insertData()
  • replaceData()
  • substringData() // ์ •๋ณด ์ค‘์— ์ผ๋ถ€์˜ ์ •๋ณด๋ฅผ ๊ฐ€๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” API
<!DOCTYPE html>
<html>
<head>
    <style>
    #target{
        font-size:77px;
        font-family: georgia;
        border-bottom:1px solid black;
        padding-bottom:10px;
    }
    p{
        margin:5px;
    }
    </style>
</head>
<body>
<p id="target">Cording everybody!</p>
<p> data : <input type="text" id="datasource" value="JavaScript" /></p>
<p>   start :<input type="text" id="start" value="5" /></p>
<p> end : <input type="text" id="end" value="5" /></p>
<p><input type="button" value="appendData(data)" onclick="callAppendData()" />
<input type="button" value="deleteData(start,end)" onclick="callDeleteData()" />
<input type="button" value="insertData(start,data)" onclick="callInsertData()" />
<input type="button" value="replaceData(start,end,data)" onclick="callReplaceData()" />
<input type="button" value="substringData(start,end)" onclick="callSubstringData()" /></p>
<script>
    var target = document.getElementById('target').firstChild;
    var data = document.getElementById('datasource');
    var start = document.getElementById('start');
    var end = document.getElementById('end');
    function callAppendData(){
        target.appendData(data.value);
    }
    function callDeleteData(){
        target.deleteData(start.value, end.value);
    }
    function callInsertData(){
        target.insertData(start.value, data.value); 
    }
    function callReplaceData(){
        target.replaceData(start.value, end.value, data.value);
    }
    function callSubstringData(){
        alert(target.substringData(start.value, end.value));
    }
</script>
</body>
</html>

์œ„ ์˜ˆ์ œ๋Š” ๊ผญ ์‹ค์Šตํ•ด๋ณด๊ธฐ! (Text๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œ ์ˆ˜์ • ์ถ”๊ฐ€ ๋ชจ๋‘ ๊ฐ€๋Šฅ)

๋งˆ์ง€๋ง‰ substringData๋Š” ์ถ”์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰ ์„ ํƒํ•˜๋Š” ๊ฐ’๋งŒ ๋ฆฌํ„ดํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ์ถ”์ถœ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•
Contents

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

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