
์ด๋ฒคํธ๋?
- ์์คํ ์์ ์ผ์ด๋๋ ์ฌ๊ฑด์ ์๋ฏธ
- 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>'Frontend > ๐ JQuery' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| JQuery ์ ๋ณตํ๊ธฐ 4 (ํ์, animation, ajax) (0) | 2022.07.06 | 
|---|---|
| JQuery ์ ๋ณตํ๊ธฐ 2 (wrapper, ์ ํ์, chain) (0) | 2022.07.06 | 
| JQuery ์ ๋ณตํ๊ธฐ 1 (Library, JQuery, javascript์ ๋น๊ต) (0) | 2022.07.06 | 
๋๊ธ