์ƒˆ์†Œ์‹

Languages/Java Script

[Js] ๋…ธ๋“œ ์ข…๋ฅ˜ API | ๋…ธ๋“œ ๋ณ€๊ฒฝ API | jQuery ๋…ธ๋“œ ๋ณ€๊ฒฝ 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


 

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ๊ฐ•์˜์˜ "Node๊ฐ์ฒด" ์ค‘ ๋…ธ๋“œ ์ข…๋ฅ˜ API ๊ฐ•์˜์™€ ๋…ธ๋“œ ๋ณ€๊ฒฝ API๊ฐ•์˜, ๊ทธ๋ฆฌ๊ณ  jQuery ๋…ธ๋“œ ๋ณ€๊ฒฝ API๊ฐ•์˜๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๋…ธ๋“œ ์ข…๋ฅ˜ API

๋…ธ๋“œ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ ์„ ํƒ๋œ ๋…ธ๋“œ๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ธ์ง€๋ฅผ ํŒ๋‹จํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” API๊ฐ€ nodeType, nodeName์ด๋‹ค.

 

  • Node.nodeType
    node์˜ ํƒ€์ž…์„ ์˜๋ฏธํ•œ๋‹ค. 
  • Node.nodeName
    node์˜ ์ด๋ฆ„ (ํƒœ๊ทธ๋ช…์„ ์˜๋ฏธํ•œ๋‹ค.)

 

Node Type

๋…ธ๋“œ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ์„œ ์ •ํ•ด์ง„ ์ƒ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์•„๋ž˜๋Š” ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ข…๋ฅ˜์™€ ์ข…๋ฅ˜์— ๋”ฐ๋ฅธ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋‹ค.

for(var name in Node){
   console.log(name, Node[name]);
}

 

๊ฒฐ๊ณผ

ELEMENT_NODE 1 
ATTRIBUTE_NODE 2 
TEXT_NODE 3 
CDATA_SECTION_NODE 4 
ENTITY_REFERENCE_NODE 5 
ENTITY_NODE 6 
PROCESSING_INSTRUCTION_NODE 7 
COMMENT_NODE 8 
DOCUMENT_NODE 9 
DOCUMENT_TYPE_NODE 10 
DOCUMENT_FRAGMENT_NODE 11 
NOTATION_NODE 12 
DOCUMENT_POSITION_DISCONNECTED 1 
DOCUMENT_POSITION_PRECEDING 2 
DOCUMENT_POSITION_FOLLOWING 4 
DOCUMENT_POSITION_CONTAINS 8 
DOCUMENT_POSITION_CONTAINED_BY 16 
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 32

 

 

๋ช‡๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์‹คํ–‰์‹œ์ผœ ๋ณด๋ฉด ๋…ธ๋“œํƒ€์ž…์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๊ฐ๊ฐ์˜ ๋…ธ๋“œ ํƒ€์ž…์€ ์ˆซ์ž๋ฅผ ๋‹ค ์™ธ์šฐ๊ธฐ๊ฐ€ ํž˜๋“ค ๊ฒƒ์„ ๊ณ ๋ คํ•˜์—ฌ ๋ฌธ์ž(๋ณ€์ˆ˜)๋กœ ์ „๋ถ€ ์ œ๊ณต๋œ๋‹ค.

 

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

๋…ธ๋“œ ๋„ค์ž„์€ ์กฐ๊ธˆ๋” ๋””ํ…Œ์ผ ํ•œ ์ •๋ณด ex: ํƒœ๊ทธ์˜ ์ด๋ฆ„ ๋“ฑ์„ ์•Œ๋ ค์ฃผ๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

 

 

์•„๋ž˜ ์˜ˆ์ œ๋Š” ๋…ธ๋“œ ์ข…๋ฅ˜ API๋ฅผ ์ด์šฉํ•ด์„œ ๋…ธ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜ˆ์ œ๋‹ค.

ํ•จ์ˆ˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ์žฌ๊ท€ํ•จ์ˆ˜๋ผ๊ณ  ํ•˜๋Š”๋ฐ ๋ณธ ์˜ˆ์ œ๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

<!DOCTYPE html>
<html>
<body id="start">
<ul>
    <li><a href="./532">html</a></li> 
    <li><a href="./533">css</a></li>
    <li><a href="./534">JavaScript</a>
        <ul>
            <li><a href="./535">JavaScript Core</a></li>
            <li><a href="./536">DOM</a></li>
            <li><a href="./537">BOM</a></li>
        </ul>
    </li>
</ul>
<script>
function traverse(target, callback){
    if(target.nodeType === 1){
        //if(target.nodeName === 'A')
        callback(target);
        var c = target.childNodes;
        for(var i=0; i<c.length; i++){
            traverse(c[i], callback);       
        }   
    }
}
traverse(document.getElementById('start'), function(elem){//๋‘๋ฒˆ์งธ๋กœ ํ˜ธ์ถœ ํ•˜๋Š” ํ•จ์ˆ˜ = "์ต๋ช… ํ•จ์ˆ˜"
    console.log(elem);
});
</script>
</body>
</html>

 

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์งœ๋ ค๋ฉด ํ•จ์ˆ˜ ๊ตฌ์„ฑ์„ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. traverseํ•จ์ˆ˜๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜์˜€๋Š”์ง€๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. ์•„๋ž˜ ์˜์ƒ์„ ์ฐธ๊ณ ํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค.

www.youtube.com/watch?v=RcDsVD_O8dQ&feature=youtu.be

 


๋…ธ๋“œ ๋ณ€๊ฒฝ API

๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

 

๋…ธ๋“œ ์ถ”๊ฐ€

๋…ธ๋“œ์˜ ์ถ”๊ฐ€์™€ ๊ด€๋ จ๋œ API๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • appendChild(child)
    ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹์œผ๋กœ ์ฃผ์–ด์ง„ ์—˜๋ฆฌ๋จผํŠธ ์ถ”๊ฐ€
  • insertBefore(newElement, referenceElement)
    appendChild์™€ ๋™์ž‘๋ฐฉ๋ฒ•์€ ๊ฐ™์œผ๋‚˜ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ „๋‹ฌ ํ–ˆ์„ ๋•Œ ์ด๊ฒƒ ์•ž์— ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.

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

 

<ul id="target">
    <li>HTML</li>
    <li>CSS</li>
</ul>
<input type="button" onclick="callAppendChild();" value="appendChild()" />
<input type="button" onclick="callInsertBefore();" value="insertBefore()" />
<script>
    function callAppendChild(){//์ œ์ผ ๋์— ๋…ธ๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ
        var target = document.getElementById('target');
        var li = document.createElement('li');
        var text = document.createTextNode('JavaScript');
        li.appendChild(text);//ํ•ฉ์ณ์ค˜์•ผํ•จ 
        target.appendChild(li);
    }
 
    function callInsertBefore(){//๊ฐ€์žฅ ์•ž์— ๋…ธ๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ
        var target = document.getElementById('target');
        var li = document.createElement('li');
        var text = document.createTextNode('jQuery');
        li.appendChild(text);
        target.insertBefore(li, target.firstChild);//insertBefore : ์•ž์— ์ถ”๊ฐ€
    }
</script>

 

๋…ธ๋“œ ์ œ๊ฑฐ

๋…ธ๋“œ ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ API๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ๋•Œ ๋ฉ”์†Œ๋“œ๋Š” ์‚ญ์ œ ๋Œ€์ƒ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ๊ฒƒ์„ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์ž.

<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li id="target">JavaScript</li>
</ul>
<input type="button" onclick="callRemoveChild();" value="removeChild()" />
<script>
    function callRemoveChild(){
        var target = document.getElementById('target');
        target.parentNode.removeChild(target);
    }
</script>

 

๋…ธ๋“œ ๋ฐ”๊พธ๊ธฐ

๋…ธ๋“œ ๋ฐ”๊พธ๊ธฐ์—๋Š” ์•„๋ž˜ API๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li id="target">JavaScript</li>
</ul>
<input type="button" onclick="callReplaceChild();" value="replaceChild()" />
<script>
    function callReplaceChild(){
        var a = document.createElement('a');
        a.setAttribute('href', 'http://opentutorials.org/module/904/6701');
        a.appendChild(document.createTextNode('Web browser JavaScript'));
 
        var target = document.getElementById('target');
        target.replaceChild(a,target.firstChild);
    }
</script>

 

๋ชจ๋‘  ํ•ฉ์ณ๋ณธ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<ul id="target">
    <li>HTML</li>
    <li>CSS</li>
    <li id = "tata">JavaScript</li>
</ul>
<input type="button" onclick="callAppendChild();" value="appendChild" />
<input type="button" onclick="callInsertBefore();" value="insertBefore" />
<input type="button" onclick="callRemoveChild();" value="remove" />

<input type = "button" onclick ="callReplaceChild();" value="change"/>

<script>
    function callAppendChild(){
        var target = document.getElementById('target');
        var li = document.createElement('li');
        var text = document.createTextNode('JavaScript');
        li.appendChild(text);
        target.appendChild(li);
    }
 
    function callInsertBefore(){
        var target = document.getElementById('target');
        var li = document.createElement('li');
        var text = document.createTextNode('jQuery');
        li.appendChild(text);
        target.insertBefore(li, target.firstChild);
    }
    function callRemoveChild(){
        var tata = document.getElementById('tata');
        tata.parentNode.removeChild(tata);
    }

    function callReplaceChild(){
        var a = document.createElement('a');
        a.setAttribute('href','https://lucete1230-cyberpolice.tistory.com/manage/posts/');
        a.appendChild(document.createTextNode('my blog~~'));

        var tata = document.getElementById('tata');
        tata.replaceChild(a,tata.firstChild);


    }
</script>

 

๊ฒฐ๊ณผ 

 


jQuery ๋…ธ๋“œ ๋ณ€๊ฒฝ API

jQuery๋ฅผ ์ด์šฉํ•ด์„œ ๋…ธ๋“œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. jQuery์—์„œ ๋…ธ๋“œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์ฃผ๋กœ Manipulation ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•ด ์žˆ๋‹ค.

http://api.jquery.com/category/manipulation/

 

Manipulation | jQuery API Documentation

Adds the specified class(es) to each element in the set of matched elements. Insert content, specified by the parameter, after each element in the set of matched elements. Insert content, specified by the parameter, to the end of each element in the set of

api.jquery.com

 

์ถ”๊ฐ€

์ถ”๊ฐ€์™€ ๊ด€๋ จ๋œ ์ฃผ์š”ํ•œ ๋ฉ”์†Œ๋“œ๋Š” 4๊ฐ€์ง€๋‹ค. ๊ฐ๊ฐ์˜ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์œ„์˜ API๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์„œ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•œ ์˜ˆ์ด๋‹ค. (์‹คํ–‰)

<div class="target">
    content1
</div>
 
<div class="target">
    content2
</div>
 
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script>
    $('.target').before('<div>before</div>');
    $('.target').after('<div>after</div>');
    $('.target').prepend('<div>prepend</div>');
    $('.target').append('<div>append</div>');
</script>

 

๊ฐ ๋ฉ”์†Œ๋“œ๊ฐ€ ์–ด๋””์— ์œ„์น˜๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

www.youtube.com/watch?v=sONnwMyHNQQ&feature=youtu.be

 

 

์ œ๊ฑฐ

์ œ๊ฑฐ์™€ ๊ด€๋ จ๋œ API๋Š” remove์™€ empty๊ฐ€ ์žˆ๋‹ค. remove๋Š” ์„ ํƒ๋œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด๊ณ  empty๋Š” ์„ ํƒ๋œ ์—˜๋ฆฌ๋จผํŠธ์˜ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์‹คํ–‰)

<div class="target" id="target1">
    target 1
</div>
 
<div class="target" id="target2">
    target 2
</div>
 
<input type="button" value="remove target 1" id="btn1" />
<input type="button" value="empty target 2" id="btn2" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
    $('#btn1').click(function(){
        $('#target1').remove();
    })
    $('#btn2').click(function(){
        $('#target2').empty();
    })
</script>

 

๋ฐ”๊พธ๊ธฐ

replaceAll๊ณผ replaceWith๋Š” ๋ชจ๋‘ ๋…ธ๋“œ์˜ ๋‚ด์šฉ์„ ๊ต์ฒดํ•˜๋Š” API์ด๋‹ค. replaceWith๊ฐ€ ์ œ์–ด ๋Œ€์ƒ์„ ๋จผ์ € ์ง€์ •ํ•˜๋Š” ๊ฒƒ์— ๋ฐ˜ํ•ด์„œ replaceAll์€ ์ œ์–ด ๋Œ€์ƒ์„ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค. (์‹คํ–‰)

<div class="target" id="target1">
    target 1
</div>
 
<div class="target" id="target2">
    target 2
</div>
 
<input type="button" value="replaceAll target 1" id="btn1" />
<input type="button" value="replaceWith target 2" id="btn2" />
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
  
<script>
    $('#btn1').click(function(){
        $('<div>replaceAll</div>').replaceAll('#target1');
    })
    $('#btn2').click(function(){
        $('#target2').replaceWith('<div>replaceWith</div>');
    })
</script>

 

๋ณต์‚ฌ

๋…ธ๋“œ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. (์‹คํ–‰)

<div class="target" id="target1">
    target 1
</div>
 
<div class="target" id="target2">
    target 2
</div>
 
<div id="source">source</div>
 
<input type="button" value="clone replaceAll target 1" id="btn1" />
<input type="button" value="clone replaceWith target 2" id="btn2" />
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script>
    $('#btn1').click(function(){
        $('#source').clone().replaceAll('#target1');
    })
    $('#btn2').click(function(){
        $('#target2').replaceWith($('#source').clone());
    })
</script>

 

์ด๋™

dom manipulation API์˜ ์ธ์ž๋กœ ํŠน์ • ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๋ฉด ์ด๋™์˜ ํšจ๊ณผ๊ฐ€ ๋‚œ๋‹ค. (์‹คํ–‰)

<div class="target" id="target1">
    target 1
</div>
 
<div id="source">source</div>
 
<input type="button" value="append source to target 1" id="btn1" />
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script>

<script>
    $('#btn1').click(function(){
        $('#target1').append($('#source'));
    })
</script>
๋ฐ˜์‘ํ˜•
Contents

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

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