๐Ÿ’ป

JavaScript ์˜ ์ดํ•ด - [jQuery] Selector(2) : ํ•„ํ„ฐ ๋ณธ๋ฌธ

KITRI/JAVASCRIPT

JavaScript ์˜ ์ดํ•ด - [jQuery] Selector(2) : ํ•„ํ„ฐ

๋˜ํšจ๋‹ˆ 2020. 7. 21. 11:31

https://api.jquery.com/category/selectors/basic-filter-selectors/

 

Basic Filter | jQuery API Documentation

Select all elements that are in the progress of an animation at the time the selector is run. Select the element at index n within the matched set. Selects even elements, zero-indexed. See also :odd. Selects the first matched DOM element. Selects element i

api.jquery.com

10_BasicFilter.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - ๊ธฐ๋ณธ ํ•„ํ„ฐ ์„ ํƒ์ž</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("p:even").css("color","red"); 	//์ง์ˆ˜์„ ํƒ(0๋ฒˆ์ง€)
		$("p:first").css("color","blue");
		$("p:last").css("color","pink");
		$("p:eq(2)").css("color","yellow"); //(๋ฒˆ์ง€๊ฐ’)
		
		//class๊ฐ€ first์ธ ํƒœ๊ทธ์˜ ์ž์‹ ์ค‘์— ์ง์ˆ˜๋ฒˆ์งธ liํƒœ๊ทธ
		$(".first > li:even").css("color", "blue");
		//class๊ฐ€ first์ธ ํƒœ๊ทธ์˜ ์ž์‹ ์ค‘์—  ํ™€์ˆ˜๋ฒˆ์งธ liํƒœ๊ทธ
		$(".first > li:odd").css("color", "red");
		
		$(".second > li:first").css("color", "red");
		$(".second > li:last").css("color", "blue");
		$(".second > li:eq(2)").css("color", "yellow");
		
		//class๊ฐ€ third์ธ liํƒœ๊ทธ์˜ 2๋ฒˆ์งธํƒœ๊ทธ๋ณด๋‹ค ์ธ๋ฑ์Šค๊ฐ€ ํฐ ๊ฒƒ ์„ ํƒ
		$(".third > li:gt(2)").css("color", "red");
		//class๊ฐ€ third์ธ liํƒœ๊ทธ์˜ 2๋ฒˆ์งธํƒœ๊ทธ๋ณด๋‹ค ์ธ๋ฑ์Šค๊ฐ€ ์ž‘์€ ๊ฒƒ ์„ ํƒ
		$(".third > li:lt(2)").css("color", "blue");
		//class๊ฐ€ third์ธ liํƒœ๊ทธ์˜ 2๋ฒˆ์งธํƒœ๊ทธ๊ฐ€ ์•„๋‹Œ ๊ฒƒ๋“ค ์„ ํƒ
		$(".third > li:not(:eq(2))").css("font-size", "20px");

	});
</script>
</head>
<body>
	<p>1์›”</p>
	<p>2์›”</p>
	<p>3์›”</p>
	<p>4์›”</p>
	<p>5์›”</p>
	<p>6์›”</p>
	<p>7์›”</p>
	<p>8์›”</p>
	<p>9์›”</p>
	<p>10์›”</p>
	<p>11์›”</p>
	<p>12์›”</p>
	<br/><br/>
	
	<div>
		<ul class="first">
			<li>์‚ฌ๊ณผ</li>
			<li>๋ฐ”๋‚˜๋‚˜</li>
			<li>๋”ธ๊ธฐ</li>
			<li>๋ฉ”๋ก </li>
		</ul>
	</div>
	
	<div>
		<ul class="second">
			<li>์งœ์žฅ๋ฉด</li>
			<li>์šฐ๋™</li>
			<li>์งฌ๋ฝ•</li>
			<li>ํƒ•์ˆ˜์œก</li>
		</ul>
	</div>
	
	<div>
		<ul class="third">
			<li>์ปคํ”ผ</li>
			<li>์ฝœ๋ผ</li>
			<li>์‚ฌ์ด๋‹ค</li>
			<li>ํ™˜ํƒ€</li>
			<li>์šฐ์œ </li>
		</ul>
	</div>
</body>
</html>

 

11_BasicFilter.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - ๊ธฐ๋ณธ ํ•„ํ„ฐ ์„ ํƒ์ž</title>
<style type="text/css">
	.theme_hot {
		width:300px;
		background:#aaeeaa;
		color:#404040
	}
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$('#content a:first').click(function(){
			$('.theme_1 > li:eq(2)').addClass('theme_hot');
			$('.theme_2 > li:eq(0)').addClass('theme_hot');
			$('.theme_3 > li:eq(1)').addClass('theme_hot');
		});
		
		$('#content a:last').click(function(){
			$('.theme_1 > li:eq(2)').removeClass('theme_hot');
			$('.theme_2 > li:eq(0)').removeClass('theme_hot');
			$('.theme_3 > li:eq(1)').removeClass('theme_hot');
		})
	});
</script>
</head>
<body>
	<div>
		<ul id="content">
			<li><a href="#">์ธ๊ธฐ์ถ•์ œ</a></li>
			<li><a href="#">์ค€๋น„์ค‘์ธ ์ถ•์ œ</a></li>
		</ul>
	</div>
	
	<h2>์ฃผ์š”์ง€์—ญ๋ณ„ ์ถ•์ œ ์ •๋ณด์•ˆ๋‚ด</h2>
	<ul class="theme">
		<li>
			<b>์„œ์šธ๋ฐ ๊ทผ๊ต์ง€์—ญ</b>
			<ul class="theme_1">
				<li>๊ฐ•ํ™” ๊ณ ๋ ค์‚ฐ ์ง„๋‹ฌ๋ž˜ ์ถ•์ œ</li>
				<li>๊ณ ์–‘ ๊ตญ์ œ ๊ฝƒ ๋ฐ•๋žŒํšŒ</li>
				<li>์—๋ฒ„๋žœ๋“œ ํŠค๋ฆฝ์ถ•์ œ</li>
				<li>์—ฐ์ฒœ ์ „๊ณก๋ฆฌ ๊ตฌ์„๊ธฐ ์ถ•์ œ</li>
			</ul>
		</li>
		
		<li>
			<b>๊ฐ•์›๋„</b>
			<ul class="theme_2">
				<li>๊น€์œ ์ • ๋ฌธํ•™์ œ</li>
				<li>์ถ˜์ฒœ ๋งˆ์ž„ ์ถ•์ œ</li>
			</ul>
		</li>
		
		<li>
			<b>์ถฉ์ฒญ๋„</b>
			<ul class="theme_3">
				<li>๋ณด๋ น๋จธ๋“œ์ถ•์ œ</li>
				<li>ํƒœ์•ˆ ํŠœ์šธ๋ฆฝ ๊ฝƒ ์ถ•์ œ</li>
			</ul>
		</li>
		
		<li>
			<b>์ „๋ผ๋„</b>
			<ul class="theme_4">
				<li>์ง„๋„ ์‹ ๋น„์˜ ๋ฐ”๋‹ท๊ธธ ์ถ•์ œ</li>
				<li>์ˆœ์ฒœ๋งŒ ๊ตญ์ œ์ •์› ๋ฐ•๋žŒํšŒ</li>
				<li>ํ•จํ‰ ๋‚˜๋น„ ์ถ•์ œ</li>
			</ul>
		</li>
	</ul>
</body>
</html>

 

13_VisibilityFilter.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery - ๋ณด์ž„ ํ•„ํ„ฐ ์„ ํƒ์ž</title>
<style type="text/css">
	  div {
	    width: 50px;
	    height: 40px;
	    margin: 5px;
	    border: 3px outset green;
	    float: left;
	  }
	
	  .div_hidden {
	    display: none;
	  }
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	//๋ณด์ด์ง€ ์•Š์„ ๋•Œ display:none, type(hidden), width/height(0)
	$(document).ready(function(){
		$('#btn').click(function(){
			//1์ดˆ๋™์•ˆ ์ฒœ์ฒœํžˆ ๋ณด์—ฌ์ง€๊ฒŒ ์‹œ๊ฐ„์„ ์„ค์ •
			$('div:hidden').show(1000);
		});
		
		//๋ณด์—ฌ์ง€๋Š” ๊ฑด visible
		$('div:visible').click(function(){
			$(this).css("background", "yellow");
		});
	});
</script>
</head>
<body>
	<input type="button" id="btn" value="๋ฒ„ํŠผํด๋ฆญ"/>
	<div></div>
	<div></div>
	<div class="div_hidden"></div>
	<div></div>
	<div style="display:none;"></div>
</body>
</html>

 

14_ContentFilter.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - ๋‚ด์šฉ ํ•„ํ„ฐ ์„ ํƒ์ž</title>
<style type="text/css">
	body{
		font-size:12px;
	}
</style>
</head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	$(function(){		//var obj = new Jquery(document); obj.ready(abc);
		$('li:contains("ap")').css({ 	//(๊ด„ํ˜ธ)์˜ ํ…์ŠคํŠธ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด ์š”์†Œ
			backgroundColor: '#9AB92E',
			border:"1px solid #05184D"
		});
		
		$('#sub:has(li)').css({	//์š”์†Œ ๋‚ด๋ถ€์— ์ฐพ๊ณ  ์‹ถ์€ ํƒœ๊ทธ๋ฅผ ํ›„์† ์š”์†Œ๊นŒ์ง€ ์ฐพ์•„์ค€๋‹ค.
			backgroundColor: "#9AB92E",
			border:"1px solid #05184D"
		});
		
		//๋นˆ๋ถ€๋ถ„
		$('li:empty').text("Hello");
		//์ฑ„์›Œ์ง„๋ถ€๋ถ„
		$('li:parent').css("color", "red");
	});
</script>
<body>
	<div>
		<ul>
			<li>apple</li>
			<li>banana</li>
			<li>grape</li>
			<li></li>
		</ul>
		
		<ul id="sub">
			<li>lion</li>
			<li>tiger</li>
			<li>fox</li>
			<li></li>
		</ul>
	</div>
</body>
</html>

 

15_Attribute.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - ์†์„ฑ ์„ ํƒ์ž</title>
<style type="text/css">
	body{
		font-size:12px;
	}
	.spotlight{
		background-color:#ff0;
	}
	.redtext{
		color:#f00;
	}
	.bluetext{
		color:#00f;
	}
	.largetext{
		font-size:30px;
	}
	.italctext{
		font-style:italic;
	}
	.box{
		border:3px solid #f00;
	}
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	$(function(){	
		$('span[id]').addClass('spotlight');
		$('span[id=simpletext1]').addClass('redtext');
		$('span[id!=simpletext1]').addClass('bluetext');
		
		$('span[id^="complex"]').addClass('largetext'); 	//์‹œ์ž‘๋ถ€๋ถ„
		$('span[id$="text1"]').addClass('italctext');		//๋๋‚˜๋Š”๋ถ€๋ถ„
		$('span[id*="text"]').addClass('box');				//ํฌํ•จ
	});
</script>
</head>
<body>
	<span id="simpletext1">SIMPLETEXT1</span>
	<span class="simpletext2">SIMPLETEXT2</span>
	<span id="complextext1">COMPLEXTEXT1</span>
	<span id="complextext2">COMPLEXTEXT2</span>
	<span id="complex">COMPLEX</span>
</body>
</html>

 

16_Validity.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - ์†์„ฑ ์„ ํƒ์ž</title>
</head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	$(function(){	
		$('input[type="text"]').val('๊ฐ’์„ ์ž…๋ ฅํ•˜์„ธ์š”');
		
		$('input[type="text"]').click(function(){
			$(this).val("");
		});
		
		$('input[id="btn"]').click(function(){
			var arr = new Array();
			var name = $('input[name="name"]').val();
			var id = $('input[name="id"]').val();
			
			var idType = $('input[name="id"]').attr('type');
			$('input[name="id"]').attr("style", "color:red");
			$('input[name="id"]').attr("size", "50");
			
			arr.push(name);
			arr.push(id);
			arr.push(idType);
			alert(arr);			
		});
		
		var div = $('div:eq(0)').text();
		alert(div);
		
		$('div:eq(1)').text('์•ˆ๋…•ํ•˜์„ธ์š”');
	});
</script>
<body>
	<div>hi</div>
	<div></div>
	<div></div>
	<form action="#" method="post">
		<label>์ด๋ฆ„</label>
		<input type="text" id="name" name="name"/>
		<br/><br/>
		
		<fieldset>
			<legend>ํ•„์ˆ˜์ž…๋ ฅ์‚ฌํ•ญ</legend>
			<label>์•„์ด๋””</label>
			<input type="text" id="id" name="id"/>
			<label>๋น„๋ฐ€๋ฒˆํ˜ธ</label>
			<input type="password" id="password" name="password"/>
		</fieldset>
		<br/><br/>
		
		<label>
			E-MAIL 
			<input type="text"  id="email" name="email" />
		</label>
		<br/><br/>
		
		<input type="button"  id="btn" value="์ž…๋ ฅ"/>
	</form>
</body>
</html>

 

17_Validity.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - ์†์„ฑ ์„ ํƒ์ž</title>
</head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
	$(function(){	
		$('#btn').click(function(){
			if($('#id').val()==""){
				alert('์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.');
				$('#id').focus();
				return;
			}
			
			if($('#name').val()==""){
				alert('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.');
				$('#name').focus();
				return;
			}
			
			var str = $('input[name="gender"]:checked').val();
			if(str==null){
				alert('์„ฑ๋ณ„์„ ์ฒดํฌํ•˜์„ธ์š”.');
				return;
			}
			
			var value ="";
			$('input[name="hobby"]:checked').each(function(index, element){
				value+=$(this).val() + ",";
				alert(value);
			});
			
			if(value==""){
				alert('์ทจ๋ฏธ๋ฅผ ํ•œ๊ฐœ์ด์ƒ ์ฒดํฌํ•˜์„ธ์š”.');
				return;
			}else{
				$('input[name="hobbyResult"]').val(value);
			}
			
			$('form').submit();
		});
	});
</script>	
<body>
	<form action="#" method="get">
		<label>์•„์ด๋””</label>
		<input type="text" id="id" name="id"/>
		<br/><br/>
		
		<label>์ด๋ฆ„</label>
		<input type="text" id="name" name="name"/>
		<br/><br/>
		
		<label>๋‚จ</label>
		<input type="radio" name="gender" value="๋‚จ์ž" />
		<label>์—ฌ</label>
		<input type="radio" name="gender" value="์—ฌ์ž" />
		<br/><br/>
	
		<label>์Œ์•…</label>
		<input type="checkbox" name="hobby" value="์Œ์•…"/>
		
		<label>์š”๊ฐ€</label>
		<input type="checkbox" name="hobby" value="์š”๊ฐ€"/>
		
		<label>๋…์„œ</label>
		<input type="checkbox" name="hobby" value="๋…์„œ"/>
		<input type="hidden" name="hobbyResult"/>
		<br/><br/>
		
		<input type="button" id="btn" value="์ „์†ก"/>
		<input type="reset"  value="์ทจ์†Œ"/>
	</form>
</body>
</html>
๋ฐ˜์‘ํ˜•
Comments