스파르타 코딩클럽 2주차

BYEONGMIN CHOI·2022년 4월 28일
0

WIL(Week I Learned)

목록 보기
2/8

JQuery & Ajax 를 잠깐 배움

JQuery 시작하기

JQuery란?

HTML의 요소들을 조작하는, 편리한 Javascript를 미리 작성해둔 것. 라이브러리

\rightarrow Javascript로도 모든 기능(예 - 버튼 글씨 바꾸기 등)을 구현할 수는 있지만,
1) 코드가 복잡하고, 2) 브라우저 간 호환성 문제도 고려해야해서,
jQuery라는 라이브러리가 등장하게 되었답니다.

jQuery와 Javascript - 코드 비교

\rightarrow jQuery는 Javascript와 다른 특별한 소프트웨어가 아니라 미리 작성된 Javascript 코드입니다. 전문 개발자들이 짜둔 코드를 잘 가져와서 사용하는 것임을 기억해주세요! (그렇게 때문에, 쓰기 전에 "임포트"를 해야합니다!)

// javascript
document.getElementById("element").style.display = "none";
// jQuery
$('#element').hide();
jQuery 사용하기
  • 미리 작성된 javascript 코드를 가져와 사용. import하여 사용

    jQuery CDN 부분을 참고해서 임포트하기: 링크

    <!--jQuery import--> 
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
jQuery를 사용하는 방법

\rightarrow css와 마찬가지로, jQuery를 쓸 때에도 "가리켜야" → 조작할 수 있습니다.
예) 특정 인풋박스의 값을 → 가져와줘!
예) 특정 div를 → 안보이게 해줘!
 
css에서는 선택자로 class를 썼으면,
jQuery에서는 id 값을 통해 특정 버튼/인풋박스/div/.. 등을 가리키게 됩니다.

2주차 숙제

jQuery 와 Ajax를 이용하여 실시간 날씨 API를 사용하여 날씨를 표시
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
            integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous"></script>

    <title>스파르타코딩클럽 | 부트스트랩 연습하기</title>
    <link href="https://fonts.googleapis.com/css2?family=Gowun+Dodum&display=swap" rel="stylesheet">
    <style>
        * {
            font-family: 'Gowun Dodum', sans-serif;
        }

        .mytitle {
            width: 100%;
            height: 300px;

            background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.25)), url("https://cdn.topstarnews.net/news/photo/202112/14654960_734043_239_org.jpg");
            background-position: center;
            background-size: contain;

            color: white;

            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
        .mypost {
            max-width: 500px;
            width: 95%;
            margin: 20px auto 0px auto;
            box-shadow: 0px 0px 3px 0px;
            padding: 20px;
        }
        .mypost > button {
            margin-top: 15px;
        }
        .mybtns{
            display: flex;
            flex-direction: row;
            justify-content: left;
            align-items: center;
            margin-top: 20px;
        }
        .mycard {
            /*border: 1px solid;*/
            width: 95%; /*모바일에서 보여지는 것을 처리하기 위해*/
            max-width: 500px; /*모바일에서 보여지는 것을 처리하기 위해*/
            margin: auto;
            /*box-shadow: 0px 0px 3px 0px grey;*/
            /*padding: 20px;*/
        }
        .mycard > .card {
            margin-top: 10px;
            margin-bottom: 10px;
            box-shadow: 0px 0px 3px 0px grey;
        }
    </style>
    <script>
        $(document).ready(weather_show());
        function add_comment(){
            let name = $(`#name`).val()
            let comment = $(`#comment`).val()
            let tmp_html = `<div class="card">
                                <div class="card-body">
                                    <blockquote class="blockquote mb-0">
                                    <p>${comment}</p>
                                    <footer class="blockquote-footer">${name}</footer>
                                    </blockquote>
                                </div>
                            </div>`
            $(`#list`).append(tmp_html)
        }

        function weather_show() {
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/sparta_api/weather/seoul",
                data: {},
                success: function (response) {
                    let icon = response['icon']
                    let temp = response['temp']

                    $(`#weather_icon`).attr('src', icon)
                    $(`#temp`).text(temp)
                }
            })
        }

    </script>
</head>

<body>
    <div class="mytitle">
        <h1>아이유(IU) 팬명록</h1>
        <div>
            <p>현재 기온 : <span id="temp">00.00</span><img id="weather_icon" width="30" src=""/>
            </p>
        </div>
    </div>

    <div class="mypost">
        <div class="form-floating mb-3">
            <input type="email" class="form-control" id="name" placeholder="name@example.com">
            <label for="name">닉네임</label>
        </div>
        <div class="form-floating">
            <textarea class="form-control" placeholder="Leave a comment here" id="comment" style="height: 100px"></textarea>
            <label for="comment">응원 댓글</label>
        </div>
        <div class="mybtns">
            <button onclick="add_comment()" type="button" class="btn btn-dark">응원 남기기</button>
        </div>

    </div>
    <div class="mycard" id="list">

    </div>
</body>

</html>

profile
스스로 성장하는 개발자가 되겠습니다.

0개의 댓글