JQuery ์ •๋ณตํ•˜๊ธฐ 3 (event, element, form)

    ์ด๋ฒคํŠธ๋ž€?

    • ์‹œ์Šคํ…œ์—์„œ ์ผ์–ด๋‚˜๋Š” ์‚ฌ๊ฑด์„ ์˜๋ฏธ
    • javascript๋‚˜ jQuery์—๊ฒŒ ์ด๋ฒคํŠธ๋ž€ ๋ธŒ๋ผ์šฐ์ ธ์—์„œ ์ผ์–ด๋‚˜๋Š” ์‚ฌ๊ฑด์„ ์˜๋ฏธํ•œ๋‹ค. (ํด๋ฆญ, ๋งˆ์šฐ์Šค ์ด๋™, ํƒ€์ดํ•‘, ํŽ˜์ด์ง€ ๋กœ๋”ฉ๋“ฑ)
    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ž‘๋™ํ•  ๋กœ์ง์„ ์‹œ์Šคํ…œ์—๊ฒŒ ์•Œ๋ ค๋‘๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹œ์Šคํ…œ์ด ๊ทธ ๋กœ์ง์„ ํ˜ธ์ถœํ•œ๋‹ค.

    jQuery์˜ ์ด๋ฒคํŠธ

    • ํฌ๋กœ์Šค๋ธŒ๋ผ์šฐ์ง•์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์คŒ
    • bind๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์„ค์น˜ํ•˜๊ณ , unbind๋กœ ์ œ๊ฑฐ (์˜ˆ์ œ1) (on,off๋กœ ์—…๋ฐ์ดํŠธ ๋จ)
    • trigger๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๊ฐ•์ œ๋กœ ์‹คํ–‰ (์˜ˆ์ œ2)
    • click, ready์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ ํ—ฌํผ(helper)๋ฅผ ์ œ๊ณตํ•จ
    • live๋ฅผ ์ด์šฉํ•˜๋ฉด ํ˜„์žฌ ์กด์žฌ ํ•˜์ง€ ์•Š๋Š” ์—˜๋ฆฌ๋จผํŠธ์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Œ

    ์˜ˆ์ œ1. on, off, trigger๋ฅผ ์ด์šฉํ•œ ์ด๋ฒคํŠธ์˜ ์„ค์น˜, ์ œ๊ฑฐ, ํ˜ธ์ถœ

      
    <html>
        <head>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            <script type="text/javascript">
                function clickHandler(e){
                    alert('thank you');
                }
                //๋ณดํ†ต ์ด๋ฒคํŠธ ํ•จ์ˆ˜์— ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋Šฅ์€ ์žฌ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ต๋ช…ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ
                $(document).on('ready', function(){ 
                     $('#click_me').on('click', clickHandler);
                     $('#remove_event').on('click', function(e){
                         $('#click_me').off('click', clickHandler);
                     });
                     $('#trigger_event').on('click', function(e){
                         $('#click_me').trigger('click');
                     });
                 })
            </script>
        </head>
        <body>
            <input id="click_me" type="button" value="click me" />
            <input id="remove_event" type="button" value="off" />
            <input id="trigger_event" type="button" value="trigger" />
        </body>
    </html>

    ์˜ˆ์ œ2. ์ด๋ฒคํŠธ ํ—ฌํผ

      
    <html>
        <head>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            <script type="text/javascript">
                function clickHandler(e){
                    alert('thank you');
                }
                $(document).ready(function(){
                     $('#click_me').click(clickHandler);
                     $('#remove_event').click(function(e){
                         $('#click_me').off('click', clickHandler);
                     });
                     $('#trigger_event').click(function(e){
                         $('#click_me').trigger('click');
                     });
                 })
            </script>
        </head>
        <body>
            <input id="click_me" type="button" value="click me" />
            <input id="remove_event" type="button" value="off" />
            <input id="trigger_event" type="button" value="trigger" />
        </body>
    </html>

    ์˜ˆ์ œ3. on๋ฅผ ์ด์šฉํ•˜๋ฉด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์—˜๋ฆฌ๋จผํŠธ์— ๋Œ€ํ•ด์„œ ์ด๋ฒคํŠธ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

      
    <html>
        <head>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            <script type="text/javascript">
                function clickHandler(e) {
                    alert('thank you');
                }
                $('#click_me').live('click', clickHandler);
                $('#remove_event').live('click', function(e) {
                    $('#click_me').die('click', clickHandler);
                });
                $('#trigger_event').live('click', function(e) {
                    $('#click_me').trigger('click');
                });
            </script>
        </head>
        <body>
            <input id="click_me" type="button" value="click me" />
        <input id="remove_event" type="button" value="off" />
        <input id="trigger_event" type="button" value="trigger" />
        </body>
    </html>


    ์—˜๋ฆฌ๋จผํŠธ ์ œ์–ด

    • jQuery๋Š” ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ œ์–ดํ•˜๋Š” ์ผ๊ด€๋˜๊ณ  ํ’๋ถ€ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.
    • http://api.jquery.com/category/manipulation/

    ๐Ÿ“ ์ž์‹์œผ๋กœ ์‚ฝ์ž… (.append(), .appendTo(), .html(), .prepend(), .prependTo(), .text())

    
    <!DOCTYPE html>
    <html>
        <head>
            <style>
                p {
                    background:yellow;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p>
                I would like to say:
            </p>
            <script>$("p").append("<strong>Hello</strong>");</script>
        </body>
    </html>

    ๐Ÿ“ ํ˜•์ œ๋กœ ์‚ฝ์ž… (.after(), .before(), .insertAfter(), .insertBefore())

    
    <!DOCTYPE html>
    <html>
        <head>
            <style>
                p {
                    background:yellow;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p>
                I would like to say:
            </p>
            <script>$("p").after("<b>Hello</b>");</script>
        </body>
    </html>

    ๐Ÿ“๋ถ€๋ชจ๋กœ ๊ฐ์‹ธ๊ธฐ (.unwrap(), .wrap(), .wrapAll(), .wrapInner())

    
    <!DOCTYPE html>
    <html>
        <head>
            <style>
                div {
                    border:2px blue solid;
                    margin:2px;
                    padding:2px;
                }
                p {
                    background:yellow;
                    margin:2px;
                    padding:2px;
                }
                strong {
                    color:red;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <span>Span Text</span>
            <strong>What about me?</strong>
            <span>Another One</span>
            <script>$("span").wrap("<div><div><p><em><b></b></em></p></div></div>");</script>
        </body>
    </html>

    ๐Ÿ“์‚ญ์ œ (.detach(), .empty(), .remove(), .unwrap())

    
    <!DOCTYPE html>
    <html>
        <head>
            <style>
                p {
                    background:yellow;
                    margin:6px 0;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p>
                Hello
            </p>
            how are
            <p>
                you?
            </p>
            <button>
                Call remove() on paragraphs
            </button>
            <script>
                $("button").click( function () {
                    $("p").remove();
                });
            </script>
        </body>
    </html>
    

    ๐Ÿ“์น˜ํ™˜ (.replaceAll(), .replaceWith())

    
    <!DOCTYPE html>
    <html>
        <head>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p> Hello </p>
            <p> cruel </p>
            <p> World </p>
            <script>$("<b>Paragraph. </b>").replaceAll("p"); // check replaceWith() examples        </script>
        </body>
    </html>

    ๐Ÿ“ํด๋ž˜์Šค (.addClass(), .hasClass(), .removeClass(), .toggleClass())

    
    <!DOCTYPE html>
    <html>
        <head>
            <style>p {
                    margin: 4px;
                    font-size:16px;
                    font-weight:bolder;
                    cursor:pointer;
                }
                .blue {
                    color:blue;
                }
                .highlight {
                    background:yellow;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p class="blue"> Click to toggle </p>
            <p class="blue highlight"> highlight </p>
            <p class="blue"> on these </p>
            <p class="blue"> paragraphs </p>
            <script>
                 $("p").click( function () {
                     $(this).toggleClass("highlight");
                 });
             </script>
        </body>
    </html>

    ๐Ÿ“์†์„ฑ์ œ์–ด (.attr(), .prop(), .removeAttr(), .removeProp(), .val())

    <!DOCTYPE html>
    <html>
        <head>
            <style>p {
                    color:blue;
                    margin:8px;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <input type="text" value="some text"/>
            <p>
            </p>
            <script>$("input").keyup( function () {
                    var value = $(this).val();
                    $("p").text(value);
                }).keyup();</script>
        </body>
    </html>

    ํผ(form)

    • ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ
    • Query๋Š” ํผ์„ ์ œ์–ดํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ด๋ฒคํŠธ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
    • jQuery ํผ API ๋ฌธ์„œ : http://api.jquery.com/category/forms/

    ๐Ÿ“์˜ˆ์ œ1. (.focus(), .blur(), .change(), .select())

    <!DOCTYPE html>
    <html>
        <head>
            <style>
                span {
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p>
                <input type="text" />
                <span></span>
            </p>
            <script>
                $("input").focus( function () {
                    $(this).next("span").html('focus');
                }).blur( function() {
                    $(this).next("span").html('blur');
                }).change(function(e){
                    alert('change!! '+$(e.target).val());
                }).select(function(){
                    $(this).next('span').html('select');
                });
            </script>
        </body>
    </html>
    

    ๐Ÿ“์˜ˆ์ œ2. (.submit(), .val())

    <!DOCTYPE html>
    <html>
        <head>
            <style>
                p {
                    margin:0;
                    color:blue;
                }
                div, p {
                    margin-left:10px;
                }
                span {
                    color:red;
                }
            </style>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
        </head>
        <body>
            <p>
                Type 'correct' to validate.
            </p>
            <form action="javascript:alert('success!');">
                <div>
                    <input type="text" />
     
                    <input type="submit" />
                </div>
            </form>
            <span></span>
            <script>
                $("form").submit( function() {
                    if ($("input:first").val() == "correct") {
                        $("span").text("Validated...").show();
                        return true;
                    }
                    $("span").text("Not valid!").show().fadeOut(1000);
                    return false;
                });
            </script>
        </body>
    </html>
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€