1. Description
jQuery("#id") : 해당 id로 설정된 Element를 찾는다.
※ id는 한번만 사용되어야 하지만, 똑같은 id가 중복되어 사용되었을 경우 첫 번째 id와 매칭된다.
2. Example
#1버튼을 클릭하면 id가 mygreatcompany로 설정되어 있는 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/id</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 id="tesla">Tesla</td>
<td>Twitter</td>
<td>Netflex</td>
</tr>
<tr>
<td>My Big Company</td>
<td id="mygreatcompany">My Great Company</td>
<td>My Awesom Company</td>
</tr>
</table>
</div>
<div name="_1q74-example-bottom">
<br/>
<button name="btn-find">#1. $("#mygreatcompany")</button>
</div>
</div>
<hr/>
<h5>[HTML Code]</h5>
<div name="_1q74-source">
</div>
<h5>[//HTML Code]</h5>
<script>
var element = $("#mygreatcompany");
var btnFind = $("[name='btn-find']");
btnFind.click(function() {
if(!element.is("[style]")) {
element.css("background", "DarkSlateGray");
} 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-1"] {
min-height: 80px;
}
[name="_1q74-example-1"] td {
min-width: 100px;
}
[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. Reference
https://api.jquery.com/id-selector/
'JavaScript > jQuery' 카테고리의 다른 글
【jQuery/Selector #35】:input Selector (0) | 2023.03.05 |
---|---|
【jQuery/Selector #34】:image Selector (0) | 2023.03.05 |
【jQuery/Selector #32】:hidden Selector (0) | 2023.03.05 |
【jQuery/Selector #31】:header Selector (0) | 2023.03.04 |
【jQuery/Selector #30】:has() Selector (0) | 2023.03.04 |