5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQuery 書き方を忘れがちなフィルター関係のセレクタ

Last updated at Posted at 2015-06-12

いつも忘れてしまうのでメモしときます。

属性を指定する[attr=value]

<a href=hoge.html>hoge</a>
<a href=hage.html>hage</a>
console.log($("a[href='hoge.html']").text());
結果:hoge

サンプル jsfiddle

テキストを指定する:contains(text)

<span>I am</span>
<span>pen</span>
console.log($("span:contains(pen)").text());
結果:pen

サンプル

指定した要素を含んだ要素を取得:has(selector)

例:id=span2の子要素を含むdiv要素を隠します

<div>div1:<span id=s1>Span1</span></div>
<div>div2:<span id=s2>Span2</span></div>
$("div:has(#s2)").hide()

サンプル

空の要素を取得:empty

要素の中が空の場合にマッチします。

$("div:empty").text("hoge");

サンプル

空じゃない要素を取得:parent

$("div:parent").show();

要素によって空かどうかの判定が異なります。
<p></p><span></span>は空と判定されますが、<br><hr><img><input>等は空ではないと判定されます。たぶん終了タグが必要ない要素は空ではないと判定されます。
サンプル

非表示の要素取得:hidden

$("button").click(function(){
    $("div:hidden").show();
});
ボタンをクリックすると非表示属性が表示されます

だだしopacity=0にはマッチしません。
同様に:visibleを指定すると、表示されている要素を取得できます。
サンプル

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?