1. Description
jQuery(":has(selector)") : selector를 적어도 하나 이상 가지고 있는 Element들을 찾는다.
2. Example
#1버튼을 클릭하면 <table>의 <td>안에 <b>태그를 가지고 있는 Element들을 찾아서 배경색을 변경한다. 버튼을 클릭할 때마다 해당 스타일이 토글된다.
Amazon | ||
Tesla | Netflex | |
My Big Company | My Great Company | My Awesom Company |
[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/gt</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><b>Google</b></td>
<td>Amazon</td>
<td>Facebook</td>
</tr>
<tr>
<td>Tesla</td>
<td>Twitter</td>
<td>Netflex</td>
</tr>
<tr>
<td>My Big Company</td>
<td>My Great Company</td>
<td><b>My Awesom Company</b></td>
</tr>
</table>
</div>
<div name="_1q74-example-bottom">
<br/>
<button name="btn-find">#1. $("[name='_1q74-example-1'] td:has(b))</button>
</div>
</div>
<hr/>
<h5>[HTML Code]</h5>
<div name="_1q74-source">
</div>
<h5>[//HTML Code]</h5>
<script>
var elements = $("[name='_1q74-example-1'] td:has(b)");
var btnFind = $("[name='btn-find']");
btnFind.click(function() {
if(!elements.is("[style]")) {
elements.css("background", "#009900");
} 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: 150px;
}
[name="_1q74-example-1"] {
min-height: 80px;
}
[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/has-selector/
'JavaScript > jQuery' 카테고리의 다른 글
【jQuery/Selector #32】:hidden Selector (0) | 2023.03.05 |
---|---|
【jQuery/Selector #31】:header Selector (0) | 2023.03.04 |
【jQuery/Selector #29】Has Attribute Selector [name] (0) | 2023.03.04 |
【jQuery/Selector #28】:gt() Selector (0) | 2023.02.24 |
【jQuery/Selector #27】:focus Selector (0) | 2023.02.24 |