1. Description
jQuery(":parent") |
자식 Element가 한 개 이상인 부모 Element를 찾는다. |
2. Example
#1버튼을 클릭하면 자식 Element가 존재하는 <td>태그를 찾아서 배경색을 변경한다. 버튼을 클릭할 때마다 스타일이 토글된다.
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/parent</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 colspan="4" style="height:20px;"></td>
</tr>
<tr>
<td colspan="4" style="height:20px;"></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:parent")</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:parent");
btnFind.click(function() {
if(!elements.is("[style]")) {
elements.css("background", "#76E3AF");
} 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", " "));
</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: 35px;
}
[name|="_1q74-example"] button:active
,[name="_1q74-source"] button:active {
border: 2px inset;
min-height: 35px;
}
</style>
</body>
</html>
4. File
[javascript][jquery][selector]parent.html
0.00MB
5. See also
pseudo-class, useversal-selector, jquery-extensions
【jQuery/Selector #20】:empty Selector
1. Description 자식Element를 가지고 있지 않은 모든 Element들을 찾는다. 이것은 :parent와는 정반대의 Selector이다. 자식에는 text노드도 포함된다(즉, 태그 내용에 문자열이 들어가 있으면 자식Element라고
1q74.tistory.com
6. Reference
https://api.jquery.com/parent-selector/
'JavaScript > jQuery' 카테고리의 다른 글
【jQuery/Selector #55】:radio Selector (0) | 2023.03.15 |
---|---|
【jQuery/Selector #54】:password Selector (1) | 2023.03.15 |
【jQuery/Selector #52】:only-of-type Selector (0) | 2023.03.15 |
【jQuery/Selector #51】:only-child Selector (0) | 2023.03.15 |
【jQuery/Selector #50】:odd Selector (0) | 2023.03.15 |