Annotation
jQuery Selecter (선택자) 본문
jQuery를 사용함에 있어서 가장 중요하고 신경써서 코딩해야할 선택자!
항상 잘사용하는것만 쓰다보니 가끔 사용하는 선택자는 잘 생각이 나지 않아서 첨부합니다.
출처는 w3schools 입니다.
Selector | Example | Selects |
---|---|---|
* | $("*") | All elements |
#id | $("#lastname") | The element with id="lastname" |
.class | $(".intro") | All elements with class="intro" |
.class,.class | $(".intro,.demo") | All elements with the class "intro" or "demo" |
element | $("p") | All <p> elements |
el1,el2,el3 | $("h1,div,p") | All <h1>, <div> and <p> elements |
:first | $("p:first") | The first <p> element |
:last | $("p:last") | The last <p> element |
:even | $("tr:even") | All even <tr> elements |
:odd | $("tr:odd") | All odd <tr> elements |
:first-child | $("p:first-child") | All <p> elements that are the first child of their parent |
:first-of-type | $("p:first-of-type") | All <p> elements that are the first <p> element of their parent |
:last-child | $("p:last-child") | All <p> elements that are the last child of their parent |
:last-of-type | $("p:last-of-type") | All <p> elements that are the last <p> element of their parent |
:nth-child(n) | $("p:nth-child(2)") | All <p> elements that are the 2nd child of their parent |
:nth-last-child(n) | $("p:nth-last-child(2)") | All <p> elements that are the 2nd child of their parent, counting from the last child |
:nth-of-type(n) | $("p:nth-of-type(2)") | All <p> elements that are the 2nd <p> element of their parent |
:nth-last-of-type(n) | $("p:nth-last-of-type(2)") | All <p> elements that are the 2nd <p> element of their parent, counting from the last child |
:only-child | $("p:only-child") | All <p> elements that are the only child of their parent |
:only-of-type | $("p:only-of-type") | All <p> elements that are the only child, of its type, of their parent |
parent > child | $("div > p") | All <p> elements that are a direct child of a <div> element |
parent descendant | $("div p") | All <p> elements that are descendants of a <div> element |
element + next | $("div + p") | The <p> element that are next to each <div> elements |
element ~ siblings | $("div ~ p") | All <p> elements that are siblings of a <div> element |
:eq(index) | $("ul li:eq(3)") | The fourth element in a list (index starts at 0) |
:gt(no) | $("ul li:gt(3)") | List elements with an index greater than 3 |
:lt(no) | $("ul li:lt(3)") | List elements with an index less than 3 |
:not(selector) | $("input:not(:empty)") | All input elements that are not empty |
:header | $(":header") | All header elements <h1>, <h2> ... |
:animated | $(":animated") | All animated elements |
:focus | $(":focus") | The element that currently has focus |
:contains(text) | $(":contains('Hello')") | All elements which contains the text "Hello" |
:has(selector) | $("div:has(p)") | All <div> elements that have a <p> element |
:empty | $(":empty") | All elements that are empty |
:parent | $(":parent") | All elements that are a parent of another element |
:hidden | $("p:hidden") | All hidden <p> elements |
:visible | $("table:visible") | All visible tables |
:root | $(":root") | The document's root element |
:lang(language) | $("p:lang(de)") | All <p> elements with a lang attribute value starting with "de" |
[attribute] | $("[href]") | All elements with a href attribute |
[attribute=value] | $("[href='default.htm']") | All elements with a href attribute value equal to "default.htm" |
[attribute!=value] | $("[href!='default.htm']") | All elements with a href attribute value not equal to "default.htm" |
[attribute$=value] | $("[href$='.jpg']") | All elements with a href attribute value ending with ".jpg" |
[attribute|=value] | $("[title|='Tomorrow']") | All elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen |
[attribute^=value] | $("[title^='Tom']") | All elements with a title attribute value starting with "Tom" |
[attribute~=value] | $("[title~='hello']") | All elements with a title attribute value containing the specific word "hello" |
[attribute*=value] | $("[title*='hello']") | All elements with a title attribute value containing the word "hello" |
:input | $(":input") | All input elements |
:text | $(":text") | All input elements with type="text" |
:password | $(":password") | All input elements with type="password" |
:radio | $(":radio") | All input elements with type="radio" |
:checkbox | $(":checkbox") | All input elements with type="checkbox" |
:submit | $(":submit") | All input elements with type="submit" |
:reset | $(":reset") | All input elements with type="reset" |
:button | $(":button") | All input elements with type="button" |
:image | $(":image") | All input elements with type="image" |
:file | $(":file") | All input elements with type="file" |
:enabled | $(":enabled") | All enabled input elements |
:disabled | $(":disabled") | All disabled input elements |
:selected | $(":selected") | All selected input elements |
:checked | $(":checked") | All checked input elements |
혹시 처음 jQuery를 접하는 분들이 계실수도 있다생각하여 몇자 적겠습니다.
우리가 HTML 마크업을 할때 id, class 명을 기입하죠?
모두다 그런건 아니지만 평균적으로
고유성을 띄는것은 id로,
그룹성을 띄는것을 class로..
<input type='text' value='탁탁' id='taktak' class='TAK' /> <input type='text' value='틱틱' id='tiktik' class='TAK' />
이런 마크업을 했다고 가정할게요.
여기서 id가 "taktak"인것의 input의 값을 가지고 오려면 선택자는 어떻게 해야할까요?
(본문 상단에 테이블을 참고해주세요)
$('#taktak').val();
이것이 끝입니다. 매우 간단합니다 :)
그렇다면 class명이 TAK인것중에 두번째 input의 값은 어떻게 가지고 올까요?
$('.TAK').eq(1).val();
이것 역시 매우 간단합니다 :)
따라서, 가장 많이사용하는 셀렉터(id, class)는 바로 "." or "#" 이 됩니다.
'jQuery' 카테고리의 다른 글
jQuery frameset 접근 (부재 : ready와 onload의 차이) (0) | 2015.01.28 |
---|---|
jQuery Ajax 설명 및 사용법 (4) | 2015.01.28 |
jQuery Spectrum (컬러피커 플러그인) (0) | 2015.01.20 |
jQuery에 대한 여러가지 생각 (2) | 2015.01.20 |
jQuery NProgress (프로그레스 플러그인) (0) | 2015.01.20 |
Comments