1. Description
3.4버전부터 Deprecated되었다.
매치되는 첫 번째 Element를 찾는다.
:first는 :eq(0), :lt(1)과 동일하며 오직 한 개의 Element와 매치되지만, :first-child는 여러 개의 Element와 매치될 수 있다.
2. Example
#1버튼을 클릭하면 <td>태그들 중 첫 번째 요소를 찾아서 border스타일을 변경한다. 버튼 클릭할 때마다 해당 스타일이 토글된다.
Amazon | ||
Tesla | Netflex |
[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/first</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 name="bigtech" border="1">
<tr>
<td>Google</td>
<td>Amazon</td>
<td>Facebook</td>
</tr>
<tr>
<td>Tesla</td>
<td>Twitter</td>
<td>Netflex</td>
</tr>
</table>
</div>
<div name="_1q74-example-bottom">
<br/>
<br/>
<button name="btn-find">#1. $("[name='_1q74-example-1'] td:first")</button>
</div>
</div>
<hr/>
<h5>[HTML Code]</h5>
<div name="_1q74-source">
</div>
<h5>[//HTML Code]</h5>
<script>
const btnFind = $("[name='btn-find']");
const firstTags = $("[name='bigtech'] td:first");
btnFind.click(function() {
firstTags.each(function(index, el) {
const element = $(el);
if(!element.is("[style]")) {
element.css("border", "2px dashed blue");
} else {
element.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", " "));
</script>
<style>
[name="_1q74-example-root"] {
min-height: 150px;
}
[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
5. See also
pseudo-class, useversal-selector, jquery-extensions
6. Reference
https://api.jquery.com/first-selector/
'JavaScript > jQuery' 카테고리의 다른 글
【jQuery/Selector #28】:gt() Selector (0) | 2023.02.24 |
---|---|
【jQuery/Selector #27】:focus Selector (0) | 2023.02.24 |
【jQuery/Selector #25】:first-of-type Selector (0) | 2023.02.24 |
【jQuery/Selector #24】:first-child Selector (0) | 2023.02.23 |
【jQuery/Extensions】jQuery Extensions (0) | 2023.02.23 |