jQuery セレクタ


jQuery セレクタ

私たちのを使って、さまざまなセレクタをデモンストレーションします。

セレクタ 例 選択
* $("*") すべての要素
#id $("#lastname") id="lastname" の要素
.class $(".intro") class="intro" のすべての要素
.class,.class $(".intro,.demo") class が "intro" または "demo" のすべての要素
element $("p") すべての <p> 要素
el1,el2,el3 $("h1,div,p") すべての <h1>、<div> および <p> 要素
:first $("p:first") 最初の <p> 要素
:last $("p:last") 最後の <p> 要素
:even $("tr:even") すべての偶数 <tr> 要素。インデックス値は 0 から始まり、最初の要素は偶数 (0)、2 番目の要素は奇数 (1) となります。
:odd $("tr:odd") すべての奇数 <tr> 要素。インデックス値は 0 から始まり、最初の要素は偶数 (0)、2 番目の要素は奇数 (1) となります。
:first-child $("p:first-child") 親要素の最初の子要素であるすべての <p> 要素
:first-of-type $("p:first-of-type") 親要素の最初の <p> であるすべての <p> 要素
:last-child $("p:last-child") 親要素の最後の子要素であるすべての <p> 要素
:last-of-type $("p:last-of-type") 親要素の最後の <p> であるすべての <p> 要素
:nth-child(n) $("p:nth-child(2)") 親要素の 2 番目の子要素であるすべての <p> 要素
:nth-last-child(n) $("p:nth-last-child(2)") 親要素の 2 番目の子要素であるすべての <p> 要素(最後の子要素から数えた場合)
:nth-of-type(n) $("p:nth-of-type(2)") 親要素の 2 番目の <p> であるすべての <p> 要素
:nth-last-of-type(n) $("p:nth-last-of-type(2)") 親要素の 2 番目の <p> であるすべての <p> 要素(最後の子要素から数えた場合)
:only-child $("p:only-child") 親要素の唯一の子要素であるすべての <p> 要素
:only-of-type $("p:only-of-type") 親要素の特定の型の唯一の子要素であるすべての <p> 要素
parent > child $("div > p") <div> 要素の直接の子要素であるすべての <p> 要素
parent descendant $("div p") <div> 要素の子孫であるすべての <p> 要素
element + next $("div + p") 各 <div> 要素の直後に隣接する <p> 要素
element ~ siblings $("div ~ p") <div> 要素と同階層(兄弟)のすべての <p> 要素
:eq(index) $("ul li:eq(3)") リスト内の 4 番目の要素(インデックス値は 0 から始まります)
:gt(no) $("ul li:gt(3)") インデックスが 3 より大きい要素を列挙
:lt(no) $("ul li:lt(3)") インデックスが 3 より小さい要素を列挙
:not(selector) $("input:not(:empty)") 空でないすべての入力要素
:header $(":header") すべての見出し要素 <h1>, <h2> ...
:animated $(":animated") すべてのアニメーション要素
:focus $(":focus") 現在フォーカスを持っている要素
:contains(text) $(":contains('Hello')") テキスト "Hello" を含むすべての要素
:has(selector) $("div:has(p)") 内部に <p> 要素を含むすべての <div> 要素
:empty $(":empty") すべての空の要素
:parent $(":parent") 子要素またはテキストを持つすべての親要素にマッチします。
:hidden $("p:hidden") 非表示のすべての <p> 要素
:visible $("table:visible") 表示されているすべてのテーブル
:root $(":root") ドキュメントのルート要素
:lang(language) $("p:lang(de)") lang 属性の値が "de" のすべての <p> 要素
     
[attribute] $("[href]") href 属性を持つすべての要素
[attribute=value] $("[href='default.htm.html']") href 属性を持ち、その値が "default.htm" と等しいすべての要素
[attribute!=value] $("[href!='default.htm']") href 属性を持ち、その値が "default.htm" と等しくないすべての要素
[attribute$=value] $("[href$='.jpg']") href 属性を持ち、その値が ".jpg" で終わるすべての要素
[attribute|=value] $("[title|='Tomorrow']") title 属性を持ち、その値が 'Tomorrow' と等しいか、'Tomorrow' の後にハイフンが続く文字列で始まるすべての要素
[attribute^=value] $("[title^='Tom']") title 属性を持ち、その値が "Tom" で始まるすべての要素
[attribute~=value] $("[title~='hello']") title 属性を持ち、その値が単語 "hello" を含むすべての要素
[attribute*=value] $("[title*='hello']") title 属性を持ち、その値が文字列 "hello" を含むすべての要素
[name=value][name2=value2] $( "input[id][name$='man']" ) id 属性を持ち、name 属性が man で終わる入力要素
     
:input $(":input") すべての input 要素
:text $(":text") type="text" のすべての input 要素
:password $(":password") type="password" のすべての input 要素
:radio $(":radio") type="radio" のすべての input 要素
:checkbox $(":checkbox") type="checkbox" のすべての input 要素
:submit $(":submit") type="submit" のすべての input 要素
:reset $(":reset") type="reset" のすべての input 要素
:button $(":button") type="button" のすべての input 要素
:image $(":image") type="image" のすべての input 要素
:file $(":file") type="file" のすべての input 要素
:enabled $(":enabled") 有効なすべての要素
:disabled $(":disabled") 無効なすべての要素
:selected $(":selected") 選択されているすべてのドロップダウンリスト要素
:checked $(":checked") チェックされているすべてのチェックボックスオプション
.selector $(selector).selector jQuery 1.7 では非推奨となりました。jQuery() に渡された元のセレクタを返します。
:target $( "p:target" ) セレクタは、ID と URI 内のフォーマットされた識別子が一致する <p> 要素を選択します。

その他の拡張