선행작업
0. jQuery의 필요성
- 브라우저 마다 기능을 다른 코드로 해줘야하는 불편함이 있다.
- 크롬 : addEventLisrener 로 접근
- 익스플러러 : attachEvent 로 접근
- jQuery 라이브러리는 크로스브라우징 기능으로 웹 브라우저간 동등성을 제공 하여 브라우저에 대한 제약을 벗어날 수 있다 !
1. jQuery 기본 문법
- 노랑색 영역과 빨강색 영역의 차이를 알아보자
- 노랑색 영역 : jQuery 라이브러리를 사용하기 위해 불러옴
- 빨강색 영역 : script 태그 내(컨턴츠 또는 내용) jQuery 문법을 작성
1.1. head 태그에 불러오기
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/jquery-3.7.1.min.js"></script>
</head>
1.2. body 태그에 적용하기
<body>
<img src="https://lh3.googleusercontent.com/proxy/Yyqrz8WQtz0ZU9u7JDeUfZrUem-sJGH-8YPU2IORyxYGP4rvV0yNZw_5uuYfvqCBOdMTPHhts-VQzpC61CSTiy73sd9759Zsfw1OQyzuvbTIDW1Wt_UfFiM" alt="">
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBhUIBxIVFhUWGRsXGRgXGR0gIBoeGxoWGB0eIB0ZHyggGx8nJxcbITIhJikrLi4uHyszOTMuNzQtLi0BCgoKDg0OGxAQGy0mICUuLy8vNS03LS0tLTAtLS0uLS0tLS0tLS0tLy8tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAOEA4QMBEQACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAABgcDBAUCAf/EAD0QAAIBAwIEAwUECQMFAQAAAAABAgMEEQUhBhIxQQdRcRMiMmGBFUKRsRQWI1JTcqHB4TRDYkSCkrLwF//EABoBAQACAwEAAAAAAAAAAAAAAAAEBQEDBgL/xAAwEQEAAgEDBAECBAQHAAAAAAAAAQIDBBEhBRIxQRNRcSIyobEUFTPRBiM0QlJhgf/aAAwDAQACEQMRAD8AvE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AAAAAAAAAAAAAAAAAAAAABraj/oZ/ysCKcD/6uXoBNAAAAAAAAAAAAA//2Q==" alt="">
<img src="https://i.namu.wiki/i/5BHb2UsJISdW7UNCo_Q5Dld-BplCMkjfb81-YPIGGQYu5tN0FRzCAPpENK4eqmdPEELS4wGTCLbwnRy5waPNQQ.webp" alt="">
<img src="https://i.namu.wiki/i/2xsBZTJ8WjEpM62TNTV9r6PpS4Tixp946ZL0R5wXa6Oz_PddaUvyAX6qDkICeYKPhiY100UD3-EvJn4oLmGqNQ.webp" alt="">
<script>
console.log($('img'));
$('img').width('100px').height('100px');
</script>
</body>
- 결과
2. 문법습득
2.1. 요소 및 태그 접근
처음화면 |
내용 변경 클릭시 |
스타일 변경 클릭시 |
사진 변경 클릭시 |
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/jquery-3.7.1.min.js"></script>
</head>
<body>
<img id="pic" width="200px" src="https://i.namu.wiki/i/2xsBZTJ8WjEpM62TNTV9r6PpS4Tixp946ZL0R5wXa6Oz_PddaUvyAX6qDkICeYKPhiY100UD3-EvJn4oLmGqNQ.webp">
<p id="pTag">나나는 메타몽 뭐든 변해요 ~ </p>
<input type="text" id="inputTxt">
<button id="btn">내용 변경!</button>
<br>
<button id="style-btn">스타일 변경</button>
<button id="pic-btn">사진 변경</button>
<script>
let pTag = $('#pTag');
console.log("p태그 : ",pTag);
console.log("텍스트만 : ", pTag.text());
console.log("텍스트만 : ", pTag.text("메에에에엥타몽"));
pTag.html('<h1>html로 바꾼 값</h1>')
$('#btn').click( () => {
let val = $('#inputTxt').val();
console.log('input값 : ',val);
pTag.text(val);
$('#inputTxt').val('');
})
$('#style-btn').click(() => {
pTag.css('color', 'hotpink').css('fontFamily', '궁서')
})
$('#pic-btn').click(()=>{
$('#pic').attr('src', 'https://i.namu.wiki/i/5BHb2UsJISdW7UNCo_Q5Dld-BplCMkjfb81-YPIGGQYu5tN0FRzCAPpENK4eqmdPEELS4wGTCLbwnRy5waPNQQ.webp').width('100px');
})
</script>
</body>
2.2. 요소 및 태그 접근
- 다음 카운트 세어주는 기능을 만들어보자
( 0보다 작을땐 값이 음수가 되지 않는다 )
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/jquery-3.7.1.min.js"></script>
</head>
<body>
<span id="num">0</span>
<br>
<button id="plus">+1</button>
<button id="minus">-1</button>
<script>
let num = $('#num').text(0);
let count = 0;
$('#plus').click(()=>{
console.log("+버튼 클릭");
num.text(++count) ;
})
$('#minus').click(()=>{
console.log("-버튼 클릭");
if (count > 0) {
num.text(--count) ;
}
})
</script>
</body>
2.3. $(document).on('click', 클릭 될 대상, 함수)
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/jquery-3.7.1.min.js"></script>
</head>
<body>
<button id="addBtn">버튼 추가하기</button>
<div id="divTag"></div>
<script>
let btn = $('#addBtn');
let div = $('#divTag');
btn.click(() => {
div.html("<button id='newBtn'>새로운 버튼</button>");
})
$(document).on('click', '#newBtn', () => {
console.log("새로운 버튼 클릭!");
})
</script>
2.4. 버튼 기능 활용
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/jquery-3.7.1.min.js"></script>
<style>
img{
width: 200px;
height: 200px;
}
.newClass{
border : 5px solid yellow;
}
</style>
</head>
<body>
<div>
<img src="./img/bolo.jpg" alt="">
</div>
<button id="btn1">div내부 앞에 추가</button>
<button id="btn2">div내부 뒤에 추가</button>
<button id="btn3">div외부 앞에 추가</button>
<button id="btn4">div외부 뒤에 추가</button>
<br>
<button id="btn5">img 1개 삭제</button>
<button id="btn6">div 안 비우기</button>
<button id="btn7">class 추가</button>
<button id="btn8">class 삭제</button>
<button id="btn9">부모요소 접근</button>
<button id="btn10">this문법</button>
<script>
$('#btn1').click(()=>{
$('div').prepend('<img src="./img/mali.jpg" alt="">');
})
$('#btn2').click(()=>{
$('div').append('<img src="./img/meta.jpg" alt="">');
})
$('#btn3').click(()=>{
$('div').before('<img src="./img/miZz.jpg" alt="">');
})
$('#btn4').click(()=>{
$('div').after('<img src="./img/yote.jpg" alt="">');
})
$('#btn5').click(()=>{
$('#img1').remove();
})
$('#btn6').click(()=>{
$('div').empty();
})
$('#btn7').click(()=>{
$('img').addClass('newClass');
})
$('#btn8').click(()=>{
$('img').removeClass('newClass');
})
$('#btn9').click(()=>{
$('img').parent().css('backgroundColor', 'pink');
})
$('#btn10').click( function() {
$(this).text('버튼 바꾸기 성공!');
})
</script>
</body>
3. 실습 해보기
- 한상의 대모험 !
- 배경 이미지에서 벗어나지 않는 선에서 왼, 오른쪽 방향키에 맞춰 움직인다.
event
- 이벤트에 대한 모든 정보가 들어있는 객체
- 함수의 매개변수를 지정하게 되면 자동으로 event 객체 수집
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link rel="stylesheet" href="css/moveImgStyle.css" />
<script src="../js/jquery-3.7.1.min.js"></script>
</head>
<body>
<div id="bg">
<img id="hansang" src="img/hansang.png" />
</div>
<script>
let key = $('body');
let img = $('#hansang');
let point = 0;
key.on("keydown",(event)=>{
console.log(event)
console.log(event.keyCode);
if (event.keyCode == '37'){
if (point < 1050) {
point = (point + 50)
img.css('right', point+"px");
console.log("왼", point);
}
} else if (event.keyCode == '39'){
if (point > 0) {
point = (point - 50)
img.css('right', point+"px");
console.log("오", point);
}
} else if(event.keyCode == '38'){
console.log("위");
} else if (event.keyCode == '40'){
console.log("아");
} else if (event.keyCode == '32'){
$('img').stop(true).animate({"bottom":"300px"}, 500).animate({"bottom" : "150px"},500);
}
})
</script>
</body>