JiYoung Dev πŸ–₯

javascript CallBack ν•¨μˆ˜ & jQuery (2023.05.03) λ³Έλ¬Έ

full stack/JavaScript

javascript CallBack ν•¨μˆ˜ & jQuery (2023.05.03)

Shinjio 2023. 5. 3. 20:37

🎈 CallBackν•¨μˆ˜

μ½”λ“œλ₯Ό 톡해 λͺ…μ‹œμ μœΌλ‘œ ν˜ΈμΆœν•˜λŠ” ν•¨μˆ˜κ°€ μ•„λ‹ˆλΌ μ–΄λ–€ μ΄λ²€νŠΈκ°€ λ°œμƒν•˜κ±°λ‚˜ νŠΉμ • μ‹œμ μ— λ„λ‹¬ν–ˆμ„ λ•Œ μ‹œμŠ€ν…œμ—μ„œ ν˜ΈμΆœν•˜λŠ” ν•¨μˆ˜ (λͺ…μ‹œμ  호좜 X)

κ΅¬μ‘°μƒμœΌλ‘œλŠ” ν•¨μˆ˜ μ•ˆμ— νŒŒλΌλ―Έν„°λ‘œ ν•¨μˆ˜κ°€ λ“€μ–΄κ°€λŠ” ꡬ쑰

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>callback</title>
</head>
<body>
    <button>Click!</button>

    <script>
        // callback ν•¨μˆ˜
        //  : νŠΉμ • μ΄λ²€νŠΈκ°€ λ°œμƒν–ˆμ„ λ•Œ ν˜Ήμ€ νŠΉμ • μ‹œμ μ— λ„λ‹¬ν–ˆμ„ λ•Œ 
        //    μ‹œμŠ€ν…œμ—μ„œ ν˜ΈμΆœν•˜λŠ” ν•¨μˆ˜ (λͺ…μ‹œμ  호좜 X)
        //  : ν•¨μˆ˜ μ•ˆμ— νŒŒλΌλ―Έν„°λ‘œ ν•¨μˆ˜κ°€ λ“€μ–΄κ°€λŠ” ꡬ쑰

        // 1. 이벀트 ν•Έλ“€λŸ¬ : addEventListner(μ΄λ²€νŠΈνƒ€μž…, μ΄λ²€νŠΈν•Έλ“€λŸ¬)
        // λ²„νŠΌμ„ ν΄λ¦­ν–ˆμ„ λ•Œ λ©”μ‹œμ§€λ₯Ό 좜λ ₯
        document.querySelector('button').addEventListener('click', ()=>{
            console.log('🐱');
        })

        // 2. νƒ€μž„ ν•Έλ“€λŸ¬ : setTimeout(νƒ€μž„ν•Έλ“€λŸ¬, μ‹œκ°„μ •λ³΄(ms))
        // νŠΉμ • μ‹œκ°„μ΄ μ§€λ‚œ 이후에 λ©”μ‹œμ§€λ₯Ό 좜λ ₯
        console.log('🐬'); //1
        console.log('🐱‍πŸš€'); //2
        setTimeout(()=>{
            console.log('πŸ•'); //4    
        }, 2500)
        console.log('🍟'); //3
    </script>
</body>
</html>

 

μ½œλ°±ν•¨μˆ˜ μ‹€μŠ΅ 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>callback practice</title>
</head>
<body>
    <button>Click!</button>
    <br><br>
    <!-- μ‹œκ°„ 정보가 좜λ ₯될 곡간 -->
    <div id="container"></div>

    <script>
        // HTML 화면에 1μ΄ˆλΆ€ν„° 10μ΄ˆκΉŒμ§€ μ‹œκ°„ 정보가 좜λ ₯λ˜λ„λ‘!
        let divVal = document.getElementById('container');
        for(let i = 1 ; i <= 10 ; i++){
            setTimeout(()=>{
                divVal.innerHTML += `${i}초⏰<br>`;
            }, i*1000)
        }

        // λ²„νŠΌ ν΄λ¦­μ‹œ 'click' λ©”μ‹œμ§€ 좜λ ₯
        document.querySelector('button').addEventListener('click', ()=>{
            divVal.innerHTML += 'clickπŸ–±<br>'
        })
    </script>
</body>
</html>

 

🎈 JQuery

 πŸ“– 라이브러리 vs ν”„λ ˆμž„μ›Œν¬

곡톡점 : ν•¨μˆ˜(μ½”λ“œ)λ₯Ό ꡉμž₯히 많이 λͺ¨μ•„놓은 것 

차이점 : λˆ„κ°€ μ œμ–΄λ₯Ό ν•˜λŠ”κ°€? 

- λΌμ΄λΈŒλŸ¬λ¦¬λŠ” λ‚΄(개발자)κ°€ 주체가 λ˜μ–΄ ν•„μš”ν•œ μ†ŒμŠ€λ₯Ό κ°€μ Έλ‹€ μ‚¬μš©ν•˜λŠ” 것

  → λ‚΄κ°€ μ›ν•˜λŠ” 도ꡬλ₯Ό μ„ νƒν•΄μ„œ μ‚¬μš©ν•˜λŠ” 것 (예 : 곡ꡬ함)

- ν”„λ ˆμž„μ›Œν¬λŠ” 주어진 ν™˜κ²½ μ•ˆμ—μ„œ κ·œμΉ™μ„ λ§Œμ‘±μ‹œν‚€λ©΄μ„œ μ‚¬μš© (예 : 섀계도)

 

βš™οΈ 라이브러리

μ†Œν”„νŠΈμ›¨μ–΄λ₯Ό κ°œλ°œν•  λ•Œ 컴퓨터 ν”„λ‘œκ·Έλž¨μ΄ μ‚¬μš©ν•˜λŠ” λΉ„νœ˜λ°œμ„± μžμ›μ˜ λͺ¨μž„. 즉, νŠΉμ • κΈ°λŠ₯을 λͺ¨μ•„λ‘” μ½”λ“œ, ν•¨μˆ˜λ“€μ˜ 집합이며 μ½”λ“œ μž‘μ„±μ‹œ ν™œμš© κ°€λŠ₯ν•œ 도ꡬ듀을 μ˜λ―Έν•¨ (jQuery, React.js λ“±) 

 

βš™οΈ ν”„λ ˆμž„μ›Œν¬

ν”„λ ˆμž„μ›Œν¬λŠ” μ›ν•˜λŠ” κΈ°λŠ₯ κ΅¬ν˜„μ— μ§‘μ€‘ν•˜μ—¬ κ°œλ°œν•  수 μžˆλ„λ‘ μΌμ •ν•œ ν˜•νƒœμ™€ ν•„μš”ν•œ κΈ°λŠ₯을 κ°–μΆ”κ³  μžˆλŠ” λΌˆλŒ€, 골격을 의미. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œμ‹œ ν•„μˆ˜μ μΈ μ½”λ“œ, μ•Œκ³ λ¦¬μ¦˜, DB연동과 같은 κΈ°λŠ₯듀을 μœ„ν•΄ μ–΄λŠ 정도 λΌˆλŒ€(ꡬ쑰)λ₯Ό μ œκ³΅ν•˜λ©° μ΄λŸ¬ν•œ λΌˆλŒ€ μœ„μ—μ„œ μ‚¬μš©μžλŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ—¬ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ κ°œλ°œν•¨. μ•±/μ„œλ²„ λ“±μ˜ ꡬ동, λ©”λͺ¨λ¦¬ 관리 λ“±μ˜ κ³΅ν†΅λœ 뢀뢄은 ν”„λ ˆμž„μ›Œν¬κ°€ κ΄€λ¦¬ν•˜λ©° μ‚¬μš©μžλŠ” ν”„λ ˆμž„μ›Œν¬κ°€ μ •ν•΄μ€€ λ°©μ‹λŒ€λ‘œ 클리슀, λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„

(μžλ°” - spring, 파이썬 - Django, Flask, μ•ˆλ“œλ‘œμ΄λ“œ - android λ“±)

 

πŸ“– jQuery

λͺ¨λ“  λΈŒλΌμš°μ €μ—μ„œ λ™μž‘ν•˜λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό κ°„νŽΈν•˜κ²Œ μ‚¬μš©ν•  수 μžˆλ„λ‘ λ‹¨μˆœν™”μ‹œν‚¨ μ˜€ν”ˆμ†ŒμŠ€ 기반 라이브러리

 

크둜슀 λΈŒλΌμš°μ§• : κ°™μ€ λ™μž‘μ΄μ§€λ§Œ λΈŒλΌμš°μ €μ— λ”°λΌμ„œ μ—¬λŸ¬κ°€μ§€ ν˜•νƒœλ‘œ μž‘μ„± 

→ 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ λ“±μž₯ν•œ 것이 JS 라이브러리인 jQuery

 

Javascript와 jQuery μ½”λ“œ 차이

βš™οΈ jQuery 라이브러리 연동

 

ν˜‘μ—…ν•˜λŠ” μƒν™©μ—μ„œλŠ” λ„€νŠΈμ›Œν¬ 방식을 ν™œμš© - λ‹€μš΄λ‘œλ“œ λ°©μ‹μ—μ„œλŠ” 제이쿼리 μ €μž₯ κ²½λ‘œκ°€ λ‹€ λ‹€λ₯Ό 수 있기 λ•Œλ¬Έμ—

 

jQuery λ‹€μš΄λ‘œλ“œμ‹œ 버전별 차이

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery start</title>

    <!-- 1. λ‹€μš΄λ‘œλ“œ 방식 -->
     <!-- HTML λ¬Έμ„œμ— jQuery 라이브러리 μ—°κ²°  -->
    <!-- <script src="./js/jquery-3.6.4.min.js"></script> -->

    <!-- 2. λ„€νŠΈμ›Œν¬ 방식 -->
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

</head>
<body>
    <p>jQuery start</p>
    <button id="textBtn">TEXT</button>
    <button id="htmlBtn">HTML</button>

    <script>
        // jQuery/$ 
        // → wrapper(래퍼) : 인자둜 μ „λ‹¬λœ μš”μ†Œλ“€μ— jQuery의 κΈ°λŠ₯성을 λΆ€κ°€ν•΄μ„œ λ°˜ν™˜ 
                        //   jQuery 객체 λ°˜ν™˜
        // $(μ„ νƒμž).λ©”μ„œλ“œ()

        // 1. pμš”μ†Œ κ°€μ Έμ˜€κΈ°
        // (1) μžλ°”μŠ€ν¬λ¦½νŠΈ 문법
        let valJS = document.querySelector('p');
        console.log(valJS);
        // (2) 제이쿼리 문법
        // let valJQ = jQuery('p');
        let valJQ = $('p')
        // jQeury λŒ€μ‹  $ 기호 μ‚¬μš© κ°€λŠ₯
        console.log(valJQ);

        // 2. TEXTλ²„νŠΌ ν΄λ¦­μ‹œ, λ‚΄μš©λ§Œ 
        // JS → innerText μ‚¬μš©
        // JQ → text() μ‚¬μš©
        $('#textBtn').click(()=>{
            // valJS.innerText = 'λ‚΄μš© λ³€κ²½!';
            $('p').text('λ‚΄μš© λ³€κ²½!'); 
        })

        // 3. HTMLλ²„νŠΌ ν΄λ¦­μ‹œ, νƒœκ·ΈκΉŒμ§€
        // JS → innerHTML
        // JQ →
        $('#htmlBtn').click(()=>{
            // valJS.innerHTML = '<a href="#">aνƒœκ·Έλ‘œ λ³€κ²½</a>';
            $('p').html('<a href="#">aνƒœκ·Έλ‘œ λ³€κ²½!</a>');
        })
    </script>
</body>
</html>

 

βš™οΈ jQuery μš”μ†Œ κ°€μ Έμ˜€κΈ° 및 λ™μž‘ μ„€μ •ν•˜κΈ°

 

βš™οΈ jQuery μŠ€νƒ€μΌ λ³€κ²½ν•˜κΈ°

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery style</title>
    <script src="./js/jquery-3.6.4.min.js"></script>
</head>
<body>
    <p>jQuery style</p>
    <button>change</button>

    <script>
        // λ²„νŠΌμ„ ν΄λ¦­ν–ˆμ„ λ•Œ, p μš”μ†Œμ˜ μŠ€νƒ€μΌ μ œμ–΄!
        $('button').click(()=>{
            // μŠ€νƒ€μΌ μ œμ–΄
            // JS → .style
            // jQ → css()Aimage.png

            // 1. ν•œμ€„μ”© μž…λ ₯
            // $('p').css('color','lightgreen');
            // $('p').css('background-color','black');
            // $('p').css('font-style','italic');

            // 2. chain 방식 μž…λ ₯
            // jQuery λ©”μ†Œλ“œλŠ” λ©”μ†Œλ“œκ°€ μ œμ–΄ν•˜λŠ” λŒ€μƒμ„ 리턴 
            // → λ©”μ†Œλ“œλ₯Ό κ³„μ†ν•΄μ„œ λΆ™μ—¬λ„£λŠ” chain 방식 μ‚¬μš© κ°€λŠ₯
            // $('p').css('color','lightgreen')
            //       .css('background-color','black')
            //       .css('font-style','italic');

            // 3. 객체 ν˜•νƒœλ‘œ μž…λ ₯ 
            $('p').css({
                'color' : 'lightgreen',
                'background-color' : 'black',
                'font-style' : 'italic'
            })
        })
    </script>
</body>
</html>

 

μŠ€νƒ€μΌ λ³€κ²½ μž…λ ₯방식 3가지

가지고 올 μš”μ†Œμ™€ λ©”μ†Œλ“œκ°€ λ‹€ λ‹€λ₯΄λ©΄ → ν•œμ€„ 방식

가지고 올 μš”μ†Œλ§Œ κ°™κ³  λ©”μ†Œλ“œκ°€ λ‹€λ₯΄λ©΄ → chain 방식

κ°€μ§€κ³ μ˜¬ μš”μ†Œμ™€ λ©”μ†Œλ“œκ°€ κ°™μœΌλ©΄ → 객체 ν˜•νƒœ

 

βš™οΈ jQuery μ½”λ“œ μˆœμ„œ κ΄€λ ¨

 

βš™οΈ jQuery 이벀트

μ›Ή νŽ˜μ΄μ§€λ₯Ό λ°©λ¬Έν•œ μ‚¬μš©μžκ°€ μˆ˜ν–‰ν•˜λŠ” λ™μž‘ μ‹€ν–‰ μ‹œμ μ— μ œμ–΄

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>mouse event</title>
    <style>
        .parentDiv{
            width: 200px;
            height: 200px;
            background-color: wheat;
            padding: 50px;
            margin: 10px;
        }
        .childDiv{
            width: 100%;
            height: 100%;
            background-color: olive;
        }
    </style>
    <script src="./js/jquery-3.6.4.min.js"></script>

</head>
<body>
    <div class="parentDiv">
        <div class="childDiv"></div>
    </div>

    <script>
        // 마우슀 이벀트
        // mouseover → 마우슀 μ˜¬λ Έμ„ λ•Œ, μžμ‹ μš”μ†Œ 포함(↔mouseout)
        // mouseenter → 마우슀 μ˜¬λ Έμ„ λ•Œ, μžμ‹ μš”μ†Œ μ œμ™Έ(↔mouseleave)
        $('.parentDiv')
        .mouseover(()=>{console.log('mouseover🍳');})
        .mouseenter(()=>{console.log('mouseenterπŸ₯©');})
        
    </script>

</body>
</html>

 

βš™οΈ on λ©”μ†Œλ“œ

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/jquery-3.6.4.min.js"></script>
</head>
<body>
    <h1>πŸ™ Event 지정</h1>
    <h1>πŸ™ Event 지정</h1>
    <h1>πŸ™ Event 지정</h1>
    <button>Change!</button>

    <script>
        // $(μ„ νƒμž).on(μ΄λ²€νŠΈνƒ€μž…, μ΄λ²€νŠΈν•Έλ“€λŸ¬)

        // 1. λ²„νŠΌ ν΄λ¦­μ‹œ, h1νƒœκ·Έ 글씨색 νŒŒλž€μƒ‰ >> click
        $('button').on('click', ()=>{
            $('h1').css('color','blue');
        })

        // 2. h1νƒœκ·Έ 마우슀 μ˜¬λ Έμ„ λ•Œ, 글씨색 빨간색 >> mouseover
        $('h1').on('mouseover', function(){
            $(this).css('color', 'red');
            // this : 호좜된 HTML 객체λ₯Ό μ €μž₯
            //  - ν™”μ‚΄ν‘œ ν•¨μˆ˜μ—μ„œλŠ” μž‘λ™ν•˜μ§€ μ•ŠμŒ
        })

        // 3. h1νƒœκ·Έ 마우슀 내렸을 λ•Œ, 글씨색 검은색 >> mouseout
        $('h1').on('mouseout', (e)=>{
            console.log(e.target);
            // ν™”μ‚΄ν‘œ ν•¨μˆ˜μ—μ„œ this λŒ€μ‹  e.target μ‚¬μš©
            $(e.target).css('color', 'black');
        })

    </script>
</body>
</html>