순회(탐색) 메소드 - Ancestors


선택한 요소의 상위 요소를 찾는 메소드이다.


parent() : 해당 요소의 바로 상위 요소


parents() : 해당 요소의 상위 요소들 전부


parentsUntil() : 해당 요소의 상위 요소부터 괄호 안에 입력된 요소까지




<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Insert title here</title>
<style>
	.wrap * {
		border:1px solid lightgray;
		display:block;
		padding:5px;
		margin:15px;
		color:gray;
	}
</style>
</head>
<body>
	<h1>순회(탐색) 메소드</h1>
	<h3>Ancestors 메소드</h3>
	<p>선택된 요소의 상위 요소들을 선택할 수 있는 메소드</p>
	
	<div class="wrap">
		<div style="width:500px;">div (grand-grandParent)
			<ul>ul (grandParent)
				<li>li (directParent)
					<span>span</span>
				</li>
			</ul>
		</div>
		<div style="width:500px;">div (grandParent)
			<p>p (directParent)
				<span>span</span>
			</p>
		</div>
	</div>
	
	<script>
		$(function(){
			//선택된 요소의 바로 상위 요소만 리턴
			$("span").parent().css({"color":"red", "border":"2px solid red"});
			
			//선택된 요소의 모든 상위 요소 리턴(매개변수가 있으면 매개변수와 일치하는 부모만 리턴)
			$("span").parents("ul").css({"color":"blue", "border":"2px solid blue"});
			
			$("span").parentsUntil("div").css({"color":"yellowgreen", "border":"2px solid yellow"});
		});
	</script>
</body>
</html>





순회(탐색) 메소드 - Descendants


선택한 요소의 하위 요소를 찾는 메소드이다.


children() : 바로 다음의 하위 객체


find() : 하위에서 괄호안의 객체를 찾는다




<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Insert title here</title>
<style>
	.wrap * {
		border:1px solid lightgray;
		display:block;
		padding:5px;
		margin:15px;
		color:gray;
	}
</style>
</head>
<body>
	<h1>순회(탐색) 메소드</h1>
	<h3>descendants 메소드</h3>
	<p>선택된 요소의 하위 요소를 선택할 수 있는 메소드</p>
	
	<div class="wrap">
		<div style="width:500px;">div (grand-grandParent)
			<ul>ul (grandParent)
				<li>li (directParent)
					<span>span</span>
				</li>
			</ul>
		</div>
		<div style="width:500px;">div (grandParent)
			<p>p (directParent)
				<span>span</span>
			</p>
		</div>
	</div>
	<script>
		$(function(){
			// 선택된 요소의 모든 자손(바로 다음 레벨) 객체를 리턴하고
			//매개변수가 있는 경우 일치하는 자손 객체를 리턴한다.
			//$(".wrap").children().css({"color":"red", "border":"2px solid red"});
			
			// 자식의 자식, 손자
			//$(".wrap").children().children().css({"color":"red", "border":"2px solid red"});
			
			// 손자 중 ul 태그만
			$(".wrap").children().children("ul").css({"color":"red", "border":"2px solid red"});
		
			$(".wrap").find("span").css({"color":"yellowgreen", "border":"2px solid yellowgreen"});
		});
	</script>
</body>
</html>







순회(탐색) 메소드 - sideway


siblings() : 선택 요소와 같은 레벨에 있는 요소를 리턴, 인자가 있다면 일치하는 요소만 리턴


next() : 선택 요소의 다음 요소를 리턴


nextAll() : 선택 요소의 다음 요소들을 전부 리턴


nextUntil() : 선택 요소 다음부터 괄호에 인자로 받은 태그명 까지 리턴


prev() : 선택 요소의 전 요소를 리턴


prevAll() : 선택 요소의 전 요소들을 전부 리턴


prevUntil() : 선택 요소 전부터 괄호에 인자로 받은 태그명 까지 리턴




<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
	.wrap, .wrap * {
		display:block;
		border:1px solid lightgray;
		color:lightgray;
		padding:5px;
		margin:15px;
	}
</style>
</head>
<body>
	<h1>순회(탐색) 메소드</h1>
	<h3>sideway 메소드</h3>
	<div class="wrap">div (parent)
		<p>p</p>
		<span>span</span>
		<h2>h2</h2>
		<h3>h3</h3>
		<p>p</p>
	</div>
	<script type="text/javascript">
		$(function() {
			// 선택된 요소와 같은 레벨에 있는 요소 리턴
			// 인자가 있는 경우 인자와 일치하는 요소 리턴
			$("h2").siblings().css({"color":"red", "border":"2px solid red"});
			$("h2").siblings("p").css({"color":"orange", "border":"2px solid orange"});
			
			// 선택된 요소의 값은 레벨의 형제들 중 선택된 요소의 다음 한 개의 요소를 리턴한다.
			$("h2").next().css({"color":"blue", "border":"2px solid blue"});
			
			// 선택된 요소의 같은 레벨 형제 중 선택된 요소 다음 모든 요소를 리턴한다.
			$("h2").nextAll().css({"font-size" : "2em"});
			
			// 선택된 요소부터 같은 레벨의 인자 범위까지의 모든 요소를 리턴
			$("h2").nextUntil("p").css("border", "dashed");
			
			// 선택된 요소부터 같은 레벨의 형제 중 요소 이전의 한 개 요소를 리턴한다.
			$("h2").prev().css("background", "yellow");
			
			// 선택된 요소부터 같은 레벨의 형제 요소 중 이전의 모든 요소를 리턴한다.
			$("h2").prevAll().css({"background":"cyan", "color":"red"});
			
			// 선택된 요소의 같은 레벨의 형제 요소들 중
			// 인자로 전달된 요소 이전의 요소를 리턴
			$("h2").prevUntil("p").css("border", "dotted");
		})
	</script>
</body>
</html>






테이블에서 전에 썻던 even odd first last 와 같은 방법으로는 한계가 있다.


고로 아래의 방법을 확인해보자.


eq() : 괄호 안의 번호에 해당하는 순서 태그를 지칭


nth-child() : 괄호 안의 식에 해당하는 순서 태그들을 지칭


gt() : 괄호 안의 번호에 초과하는 태그들을 지칭


lt() : 괄호 안의 번호에 미만인 태그들을 지칭


contains() : 특정 문자열을 포함


has() : 특정 태그를 가지고 있는


not() : 해당 선택자와 일치하지 않는 객체



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
	<h1>함수 형태의 필터 선택자</h1>
	<h3>:nth-child() 필터선택자</h3>
	
	<table border="1">
		<tr>
			<th>이름</th>
			<th>혈액형</th>
			<th>지역</th>
		</tr>
		<tr>
			<td>김김김</td>
			<td>C형</td>
			<td><a>달</a></td>
		</tr>
		<tr>
			<td>님님님</td>
			<td>D형</td>
			<td>태양</td>
		</tr>
		<tr>
			<td>딤딤딤</td>
			<td>E형</td>
			<td>은하계</td>
		</tr>
		<tr>
			<td>림림림</td>
			<td>C형</td>
			<td>지구</td>
		</tr>
		<tr>
			<td>밈밈밈</td>
			<td>D형</td>
			<td>소행성</td>
		</tr>
		<tr>
			<td>빔빔빔</td>
			<td>C형</td>
			<td>집</td>
		</tr>
		<tr>
			<td colspan="2">총원</td>
			<td>6</td>
		</tr>
	</table>
	
	<script type="text/javascript">
		$(function() {
			// n번째에 위치하는 객체 선택
			$("tr:eq(0)").css({"background":"black", "color":"white"});
			
			// 수열번 째 위치하는 태그 선택(순서로 적용)
			$("tr:nth-child(2n)").css("background","yellowgreen");
			$("tr:nth-child(3n)").css("background", "orange");
			
			// n번째를 초과하는 객체 선택(인덱스 적용)
			$("tr:gt(6)").css("background", "lightgray");
			
			// n번째 미만에 위치하는 객체 선택
			$("tr:lt(7)").css("font-size", "1.5em");
			// em은 부모 객체의 font size를 의미
			
			// 특정 문자열을 포함하는 객체  선택
			$("tr:contains('A형')").css("color", "red");
			
			// 특정 태그를 가지고 있는 객체 선택
			$("td:has(a)").css("color", "white");
			
			// 선택자와 일치하지 않는 객체 선택
			$("td:not(:contains('김김김'))").css("color", "lightgray");
			
			
		});
	</script>
</body>
</html>



앞의 선택자가 태그 이름과 선택자의 조합이었다면


필터링은 메소드 형태로 조건을 괄호로 받는다.


.filter(:odd) 와 같이..



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
	<h1>필터링 메소드</h1>
	<h3>filtering 메소드의 사용법</h3>
	<p>필터 선택자를 메소드 형태로 제공한다.</p>
	
	<h4 class="test">test-1</h4>
	<h4 class="test">test-2</h4>
	<h4 class="test">test-3</h4>
	<h4 class="test">test-4</h4>
	<h4>test-5</h4>
	<h4 class="test">test-6</h4>
	
	<script type="text/javascript">
		$(function() {
			$("h4:even").css("background", "black").css("color", "white");
			
			// filter() 메소드 사용
			$("h4").filter(":even").css("background", "orangered").css("color", "white");
			
			// 함수를 매개변수로 하는 filter() 메소드 사용
			$("h4").filter(function(index) {
				return index % 2 == 1;
			}).css("background", "yellowgreen").css("color", "black");
			
			// 선택된 요소 중 제일 처음에 있는 요소 리턴
			$("h4").first().css("font-size", "1.5em");
			
			// 선택된 요소 중 제일 마지막에 있는 요소 리턴
			$("h4").last().text("마지막 요소");
			
			// 인덱스 번호와 일치하는 요소만 리턴
			$("h4").eq(3).text("eq()로 선택됨");
			
			// 인자값과 일치하지 않는 요소만 리턴
			$("h4").not(".test").css({"background":"yellow", "color":"black"});
		});
	</script>
</body>
</html>



input 태그의 타입 별  선택 방법


$("input:타입").~~~과 같이 쓴다.


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>
	<h1>입력 양식 필터 선택자</h1>
	<p>input 태그의 type속성에 따라 문서 객체 선택 가능</p>
	<label>텍스트 상자 : </label><input type="text"><br>
	<label>버튼 : </label><input type="button"><br>
	<label>체크박스 : </label><input type="checkbox"><br>
	<label>파일 : </label><input type="file"><br>
	<label>이미지 : </label><input type="image" src="../image/flower1.PNG"><br>
	<label>패스워드 : </label><input type="password"><br>
	<label>라디오버튼 : </label><input type="radio"><br>
	<label>리셋버튼 : </label><input type="reset"><br>
	<label>submit 버튼 : </label><input type="submit"><br>
	<script type="text/javascript">
		$(function name() {
			$("input:text").css("background", "red");
			
			$("input:button").attr("value", "왕버튼").css({"width":"200px", "height":"100px"});
			
			$("input:checkbox").attr("checked", true).css({"width":"50px", "height":"50px"});
			
			$("input:file").css("background", "yellowgreen");
			
			$("input:image").mouseenter(function() {
				$(this).attr("src", "../image/river1.PNG");
				// $(this)는 자신을 의미
			})
			
			$("input:image").mouseout(function() {
				$(this).attr("src", "../image/flower1.PNG");
			})
			
			
			$("input:radio").attr("checked", true);
			
			$("input:reset").css({"background":"blue", "color":"white"});
			
			$("input:submit").css({"background":"purple", "color":"white"});
		});
	</script>

	
</body>
</html>




input 태그에 쓰인 value의 값을 반환받는 방법은


JQuery로 원하는 태그를 불러오고 .val() 키워드를 사용


체크박스의 체크 여부 확인의 경우는 .prop("checked"); 이다.



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>
	<h1>입력 양식 필터 선택자</h1>
	<h3>checkbox의 상태에 대한 선택자</h3>
	
	<input type="checkbox" name="hobby" value="game" id="game">
	<label for="game">게임</label>
	<input type="checkbox" name="hobby" value="movie" id="movie">
	<label for="movie">영화</label>
	<input type="checkbox" name="hobby" value="music" id="music">
	<label for="music">음악</label>
	
	<script type="text/javascript">
		// form 태그에서 submit으로 전송하면 ?hobby=game&hobby=movie 형태로 넘어간다.
		// 이 값은 배열 형태로 저장되게 된다.
		$("input:checkbox").change(checkedChange);
		
		function checkedChange() {
			// console.log($(this).prop("checked"));
			
			if($(this).prop("checked")){
				$(this).css({"width":"50px", "height":"50px"});
			}else{
				$(this).css({"width":"15px", "height":"15px"});
			}
		}
	</script>
	
	<hr>
	
	<h3>select > option 태그의 상태에 대한 선택자</h3>
	<select name="national" id="national">
		<option value="한국" selected>한국</option>
		<option value="미국">미국</option>
		<option value="일본">일본</option>
		<option value="중국">중국</option>
	</select>
	
	<label>선택한 나라 : </label><input type="text" id="result">
	
	<script type="text/javascript">
		$(selectChange);
		
		$("#national").change(selectChange);
	
		function selectChange() {
			var value = $("option:selected").val();
			
			$("#result").val(value);
		}
	</script>
	
	<hr>
	
	<h3>input 상태에 대한 선택자</h3>
	<label>활성화 텍스트상자 : </label><input type="text"><br>
	<label>비 활성화 텍스트 상자 : </label><input type="text" disabled><br>
	<label>활성화 버튼 : </label><input type="button" value="활성화"><br>
	<label>비 활성화 버튼 : </label><input type="button" value="비활성화" disabled><br>
	
	<script type="text/javascript">
		$(function() {
			$("input:enabled").css("background", "yellowgreen");
			$("input:disabled").css("background", "red");
		})	
	</script>
	
	<hr>
	
	<h3>필터 선택자</h3>
	
	<table border="1">
		<tr>
			<th>이름</th>
			<th>혈액형</th>
			<th>지역</th>
		</tr>
		<tr>
			<td>김김김</td>
			<td>C형</td>
			<td>달</td>
		</tr>
		<tr>
			<td>님님님</td>
			<td>D형</td>
			<td>태양</td>
		</tr>
		<tr>
			<td>딤딤딤</td>
			<td>E형</td>
			<td>은하계</td>
		</tr>
		<tr>
			<td>림림림</td>
			<td>C형</td>
			<td>지구</td>
		</tr>
		<tr>
			<td>밈밈밈</td>
			<td>D형</td>
			<td>소행성</td>
		</tr>
		<tr>
			<td>빔빔빔</td>
			<td>C형</td>
			<td>집</td>
		</tr>
		<tr>
			<td colspan="2">총원</td>
			<td>6</td>
		</tr>
	</table>
	
	<script type="text/javascript">
		$(function() {
			// odd : 홀수, 0번째 인덱스 부터 계산
			$("tr:odd").css("background", "orange");
			// even : 짝수, 0번째 인덱스 포함
			$("tr:even").css("background", "lightgray");
			// first : 첫번째
			$("tr:first").css("background", "black").css("color", "white");
			// last : 마지막
			$("tr:last").css("background", "orangered").css("color", "white");
		});
	</script>
</body>
</html>






+ Recent posts