[JQuery] Form

XIXIยท2022๋…„ 6์›” 22์ผ
0

๋Œ€๊ตฌAI์Šค์ฟจ

๋ชฉ๋ก ๋ณด๊ธฐ
40/57
post-thumbnail

๐ŸŒฑ Form

โœ๏ธFormSel1

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	$(function(){
		$(":text").css("background-color", "yellow");
		$(":password").css("background-color", "red");
	});
</script>
</head>
<body>
	<input type="text"/><br/>
	<input type="password"/><br/>
	<input type="number"/><br/>
</body>

โœ๏ธFormSel2

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	$(function(){
		$(":enabled").css("background-color", "yellow");
		$(":disabled").css("background-color", "red");
	});
</script>
</head>
<body>
	<input type="text"/>ํ™œ์„ฑ ์ƒํƒœ<br/>
	<input type="password"/>ํ™œ์„ฑ ์ƒํƒœ<br/>
	<input type="text", disabled="disabled"/>๋น„ ํ™œ์„ฑ ์ƒํƒœ<br/>
	<input type="password", disabled="disabled"/>๋น„ ํ™œ์„ฑ ์ƒํƒœ<br/>
</body>

๐ŸŒฑ ์ด๋ฒคํŠธ ํ•จ์ˆ˜

โœ๏ธEventSel1

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	$(function(){
		$("#a1").click(function(){
			$("#a1").css("background-color", "black");
			$("#a1").css("color", "white");
		});

		$("#a2").dblclick(function(){
			$("#a2").css("background-color", "black");
			$("#a2").css("color", "white");
		});

		$("#a3").mouseenter(function(){
			$("#a3").css("background-color", "black");
			$("#a3").css("color", "white");
		});

		$("#a3").mouseleave(function(){
			$("#a3").css("background-color", "white");
			$("#a3").css("color", "black");
		});

		$("#a4").mousedown(function(){
			$("#a4").css("background-color", "black");
			$("#a4").css("color", "white");
		});

		$("#a4").mouseup(function(){
			$("#a4").css("background-color", "white");
			$("#a4").css("color", "black");
		});
		
		$("#a5").hover(function(){
			$("#a5").css("background-color", "black");
			$("#a5").css("color", "white");
		}, function(){
			$("#a5").css("background-color", "white");
			$("#a5").css("color", "black");
		});		
		
		$("#a6").focus(function(){
			$("#a6").css("background-color", "blue");
		});
		
		$("#a6").blur(function(){
			$("#a6").css("background-color", "red");
		});
		
		$("#a7").on("click", function(){
			alert('a7');
		});
		
		$("#a8").one("click", function(){
			alert('a8');
		});
		
		$("#a9").on({
			click : function(){
				alert('click');
			},
			mouseenter : function(){
				$("a9").css("background-color", "black");
			},
			mouseleave : function(){
				$("a9").css("background-color", "white");
			}
		});
		function remove_event(){
			$("#a7").off("click");
		}
	});
</script>
</head>
<body>
	<h3 id="a1">click</h3>
	<h3 id="a2">double click</h3>
	<h3 id="a3">mouse enter/leave</h3>
	<h3 id="a4">mouse down/up</h3>
	<h3 id="a5">mouse hover</h3>
	
	<input id="a6" type="text"/>
	
	<h3 id="a7">on</h3>
	<button type="button" onclick="remove_event()">์ด๋ฒคํŠธ ์ œ๊ฑฐ</button>
	<br/>
	
	<h3 id="a8">one</h3>
	<h3 id="a9">event setting</h3>
</body>

โ€ข click : ํด๋ฆญ
โ€ข Dblclick : ๋”๋ธ” ํด๋ฆญ
โ€ข Mouseenter : ๋งˆ์šฐ์Šค ์ปค์„œ๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ
โ€ข Mouseleave : ๋งˆ์šฐ์Šค ์ปค์„œ๊ฐ€ ๋‚˜๊ฐ”์„ ๋•Œ
โ€ข Mousedown : ๋งˆ์šฐ์Šค ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ
โ€ข Mouseup : ๋งˆ์šฐ์Šค ํ‚ค๋ฅผ ๋–ผ์—ˆ์„ ๋•Œ
โ€ข Hover : ๋งˆ์šฐ์Šค ์ปค์„œ๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ์™€ ๋‚˜๊ฐ”์„ ๋•Œ
โ€ข focus : ํฌ์ปค์Šค๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ
โ€ข Blur : ํฌ์ปค์Šค๋ฅผ ์žƒ์—ˆ์„ ๋•Œ
โ€ข on : ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•˜๋Š” ํ•จ์ˆ˜
โ€ข off : ์„ค์ •๋œ ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ํ•จ์ˆ˜
โ€ข one : ์ด๋ฒคํŠธ๋ฅผ ์„ค์ •ํ•˜๊ณ  ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ ์ œ๊ฑฐ

โœ๏ธ์ •๋ฆฌ

โ€ข jQuery๋Š” ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜๋ฅผ ์ œ๊ณต
โ€ข ์ด๋ฒคํŠธ์˜ ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌ
โ€ข on์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง€์ •๋œ ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋ก
โ€ข off๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง€์ •๋œ ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ฑฐ
โ€ข one์„ ์‚ฌ์šฉํ•˜๋ฉด 1ํšŒ์„ฑ ์ด๋ฒคํŠธ ๋“ฑ๋ก์ด ๊ฐ€๋Šฅ

๐ŸŒฑ DOM

โœ๏ธDom1

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function getA1() {
		var str = $("#a1").text();
		alert(str);
	}
	
	function getA2() {
		var str = $("#a2").text();
		alert(str);
	}
	
	function setA3() {
		$("#a3").text("๋ฌธ์ž์—ด ์„ค์ •");
	}
	
	function setHTML() {
		$("#a3").text("<a href='http://apple.co.kr'>apple</a>");
	}
</script>
</head>
<body>
	<div id="a1">a1 ๋ฌธ์ž์—ด</div>
	<button onclick="getA1()">a1 ๋ฌธ์ž์—ด ๊ฐ€์ ธ์˜ค๊ธฐ</button>
	
	<div id="a2">a2 ๋ฌธ์ž์—ด
	<a href="http://www.google.co.kr">google</a>
	</div>
	<button onclick="getA2()">a2 ๋ฌธ์ž์—ด ๊ฐ€์ ธ์˜ค๊ธฐ</button>
	
	<div id="a3"></div>
	<button onclick="setA3()">a3 ๋ฌธ์ž์—ด ์„ค์ •ํ•˜๊ธฐ</button>
	<button onclick="setHTML()">a3 html ์„ค์ •ํ•˜๊ธฐ</button>
</body>




โœ๏ธDom2

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function getHTML() {
		var html = $("#a1").html();
		alert(html);
	}
	
	function setHTML() {
		$("#a1").html("<a href='http://apple.co.kr'>apple</a>");
	}
	
</script>
</head>
<body>
	<div id="a1">
	<a href="http://www.google.co.kr">google</a>
	</div>
	<button onclick="getHTML()">html ๊ฐ€์ ธ์˜ค๊ธฐ</button>
	<button onclick="setHTML()">html ์…‹ํŒ…ํ•˜๊ธฐ</button>
</body>


โ€ข Document Object Model
โ€ข text : ํƒœ๊ทธ ์‚ฌ์ด์˜ ๋ฌธ์ž์—ด์„ ์ œ์–ด
โ€ข html : ํƒœ๊ทธ ๋‚ด๋ถ€์˜ html์„ ์ œ์–ด

โœ๏ธDom3

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function getHTML() {
		var html = $("#a1").html();
		alert(html);
	}
	
	function setHTML() {
		$("#a1").html("<a href='http://apple.co.kr'>apple</a>");
	}
	
	function getA2() {
		var value = $("#a2").val();
		alert(value);
	}
	
	function setA2() {
		$("#a2").val("1234");
	}
	
</script>
</head>
<body>
	<div id="a1">
	<a href="http://www.google.co.kr">google</a>
	</div>
	<button onclick="getHTML()">html ๊ฐ€์ ธ์˜ค๊ธฐ</button>
	<button onclick="setHTML()">html ์…‹ํŒ…ํ•˜๊ธฐ</button>
	
	<input type="text" id="a2"/>
	<br/>
	<button onclick="getA2()">value ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ</button>
	<button onclick="setA2()">value ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ</button>
</body>


โ€ข val : ์ž…๋ ฅ ๋„๊ตฌ๋“ค์˜ value ์†์„ฑ๊ฐ’์„ ์ œ์–ด

โœ๏ธattr

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function getAttr() {
		var src = $("#a1").attr("src");
		var width = $("#a1").attr("width");
		var height = $("#a1").attr("height");
		var id = $("#a1").attr("id");
		
		$("#result").html("src: "+ src + "<br/>"
						+ "width :" + width + "<br/>" 
						+ "height :" + height + "<br/>"
						+ "id :" + id + "<br/>");
	}
	
	function setAttr() {
		$("#a1").attr("width", 544);
		$("#a1").attr("height", 184);
	}


</script>
</head>
<body>
	<img src="image/๊ตฌ๊ธ€.png" width="272" height="92" id="a1"/>
	<br/>
	<div id="result"></div>
	<button onclick="getAttr()">์†์„ฑ ์ฝ์–ด์˜ค๊ธฐ</button>
	<button onclick="setAttr()">์†์„ฑ ์„ค์ •ํ•˜๊ธฐ</button>
</body>

โ€ข attr : ํƒœ๊ทธ์˜ ์†์„ฑ์„ ์ œ์–ด

โœ๏ธ์ •๋ฆฌ

โ€ข text ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒœ๊ทธ ๋‚ด๋ถ€์˜ ๋ฌธ์ž์—ด ์ œ์–ด ๊ฐ€๋Šฅ
โ€ข html ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒœ๊ทธ ๋‚ด๋ถ€์˜ html ์ฝ”๋“œ๋ฅผ ์ œ์–ด
โ€ข val ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ ๋„๊ตฌ๋“ค์˜ value ์†์„ฑ ์ œ์–ด
โ€ข attr ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒœ๊ทธ์˜ ์†์„ฑ ์ œ์–ด

โœ๏ธappend/prepend

<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function append1 () {
		$("#a1").append("<p>์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ pl</p>");
	}
	
	function append2 () {
		var p = $("<p>");
		p.text("์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ p2");
		
		$("#a1").append(p);
	}
	
	function prepend1() {
		$("#a1").prepend("<p>์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ p3</p>");
	}
	
	function prepend2() {
		var p = $("<p>");
		p.text("์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ p4");
		
		$("#a1").prepend(p);
	}
</script>
<style>
	#a1 {
		border: 1px solid black;
	}
</style>
</head>
<body>
	<div id="a1">
		<p>p ํƒœ๊ทธ</p>		
	</div>
	<button onclick="append1()">HTML ์ฝ”๋“œ๋ฅผ ๋’ค์— ์ถ”๊ฐ€</button>
	<button onclick="append2()">HTML ๊ฐ์ฒด๋ฅผ ๋’ค์— ์ถ”๊ฐ€</button>
	<button onclick="prepend1()">HTML ์ฝ”๋“œ๋ฅผ ์•ž์— ์ถ”๊ฐ€</button>
	<button onclick="prepend2()">HTML ๊ฐ์ฒด๋ฅผ ์•ž์— ์ถ”๊ฐ€</button>
</body>

โœ๏ธafter/before

<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function after1 () {
		$("#a1").after("<p>์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ p ํƒœ๊ทธ 1</p>");
	}
	
	function after2 () {
		var p = $("<p>");
		p.text("์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ pํƒœ๊ทธ 2");
		
		$("#a1").after(p);
	}
	
	function before1() {
		$("#a1").before("<p>์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ pํƒœ๊ทธ 3</p>");
	}
	
	function before2() {
		var p = $("<p>");
		p.text("์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ pํƒœ๊ทธ 4");
		
		$("#a1").before(p);
	}
</script>
<style>
	#a1 {
		border: 1px solid black;
	}
</style>
</head>
<body>
	<div id="a1">
		<p>p ํƒœ๊ทธ</p>		
	</div>
	<button onclick="after1()">HTML ์ฝ”๋“œ๋ฅผ ๋’ค์— ์ถ”๊ฐ€</button>
	<button onclick="after2()">HTML ๊ฐ์ฒด๋ฅผ ๋’ค์— ์ถ”๊ฐ€</button>
	<button onclick="before1()">HTML ์ฝ”๋“œ๋ฅผ ์•ž์— ์ถ”๊ฐ€</button>
	<button onclick="before2()">HTML ๊ฐ์ฒด๋ฅผ ์•ž์— ์ถ”๊ฐ€</button>
</body>

โ€ข after : html ์ฝ”๋“œ๋‚˜ ํƒœ๊ทธ๋ฅผ ํƒœ๊ทธ ๋‹ค์Œ์— ๋ฐฐ์น˜
โ€ข before : html ์ฝ”๋“œ๋‚˜ ํƒœ๊ทธ๋ฅผ ํƒœ๊ทธ ์•ž์— ๋ฐฐ์น˜

โœ๏ธremove/empty

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function remove_a2(){
		$("#a2").remove();
	}
	function empty_a1(){
		$("#a1").empty();
	}
</script>
<style>
	#a1 {
		border: 1px solid black;
	}	
</style>
</head>
<body>
	<div id ="a1">
		<p>p ํƒœ๊ทธ</p>
		<p id="a2">id๊ฐ€ a2์ธ pํƒœ๊ทธ</p>
	</div>
	<button onclick="remove_a2()">a2 ์ œ๊ฑฐ</button>
	<button onclick="empty_a1()">a1 ๋‚ด๋ถ€์˜ ๋ชจ๋“  ํƒœ๊ทธ ์ œ๊ฑฐ</button>
</body>


โ€ข remove : ํƒœ๊ทธ๋ฅผ ์ œ๊ฑฐ
โ€ข empty : ํƒœ๊ทธ ๋‚ด์˜ ๋ชจ๋“  ํƒœ๊ทธ๋ฅผ ์ œ๊ฑฐ

โœ๏ธ์ •๋ฆฌ

โ€ข append : html ์ฝ”๋“œ๋‚˜ ํƒœ๊ทธ๋ฅผ ํƒœ๊ทธ ๋‚ด๋ถ€์˜ ๋’ค์— ์ถ”๊ฐ€
โ€ข prepend : html ์ฝ”๋“œ๋‚˜ ํƒœ๊ทธ๋ฅผ ํƒœ๊ทธ ๋‚ด๋ถ€์˜ ์•ž์— ์ถ”๊ฐ€
โ€ข after : html ์ฝ”๋“œ๋‚˜ ํƒœ๊ทธ๋ฅผ ํƒœ๊ทธ ๋’ค์— ๋ถ™ํž˜
โ€ข before : html ์ฝ”๋“œ๋‚˜ ํƒœ๊ทธ๋ฅผ ํƒœ๊ทธ ์•ž์— ๋ถ™ํž˜
โ€ข remove : ํƒœ๊ทธ๋ฅผ ์ œ๊ฑฐ
โ€ข empty : ํƒœ๊ทธ ๋‚ด์˜ ๋ชจ๋“  ํƒœ๊ทธ๋ฅผ ์ œ๊ฑฐ

๐Ÿƒ ์–ด๋ ค์› ๋˜ ์  or ํ•ด๊ฒฐ๋ชปํ•œ ๊ฒƒ

์ด๋ฏธ์ง€๊ฐ€ ๊นจ์ง€๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค.

๐Ÿ€ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• ์ž‘์„ฑ

โœ๏ธ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐ์„ ํ–ˆ๋Š”๊ฐ€?
โœ๏ธ ์ด๋ ‡๊ฒŒ ์ดํ•ด๋ฅผ ํ–ˆ๋‹ค
โœ๏ธ ์–ด๋””๊นŒ์ง€ ์ดํ•ดํ–ˆ์ง€?
์ž‘์„ฑ๋œ ์ฝ”๋“œ์™€ ๋‹ค์šด๋กœ๋“œ ์ด๋ฏธ์ง€ ํŒŒ์ผ๋ช…์— ์ฐจ์ด๊ฐ€ ์žˆ์—ˆ์Œ.
ํŒŒ์ผ๋ช…์„ ๋™์ผํ•˜๊ฒŒ ๋ฐ”๊พธ์—ˆ์œผ๋‚˜ ์—ฌ์ „ํžˆ ๊นจ์ง€๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ.
ํด๋” ๊ฒฝ๋กœ๋ฅผ ํ•œ๋ฒˆ ๋” ํ™•์ธํ•˜๊ณ  ์—ฐ๊ฒฐํ•  ํ•„์š”๊ฐ€ ์žˆ์–ด ๋ณด์ž„.
โœ๏ธ ๋‹ค์Œ์— ์‹œ๋„ํ•ด๋ณผ ๋ฐฉ๋ฒ•

๐ŸŒท ํ•™์Šต ์†Œ๊ฐ

css์™€ JQuery๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋‹ค์–‘ํ•œ ํšจ๊ณผ, ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ๋งŒ๋“ค์–ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

profile
Life is experience:)

0๊ฐœ์˜ ๋Œ“๊ธ€