JavaScript/jQuery

【jQuery/Selector #50】:odd Selector

1Q74 2023. 3. 15. 02:04

1. Description

jQuery(":odd") : (0부터 시작하는 인덱스 기준)홀수번째 Element를 찾는다.
3.4버전부터 Deprecated되었고, .odd()는 3.5.0이상의 버전부터 사용할 수 있다.
$(":odd")처럼 사용하면 querySelectorAll()에서 제공하는 속도향상의 이점을 기대할 수 없으므로, 우선 CSS Selector를 사용한 후 .filter(":odd")을 사용하는 것이 좋다고 한다.


※ 자세한 내용은 「6. Reference」를 참고하세요.

2. Example

#1버튼 클릭하면 <td>태그들 중 홀수번째 Element를 찾아서 배경색을 변경한다. 버튼을 클릭할 때마다 스타일이 토글된다.


[1q74.tistory.com] javascript/jquery/selector/odd
child#1-1 child#1-2 child#1-3 child#1-3
child#2-1 child#2-2 child#2-3 child#2-4
child#3-1 child#3-2 child#3-3 child#3-4


[HTML Code]
[//HTML Code]

3. Code

더보기
<!-- ---------------------------------------------------------
  --
  -- Author: 1q74.tistory.com
  --
  --------------------------------------------------------- -->
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>[1q74.tistory.com] javascript/jquery/selector/odd</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
</head>
<body>

	<div name="_1q74-example-root">
		<div name="_1q74-example-1">
			<table border="1">
				<tr>
					<td>child#1-1</td>
					<td>child#1-2</td>
					<td>child#1-3</td>
					<td>child#1-3</td>
				</tr>
				<tr>
					<td>child#2-1</td>
					<td>child#2-2</td>
					<td>child#2-3</td>
					<td>child#2-4</td>
				</tr>
				<tr>
					<td>child#3-1</td>
					<td>child#3-2</td>
					<td>child#3-3</td>
					<td>child#3-4</td>
				</tr>
			</table>
		</div>

		<div name="_1q74-example-bottom">
			<br/>
			<button name="btn-find">#1. $("[name='_1q74-example-1'] td:odd")</button>
		</div>
	</div>

	<hr/>
	<h5>[HTML Code]</h5>
	<div name="_1q74-source">
	</div>
	<h5>[//HTML Code]</h5>

<script>
var btnContainer = $("[name='_1q74-example-bottom']");
var btnFind = btnContainer.find("[name='btn-find']");

var elements = $("[name='_1q74-example-1'] td:odd");

btnFind.click(function() {
	if(!elements.is("[style]")) {
		elements.css("background", "#D1DE4B");
	} else {
		elements.removeAttr("style");
	}
});

// ------------------------------------------------------
// 소스출력
// ------------------------------------------------------
var textSource = $("[name='_1q74-source']");
var exampleHtml = $("[name='_1q74-example-root']").html();
var specialCharLines = textSource.text(exampleHtml).html().split("\n");

textSource.html(specialCharLines.join("<br/>").replaceAll("\t", "&nbsp;&nbsp;"));
</script>
<style>
[name="_1q74-example-root"] {
	min-height: 170px;
}

[name="_1q74-example-1"] {
	min-height: 140px;
}

[name="_1q74-example-root"] [type="image"]{
	width: 100px;
}

[name="_1q74-example-1"] td {
	min-width: 100px;
	min-height: 30px;
}

[name|="_1q74-example"] button
,[name="_1q74-source"] button {
	background: lightgray;
	border: 5px outset;
	min-height: 30px;
}

[name|="_1q74-example"] button:active
,[name="_1q74-source"] button:active {
	border: 2px inset;
	min-height: 30px;
}
</style>

</body>
</html>

4. File

[javascript][jquery][selector]odd.html
0.00MB


5. See also

pseudo-class, useversal-selector, jquery-extensions

 

:even

 

【jQuery/Selector #22】:even Selector

1. Description 3.4버전부터 Deprecated되었고, .even()은 3.5.0이상의 버전부터 사용할 수 있다. (0부터 시작하는 인덱스 기준)짝수번째 Element를 찾는다. $(":even")처럼 사용하면 querySelectorAll()에서 제공하는

1q74.tistory.com


6. Reference

https://api.jquery.com/odd-selector/