Frontend/๐ JQuery
                
              JQuery ์ ๋ณตํ๊ธฐ 4 (ํ์, animation, ajax)
                haeunkim.on
                 2022. 7. 6. 14:18
              
              
            
            
ํ์
- ์ฒด์ธ ์ปจํ ์คํธ๋ฅผ ์ ์งํ๋ฉด์ ์ ์ด์ ๋์์ด ๋๋ ์๋ฆฌ๋จผํธ๋ฅผ ๋ณ๊ฒฝํ๋ ๊ธฐ๋ฒ
 - http://api.jquery.com/category/traversing/
 
๐ ์์ 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style>
            body{
                font-size:11px;
                width:1000px;
            }
            #panel div,#panel li,#panel ul{
                border:2px solid black;
                margin:10px;
                padding:10px;
            }
            #panel ul{
                list-style: none;
            }
            #panel .s{
                border:2px solid red;
                background-color: #808080;
            }
            #panel #root{
##                 margin-top:0;
            }
            textarea{
####                 width:1982px;
                height:100px;
                font-size:11px;
                overflow: visible;
            }        
            #help{
                float:left;
                width:500px;
                height:450px;
                overflow-y: scroll;
                overflow-x: hidden
            }
         ```   #panel{
                float:left;
                width:2500px;
            }
            #help table{
                border:1px solid gray;
                border-collapse: collapse;
                width:100%;
         ```   }
            #help table td{
                border:1px solid gray;
                padding:5px;
            }
            #help .title{
                color:white;
                background-color:#555;
                padding:3px;
            }
            #help .title.checked{
                background-color:red;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <div id="wrapper">
            <p>
                javascript์ ์
๋ ฅ ํ ํ์ ์ํฐ๋ฅผ ๋๋ฌ์ฃผ์ธ์.
                <textarea id="code"></textarea></p>
            <div id="help">
                <table>
                    <tr id="add"><td><div class="title">.add(selector)</div>์๋ฆฌ๋จผํธ๋ฅผ ์ถ๊ฐํ๋ค</td></tr>
                    <tr id="andSelf"><td><div class="title">.andSelf()</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
์ ์ด์  ์๋ฆฌ๋จผํธ ์
์ ๋ ํ๋ค</td></tr>
                    <tr id="children"><td><div class="title">.children([selector])</div>์์ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="closet"><td><div class="title">.closest(selector)</div>๊ฐ์ฅ ๊ฐ๊น์ด selector ์กฐ์ ์๋ฆฌ๋จผํธ๋ฅผ ํ์ํ๋ค</td></tr>
                    <tr id="each"><td><div class="title">.each(function(index,Element))</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
์ ๋ฐ๋ณต ์์
์ ์คํํ๋ค</td></tr>
                    <tr id="end"><td><div class="title">.end()</div>์ด์  ์ฒด์ธ ์ปจํ
์คํธ๋ก ๋์๊ฐ๋ค.</td></tr>
                    <tr id="eq"><td><div class="title">.eq(index)</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
์์ index์ ํด๋นํ๋ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="filter"><td><div class="title">.filter(selector)</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
์์ selector์ ํด๋นํ๋ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="find"><td><div class="title">.find(selector)</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
์์ selector์ ํด๋นํ๋ ์์ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="first"><td><div class="title">.first()</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
 ์ค ์ฒซ๋ฒ์งธ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="last"><td><div class="title">.last()</div>ํ์ฌ ์๋ฆฌ๋จผํธ ์
 ์ค ๋ง์ง๋ง ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="next"><td><div class="title">.next()</div>๊ฐ๊ฐ์ ์๋ฆฌ๋จผํธ์ ๋ํ ๋ค์ ํ์ฌ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="nextAll"><td><div class="title">.nextAll()</div>๊ฐ๊ฐ์ ์๋ฆฌ๋จผํธ์ ๋ํ ๋ค์ ํ์ฌ ์๋ฆฌ๋จผํธ ์ ๋ถ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="prev"><td><div class="title">.prev()</div>๊ฐ๊ฐ์ ์๋ฆฌ๋จผํธ์ ๋ํ ์ด์  ํ์ฌ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="prevAll"><td><div class="title">.prevAll()</div>๊ฐ๊ฐ์ ์๋ฆฌ๋จผํธ์ ๋ํ ์ด์  ํ์ฌ ์๋ฆฌ๋จผํธ ์ ๋ถ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="siblings"><td><div class="title">.siblings()</div>๊ฐ๊ฐ์ ์๋ฆฌ๋จผํธ์ ๋ํ ํ์ฌ ์๋ฆฌ๋จผํธ ์ ๋ถ๋ฅผ ์ ํํ๋ค</td></tr>
                    <tr id="slice"><td><div class="title">.slice(start, [end])</div>ํ์ ์ ์๋ฆฌ๋จผํธ ์
 ์ค start์์ end๊น์ง์ ์๋ฆฌ๋จผํธ๋ฅผ ์ ํํ๋ค</td></tr>
                </table>
            </div>
            <div id="panel">
                <div id="root">
                    div#root
                    <div>
                        div
                    </div>
                    <div>
                        div
                        <ul>
                            ul
                            <li>li</li>
                            <li>li</li>
                            <li>li</li>
                            <li>li</li>
                        </ul>
                    </div>
                    <div>
                        div
                    </div>
                </div>    
            </div>
        </div>
        <script>
            var $wrapper = $('#root').addClass('selected');
            $('#code').keydown(function(e){
                if(e.keyCode == 13){
                    eval($(this).val());
                    return false;
                }
            }).change(function(){
                    eval($(this).val());
            });
            $('tr').click(function(){
                $(this).find('.title').toggleClass('checked');
            })
        </script>
    </body>
</html>
ํจ๊ณผ๋?
- ์๋ฐ์คํฌ๋ฆฝํธ์ CSS๋ฅผ ์ด์ฉํด์ HTML์๋ฆฌ๋จผํธ์ ๋์ ์ธ ํจ๊ณผ๋ฅผ ์ค ์ ์๋ค.
 - jQuery์ ํจ๊ณผ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ๋ง์ผ๋ก ๊ฐ๋จํ ํจ๊ณผ๋ฅผ ์ค ์ ์๋ค.
 
๐ ์์ 1
<!DOCTYPE html>
<html>
    <head>
        <style>        span {
                color:red;
                cursor:pointer;
            }
            div {
                margin:3px;
                width:80px;
                height:80px;
            }
            div {
                background:#f00;
            }
</style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <input type="button" id="fadeout" value="fade out" />
        <input type="button" id="fadein" value="fade in" />
        <input type="button" id="hide" value="hide" />
        <input type="button" id="show" value="show" />
        <input type="button" id="slidedown" value="slide down" />
        <input type="button" id="slideup" value="slide up" />
        <input type="button" id="mix" value="mix" />
        <div id="target">
            target
        </div>
        <script>$('input[type="button"]').click( function(e) {
                var $this = $(e.target);
                switch($this.attr('id')) {
                    case 'fadeout':
                        $('#target').fadeOut('slow');
                        break;
                    case 'fadein':
                        $('#target').fadeIn('slow');
                        break;
                    case 'hide':
                        $('#target').hide();
                        break;
                    case 'show':
                        $('#target').show();
                        break;
                    case 'slidedown':
                        $('#target').hide().slideDown('slow');
                        break;
                    case 'slideup':
                        $('#target').slideUp('slow');
                        break;
                    case 'mix':
                        $('#target').fadeOut('slow').fadeIn('slow').delay(1000).slideUp().slideDown('slow', function(){alert('end')});
                        break;
                }
            }) 
        </script>
    </body>
</html>
๐ ์์ 2
<!DOCTYPE html>
<html>
    <head>
        <style>        
            div {
                background-color:#bca;
                width:100px;
                border:1px solid green;
            }
</style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <button id="go">
            » Run
        </button>
 
        <div id="block">
            Hello!
        </div>
        <script>/* Using multiple unit types within one animation. */
 
            $("#go").click( function() {
                $("#block").animate({
                    width: "300px",
                    opacity: 0.4,
                    marginLeft: "50px",
                    fontSize: "30px",
                    borderWidth: "10px"
                }, 3000);
            });</script>
    </body>
</html>
ajax๋
- Asynchronous JavaScript and XML ์ ์ฝ์
 - ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ด์ฉํด์ ๋น๋๊ธฐ์์ผ๋ก ์๋ฒ์ ํต์ ํ๋ ๋ฐฉ์. ์ด ๋ XML์ ์ด์ฉํ๋ค.
 - ๊ผญ XML์ ์ด์ฉํ ํ์๋ ์๊ณ , ์ต๊ทผ์๋ json์ ๋ ๋ง์ด ์ด์ฉํ๋ค.
 - ๋น๋๊ธฐ์์ด๋ ์ฌ๋ฌ๊ฐ์ง ์ผ์ด ๋์์ ์ผ๋ก ๋ฐ์ํ๋ค๋ ๋ป์ผ๋ก, ์๋ฒ์ ํต์ ํ๋ ๋์ ๋ค๋ฅธ ์์ ์ ํ ์ ์๋ค๋ ์๋ฏธ.
 
$.ajax(settings)
- jQuery๋ฅผ ์ด์ฉํ ajaxํต์ ์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ API
 - ์ฃผ์์์ฑ
- data : ์๋ฒ์ ์ ์กํ ๋ฐ์ดํฐ, key/value ํ์์ ๊ฐ์ฒด
- dataType : ์๋ฒ๊ฐ ๋ฆฌํดํ๋ ๋ฐ์ดํฐ ํ์ (xml, json, script, html)
- type : ์๋ฒ๋ก ์ ์กํ๋ ๋ฐ์ดํฐ์ ํ์ (POST, GET)
- url : ๋ฐ์ดํฐ๋ฅผ ์ ์กํ URL
- success : ajaxํต์ ์ ์ฑ๊ณตํ์ ๋ ํธ์ถ๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ 
๐ ์์ 1-1. ์นํ์ด์ง
<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <div id="result"></div>
        <input type="text" id="msg" />
        <input type="button" value="get result" id="getResult" />
        <script>
            $('#getResult').click( function() {
                $('#result').html('');
                $.ajax({
                    url:'http://opentutorials.org/example/jquery/example.jquery.ajax.php',
                    dataType:'json',
                    type:'POST',
                    data:{'msg':$('#msg').val()},
                    success:function(result){
                        if(result['result']==true){
                          $('#result').html(result['msg']);
                        }
                    }
                });
            })
        </script>
    </body>
</html>
๐ ์์  1-2. ์๋ฒ ์ชฝ ๋ก์ง
<?
echo json_encode(array('result'=>true, 'msg'=>$_REQUEST['msg']));
?>
๋ฐ์ํ