[JS + jquery] ์ œ์ด์ฟผ๋ฆฌ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋งŒ๋“ค์–ด๋ณด๊ธฐ

2021. 11. 3. 11:04ใ†HTML + CSS + JS/JS ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

728x90

1. $.fn.extend() ๋ฉ”์„œ๋“œ 

$.fn.extend() ๋ฉ”์„œ๋“œ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ œ์ž‘ํ•œ ํ•จ์ˆ˜๋ฅผ jQuery์— ํ™•์žฅ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. 

 

1๏ธโƒฃ

$.fn.extend({

  ํ™•์žฅ ํ•จ์ˆ˜๋ช… : function(๋งค๊ฐœ๋ณ€์ˆ˜1, ๋งค๊ฐœ๋ณ€์ˆ˜2..) {

       $(this);

       ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ;

   }

});

 

$("์š”์†Œ ์„ ํƒ").ํ™•์žฅ ํ•จ์ˆ˜๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜1, ๋งค๊ฐœ๋ณ€์ˆ˜2..);

 

2๏ธโƒฃ

$.fn.ํ™•์žฅํ•จ์ˆ˜๋ช… = function(๋งค๊ฐœ๋ณ€์ˆ˜1, ๋งค๊ฐœ๋ณ€์ˆ˜2..) {

   ..

}

 

 

2. 2๏ธโƒฃ ๋ฐฉ๋ฒ• ์‚ฌ์šฉํ•ด์„œ ์ œ์ด์ฟผ๋ฆฌ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋งŒ๋“ค์–ด๋ณด๊ธฐ 

๋ฒ„ํŠผ1 mouserover 
๋ฒ„ํŠผ2 click

 

 

3. 1๏ธโƒฃ ๋ฐฉ๋ฒ• ์‚ฌ์šฉํ•ด์„œ ์ œ์ด์ฟผ๋ฆฌ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋งŒ๋“ค์–ด๋ณด๊ธฐ 

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> Plugin </title>
<style>
	*{margin: 0;padding: 0;}
	li{list-style: none;}
	body{font: 14px Malgun Gothic,"๋ง‘์€๊ณ ๋”•",sans-serif;color: #555;}
	h1{font-size:1em;margin-bottom:10px;}
</style>
<link rel="stylesheet" href="css/faq.css">
<script src="js/jquery.js"></script>
<script>
$(function() {
	$.fn.extend({
		faq : function(){
			var ts = $(this);
			$.each(ts, function(i, o){
				$("button", o).on("click", tabMenu);

				function tabMenu() {
					if($(this).parent().next().is(":hidden")) {
						$("li>div:visible", o).show();
						$(this).parent().next().show();
					}else {
						$("li>div:visible", o).hide();
					}
				}
			});
		}
	});

	$(".faq").faq();
});
</script>
</head>
<body>
	<h1>์งˆ๋ฌธ ์ œ๋ชฉ1</h1>
	<div class="faq">
		<ul>
			<li>
				<p><button>Q1. ์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ ๋‚ด์šฉ?</button></p>
				<div>
					<div class="cont">๋‹ต๋ณ€ ๋‚ด์šฉ1</div>
				</div>
			</li>
			<li>
					<p><button>Q2. ์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ ๋‚ด์šฉ?</button></p>
					<div>
						<div class="cont">๋‹ต๋ณ€ ๋‚ด์šฉ2</div>
					</div>
			</li>
		</ul>
	</div>
	<h1>์งˆ๋ฌธ ์ œ๋ชฉ2</h1>
	<div class="faq">
		<ul>
			<li>
				<p><button>Q1. ์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ ๋‚ด์šฉ?</button></p>
				<div>
					<div class="cont">๋‹ต๋ณ€ ๋‚ด์šฉ1</div>
				</div>
			</li>
			<li>
				<p><button>Q2. ์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ ๋‚ด์šฉ?</button></p>
				<div>
					<div class="cont">๋‹ต๋ณ€ ๋‚ด์šฉ2</div>
				</div>
			</li>
		</ul>
	</div>
</body>
</html>

 

 

 

 

 

 

 

 

 

728x90