JQuery
โข
jQuery๋?
โฆ
๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ ํด๋ผ์ด์ธํธ ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โฆ
๋ฌด๋ฃ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ ์คํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โฆ
ํ์ฌ๋ ์ฌ์ฉ ๋น๋๊ฐ ๋ค์ ๋จ์ด์ง ์ํฉ
โฆ
jQuery๋ ๋ค์ ๊ธฐ๋ฅ์ ์ํด ์ ์๋จ
โช
๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ๊ณผ ๊ด๋ จ๋ ์ฒ๋ฆฌ๋ฅผ ์ฝ๊ฒ ๊ตฌํ
โช
์ผ๊ด๋ ์ด๋ฒคํธ ์ฐ๊ฒฐ์ ์ฝ๊ฒ ๊ตฌํ
โช
์๊ฐ์ ํจ๊ณผ๋ฅผ ์ฝ๊ฒ ๊ตฌํ
โช
Ajax ์ ํ๋ฆฌ์ผ์ด์
์ ์ฝ๊ฒ ๊ฐ๋ฐ
โฆ
๋ฌธ์๊ฐ์ฒด ์บ์คํ
ํ๋ ๋ฐ ํธํจ
โข
jquery ์์
โฆ
jquery ๊ฐ์ฒด, Interval
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery_1</title>
<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
<script>
// ํ์ด์ง ๋ก๋๊ฐ ์๋ฃ ๋๊ณ ๋์ ์คํ
// $() => jquery ๊ฐ์ฒด๋ฅผ ์์ฑํด์ค๋ค
$(document).ready(function() {
// ํ์ด์ง์ ๋ชจ๋ ์ฝํ
์ธ ๊ฐ ๋ก๋๋ ํ ์คํ
// window.onload
// document.querySelectorAll('h2').forEach((item) => {
// item.style.color = 'red'
// })
$('h2').css('color', 'blue') // set / get
// ์บ์คํ
.๋ฉ์๋
// ์บ์คํ
=> jquery ๊ฐ์ฒด ์์ฑ
/////////////////////////////////////
// input casting
// $('input[type=text]').val('์๋
ํ์ธ์') // setter
// let input_str = $('input[type=text]').val()
let input_str = $('input:text').val() // ์ ์ฝ๋์ ๊ฒฐ๊ณผ๋ ๋์ผ
console.log(input_str)
// 5์ด ํ์ ๋์
setTimeout(() => {
const selected_val = $('select > option:selected').val()
console.log('selected_val >', selected_val)
let input_str = $('input:text').val()
console.log(input_str)
const checked_val = $('input:checkbox:checked').val()
console.log('checked_val >>', checked_val)
if (!checked_val) {
alert('ํ์ธํ์ธ์!!!')
}
}, 10000)
// forEach.(์ฝ๋ฐฑ (item, index, arr))
// $.each() => ๋ฐ๋ณต ์ฒ๋ฆฌ ํด์ฃผ๋ ๋ฉ์๋
// $.each(๋ฐฐ์ด, ์ฝ๋ฐฑ(index, item))
// $('selector').each(callback)
let links = [
{ name: '๋ค์ด๋ฒ', url: 'https://www.naver.com' },
{ name: '๊ตฌ๊ธ', url: 'https://www.google.com' },
{ name: 'ํ์ด์ค๋ถ', url: 'https://www.facebook.com' }
]
let output = '';
$.each(links, (index, item) => {
console.log(index, item)
output += `<a href="${item.url}">${item.name}</a>\n<br>`
})
console.log(output)
// document.querySelector('#links').innerHTML=output
$('#links').html(output)
$('h2').each(function (index, item) {
console.log(index, item.innerHTML, $(item).html())
})
})
$().ready(() => {
})
</script>
</head>
<body>
<div id="links"></div>
<select>
<option>2024</option>
<option>2023</option>
<option>2022</option>
</select>
<input type="checkbox"> ํ์ธ
<hr>
<input type="text" name="name" id="name" value="๋๋ ์ํฅ๋ฏผ์ด๋ค.">
<hr>
<h2>header - 0</h2>
<h2>header - 1</h2>
<h2>header - 2</h2>
<h2>header - 3</h2>
<h2>header - 4</h2>
</body>
</html>
JavaScript
๋ณต์ฌ
โฆ
ํ์
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery_2</title>
<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
<script>
// ํ์ : selector, filter()
// $(selector).filter(selector)
// $(selector).filter(callback)
$(document).ready(() => {
// ์ง์๋ฒ์งธ ์ธ๋ฑ์ค๋ง ์ ํ๋จ
$('table tr').filter(':even').css('background-color', 'yellow')
// ํ์๋ฒ์งธ ์ธ๋ฑ์ค๋ง ์ ํ๋จ
$('h2').css('background', 'orange').filter(':odd').css('color', 'white')
// 3๋ฒ์งธ ์์ ์ ์ฉ
$('h1').eq(3).css('color', 'red')
// ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค
$('h1').first().css('background-color', 'pink')
// ๋ง์ง๋ง ์ธ๋ฑ์ค
$('h1').last().css('background-color', 'pink')
// 3์ ๋ฐฐ์
$('h3').filter((index, item) => {
console.log(index, item)
return index % 3 == 0
}).css('color', 'blue')
})
</script>
</head>
<body>
<h3> header 13 </h3>
<h3> header 23 </h3>
<h3> header 33 </h3>
<h3> header 43 </h3>
<h3> header 53 </h3>
<h3> header 63 </h3>
<h3> header 73 </h3>
<h3> header 83 </h3>
<h3> header 93 </h3>
<h3> header 103 </h3>
<h1> header 1 </h1>
<h1> header 1 </h1>
<h1> header 1 </h1>
<h1> header 1 </h1>
<h1> header 1 </h1>
<h1> header 1 </h1>
<h2> header 2 </h2>
<h2> header 2 </h2>
<h2> header 2 </h2>
<h2> header 2 </h2>
<h2> header 2 </h2>
<table border="1">
<tr>
<td>1111</td>
<td>2222</td>
</tr>
<tr>
<td>3333</td>
<td>4444</td>
</tr>
<tr>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>1111</td>
</tr>
<tr>
<td>1111</td>
<td>1111</td>
</tr>
</table>
</body>
</html>`
JavaScript
๋ณต์ฌ
โฆ
๋ฌธ์์ด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery_3</title>
<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
<script>
let xml = `
<links>
<link>
<name>๋ค์ด๋ฒ</name>
<url>https://www.naver.com</url>
</link>
<link>
<name>๊ตฌ๊ธ</name>
<url>https://www.google.com</url>
</link>
<link>
<name>ํ์ด์ค๋ถ</name>
<url>https://www.facebook.com</url>
</link>
</links>
`
let html_doc = ''
$().ready(() => {
// ๋ฌธ์์ด => xml ๋ฌธ์ ๊ฐ์ฒด๋ก ๋ณํ
// $.parseXML(๋ฌธ์์ด)
let xmlDoc = $.parseXML(xml)
// ์์๋ฅผ ์ฐพ๋ ๋ฉ์๋ -> find(์๋ฆฌ๋จผํธ ์ด๋ฆ) 'link'
$(xmlDoc).find('link').each(function(index, item) {
console.log(index, item)
let name = $(item).find('name').text()
let url = $(item).find('url').text()
console.log(name, url)
html_doc +=
`
<div class="box">
<h1>${name}</h1>
<p>${url}</p>
</div>
`
$('body').html(html_doc)
// this ์ฌ์ฉ์ item ์๋ต ๊ฐ๋ฅ
// $(this)
})
})
</script>
<style>
.box {
border: 1px solid #aaa;
}
</style>
</head>
<body>
</body>
</html>
JavaScript
๋ณต์ฌ
โฆ
๋ฌธ์๊ฐ์ฒด ์ปจํธ๋กค
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery_4</title>
<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
<style>
.box {
background-color: aquamarine;
color: blue;
float: left;
width: 100px;
height: 100px;
border: 1px solid #000;
margin: 10px;
}
.title {
text-align: center;
}
</style>
<script>
// ๋ฌธ์ ๊ฐ์ฒด ์ปจํธ๋กค :
// css(), attr(), removeAttr(), addClass(), removeClass()
// html(), text()
// remove(), empty()
$().ready(() => {
// $('h2').addClass('box') - ํด๋์ค ์ถ๊ฐ
// $('h2').removeClass('title') - ํด๋์ค ์ ๊ฑฐ
// console.log('๊ฒฝ๋ก : ', $('a').attr('href')) // getter
// $('a').attr('href', 'http://www.facebook.com')
// // $('img').attr('src', '/image/bonobono.jpg')
// $('img').attr({
// src : '/image/bonobono.jpg',
// width : 100,
// height : 100
// })
// ์ด๋ฏธ์ง ํฌ๊ธฐ๊ฐ ์์ฐจ์ ์ผ๋ก ์ฆ๊ฐ
$('img').attr('height', function(index){
return (index + 1) * 100
})
// ๋ง์ง๋ง ์์ฑ๊ฐ ์ ํ
$('img').last().removeAttr('src')
// attr(์์ฑ์ด๋ฆ) : read
// attr(์์ฑ์ด๋ฆ, ๊ฐ) : set
// attr(์์ฑ์ด๋ฆ, callback) : set
// attr({}) : ์ฌ๋ฌ๊ฐ์ ์์ฑ์ ํ๋ฒ์ ์ค์
// css(์์ฑ์ด๋ฆ) : read
// css(์์ฑ์ด๋ฆ, ๊ฐ || callback) : set
// css({})
// html() : innerHtml
// text() : textContent
// html read - ํ๊ทธ ํฌํจ์์ ๊ฐ์ ธ์ด (์ฝ์ ์ฐฝ์์ ํ์ธ ๊ฐ๋ฅ)
console.log('html() => ', $('#menu1').html())
// text read - ํ
์คํธ๋ง ๊ฐ์ ธ์ด (์ฝ์ ์ฐฝ์์ ํ์ธ ๊ฐ๋ฅ)
console.log('text() => ', $('#menu2').text())
// html, text ์ฐจ์ด ํ์ธ
// $('#menu1').html('<h1>์๋
ํ์ธ์</h1>')
// $('#menu2').text('<h1>์๋
ํ์ธ์</h1>')
$('div').html(function(index, html) {
console.log(index, html)
$(this).html(`<h1>${html + index}</h1>`)
})
// ์๋ฆฌ๋จผํธ ์ญ์ : ๋ถ๋ชจ ์๋ฆฌ๋จผํธ ์บ์คํ
์ญ์ ๋์ ์บ์คํ
๋ถ๋ชจ์๊ฒ ์ญ์ ์์ฒญ
$('#menu3').remove()
// ๋น์, ์ญ์ ๋๋ ๊ฒ์ ๋๊ฐ์ผ๋ ์ฝ์์์ ํ์ธ ๋จ
$('#menu2').empty()
// ๋์ ์ผ๋ก ํ๊ทธ ๋ง๋ค๊ธฐ
})
</script>
</head>
<body>
<div id="menu1"><span>Menu Text1</span></div>
<div id="menu2">
<span>Menu Text2</span>
</div>
<div id="menu3">
<span>Menu Text3</span>
</div>
<img src="/image/bonobono.jpg">
<img src="/image/bonobono.jpg">
<img src="/image/bonobono.jpg">
<a href="https://www.naver.com">naver</a>
<h2 class="title">box - 1</h2>
<h2 class="title">box - 1</h2>
<h2 class="title">box - 1</h2>
<h2 class="title">box - 1</h2>
<h2 class="title">box - 1</h2>
</body>
</html>
JavaScript
๋ณต์ฌ
โฆ
๋ฌธ์ ๊ฐ์ฒด ์์ฑ + ์ด๋ฏธ์ง ์ถ๊ฐ ๋ฐ ์ ๊ฑฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery_5</title>
<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
<style>
.img {
border: 3px solid blue;
}
</style>
<script>
$().ready(() => {
// ๋ฌธ์ ๊ฐ์ฒด ์์ฑ
$('<h1></h1>').html('์๋
ํ์ธ์1234!!!!').appendTo('body') // ์๋
ํ์ธ์1234!!!!
$('<h1>์๋
ํ์ธ์!!!!</h1>').appendTo('body') // ์๋
ํ์ธ์!!!!
// ์ด๋ฏธ์ง ์ถ๊ฐ (ํด๋์ค, ์์ฑ)
// $(A).appendTo(B) : B ๋์ ์๋๋ก ์ถ๊ฐ๋จ
// $(A).prependTo(B) : B ๋์ ์๋ก ์ถ๊ฐ๋จ
$('<img>').attr('src', '/image/bonobono.jpg').addClass('img').prependTo('body')
// $(A).appendTo(B) : ํ๊น B
// $(A).prependTo(B) : ํ๊น B
// $(A).append(B) : ํ๊น A
// $(A).prepend(B) : ํ๊น A
// $(selector).append(content, content, content,...)
// $(selector).append((index) => {})
let html1 = '<a href="#">test</a>'
let html2 = '<p>TEST</p>'
let html3 = '<div>DIV TEST</div>'
$('body').append(html1, html2, html3, '<hr>')
let productList = []
let cart = []
let links = [
{ name: '๋ค์ด๋ฒ', url: 'https://www.naver.com' },
{ name: '๊ตฌ๊ธ', url: 'https://www.google.com' },
{ name: 'ํ์ด์ค๋ถ', url: 'https://www.facebook.com' }
]
$.each(links, function (index, item) {
$('#list').append(`<a></a>`)
let html =
`
<div>
<a href="${item.name}">${item.url}</a>
</div>
<tr>
<td>${item.name}</td>
<td>${item.url}</td>
</tr>
`
$('#list').append(html)
})
})
</script>
</head>
<body>
<!-- <div id="list"></div> -->
<table border="1">
<thead>
<tr>
<th>์ด๋ฆ</th>
<th>๊ฒฝ๋ก</th>
</tr>
</thead>
<tbody id="list">
</tbody>
</table>
</body>
</html>
JavaScript
๋ณต์ฌ




