jQuery cdn head์ ๋ฃ์
์ ๋ ํฐ html ๋ณต์ฌํด์ ์์
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery - ์
๋ ํฐ</title>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"
></script>
<style>
body {
font-size: 17px;
font-family: Arial, Helvetica, sans-serif;
background: #f4f4f4;
line-height: 1.5em;
}
header {
background: #333;
color: white;
padding: 20px;
text-align: center;
border-bottom: 4px solid black;
margin-bottom: 10px;
}
#container {
width: 90%;
margin: auto;
padding: 10px;
}
</style>
</head>
<body>
<header>
<h1>jQuery ์ด๋ฒคํธ</h1>
</header>
<div id="container">
<h3>Mouse Events</h3>
<button id="btn1">Button 1</button>
<button id="btn2">Button 2</button>
</div>
<script>
$("#btn1").click(function () {
alert("Button Clicked!");
});
</script>
</body>
</html>
$("#btn1").click(function(){
alert('Button Clicked!");
});
= id btn1์ ํด๋ฆญํ๋ฉด alertํจ์๊ฐ ์คํ๋๋ค.
์คํ ๊ฒฐ๊ณผ


button1๋ฅผ ํด๋ฆญํ๋ฉด alert ์ฐฝ์ด ๋ธ
button2๋ฅผ ํด๋ฆญํ๋ฉด ์๋ฌด๋ฐ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ง ์์.
์๋ script ๋ bodyํ๊ทธ ์ ์ผ ํ๋จ์ ์ฐ๋๋ฐ , ์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ๋ฉด
script๋ฅผ ์ด๋์ ์์ฑํ๋ HTML์ด ์ค๋น๋๋ฉด ํด๋น ๋ด์ฉ ์คํํจ
<script>
//script ์์น๊ฐ ์ด๋์ ์๋ ๊ฐ์ ์น๋ฌธ์๊ฐ ๋ค ์ค๋น๋๋ฉด ํด๋น ๋ด์ฉ ์คํ
$(document).ready(function () {
$("#btn1").click(function () {
alert("Button Clicked!");
});
$("#btn2").click(function () {
alert("Button2 Clicked!");
});
});
</script>
button1์ ๋๋ฅด๋ฉด ํด๋น ๋ด์ฉ์ ์จ๊ธฐ๊ณ button 2๋ฅผ ๋๋ฅด๋ฉด ์จ๊ฒจ๋์ ๋ด์ฉ์ ๋ค์ ๋ณด์ด๊ฒ ๋ง๋ค์ด ๋ณด๊ธฐ
<div id="container">
<h3>Mouse Events</h3>
<button id="btn1">Button 1</button>
<button id="btn2">Button 2</button>
<p class="para1">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Autem error
unde consequatur. Nulla odit dolorum earum nobis recusandae rem
accusantium officiis, quibusdam enim laudantium optio quod non vel,
aliquam doloribus.
</p>
</div>
**** ์ฐธ๊ณ : pํ๊ทธ ์์ Lorem ์ ์ ๋ ฅํ๊ณ space + enter ๋๋ฅด๋ฉด ๋๋คํ ๋ฌธ์ฅ ์์ฑ๋จ
<script>
button 1 ํด๋ฆญํ๋ฉด ๋ฌธ์ฅ ์ถ๋ ฅ, button 2๋ฅผ ํด๋ฆญํ๋ฉด ๋ฌธ์ฅ ์จ๊น ๊ฐ๋ฅ
์ด ๋ ๊ฐ์ง ๊ธฐ๋ฅ์ ํฉ์น๊ฒ toggle()์ด๋ค.
์ด๋ฒคํธ ๊ฐ์ฒด (event object)
์ด๋ฒคํธ ํธ๋ค๋ฌ ํจ์๋ jQuery์์ ์ฝ๋ฐฑ๋ ๋ ์ด๋ฒคํธ ๊ฐ์ฒด(event object)๋ฅผ ํจ์์ ์ธ์๋ก ์ ๋ฌ๋๋ค.
์ ๋ฌ๋ฐ์ ์ด๋ฒคํธ ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ์ด๋ฒคํธ์ ํน์ฑ์ ๊ฒฐ์ ํ๊ฑฐ๋, ์ด๋ฒคํธ์ ๊ธฐ๋ณธ ๋์์ ๋ง์ ์๋ ์๋ค.
ํ๋ฉด์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ฆฌ๋ฉด ์ขํ๋ฅผ ์ถ๋ ฅํ๊ฒ๋ ์ฝ๋ ์์ฑ
<script>

ํผ input์ focus๊ฐ ๋๋ฉด background ์์ ๋ฐ๋๊ณ focus๊ฐ ๋์ง ์์ผ๋ฉด ์๋์ ์์์ผ๋ก ๋์๊ฐ๋ ์ฝ๋ ์์ฑ
<form action="">
<label>Name</label><br />
<input type="text" id="name" name="name" />
<br />
<label>Email</label><br />
<input type="email" id="emial" name="email" />
<br />
<label>Gender</label><br />
<select name="gender" id="gender">
<option value="mail">Male</option>
<option value="femail">Female</option>
</select>
<br />
<input type="submit" value="Submit" />
</form>
script


name์ด๋ email ์์ฑ ์ console.log์ ์ถ๋ ฅ

gender ์ ํ ๋ณ๊ฒฝ ์ alert์ฐฝ ์ถ๋ ฅํ๊ฒ ๋ง๋ค๊ธฐ

console log์๋ ๋ณ๊ฒฝ๋ ์ฑ๋ณ ์ถ๋ ฅํ๊ฒ ๋ง๋ค๊ธฐ

'FRONTEND > ๊ธฐํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| jQuery ๋ฅผ ํ์ฉํด AJAX ์ฌ์ฉ load() (1) | 2023.10.06 |
|---|---|
| Dom ์์ฉ์ค์ต (1) | 2023.10.05 |
| jQuery ? (1) | 2023.10.05 |
| id๋ก DB๊ฒ์ํ์ฌ ๊ฒฐ๊ณผ ์ถ๋ ฅ (0) | 2023.10.04 |
| AJAX๋ก ๊ตฌ๊ตฌ๋จ ์ถ๋ ฅ (1) | 2023.10.04 |