Today I Learned (2023.12.13)

다율·2023년 12월 13일

TIL

목록 보기
3/23

CLASS101+ 웹 강의에서 배운 내용 정리 (실습 위주) - 1교시


JQuery

  • 코드를 간단하게 표현하도록 도움
  • $(선택자).val() : 값을 가지고 올 때 사용
// 프젝 시 사용하기 (JQuery)
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
// Todo list 실습 때 사용했던 코드
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js""></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js""></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	function x() {
        let todo = document.getElementById("input-area").val
        // Id이름이 "input-area"라는 태그의 value를 가져온다는 뜻 (괄호 안)
      }

=> JQuery

function y() {
        let todo = $("#input-area").val();
        // 위 함수와 같은 뜻
      }
	
  • 실습 (addTodo(), deleteTodo() 함수 구현)
	function addTodo() {        // 추가
            let todo = $("#input-area").val();
            console.log(todo);
            let id = Math.floor(Math.random() * 10) // Math.floor : 반올림을 해주는 함수
            console.log(id)
            let list_item = `<li class="list-group-item d-flex justify-content-between align-items-center list-${id}"
             style="text-align: center;" id="list-group-item">
                    ${todo}
                    <span class="badge badge-danger badge-pill"token interpolation">${id})">삭제</span>
            </li>`

            $("#list-group").append(item)  // 마지막에 새로운 요소 추가
            $("#input-area").val('')            // 빈값을 넣어 추가 후 input-area란 초기화
        }

        function deleteTodo(id) {       // 삭제
            $(".list-"+id).remove()      // 태그를 통채로 지움
        }

Ajax 함수 (안에 객체로 이루어짐)

	$(document).ready(function() {  
      // 웹 페이지가 시작될 때 바로 실행
            
    })


	$.ajax({		// ajax 기본 모양 (객체의 값이 넘거짐)
    	url:"",		// 도메인, api 주소
      	data:{},	// POST일 경우, "key":"value"으로 전달
      	type:"",	// GET, POST ...
      	success:function(json) {
        	// 성공 응답값
        },
    	error:function(err) {
        	// 에러 응답값
        }
    })

깃허브 링크

https://github.com/dayul/web-study

profile
새싹 개발자 🌱

0개의 댓글