jQuery filter()メソッド

❮jQueryトラバースメソッド

クラス名が「intro」のすべての<p>要素を返します。

$("p").filter(".intro")

定義と使用法

filter()メソッドは、特定の基準に一致する要素を返します。

このメソッドでは、基準を指定できます。基準に一致しない要素は選択から削除され、一致する要素が返されます。

この方法は、選択した要素のグループ内の要素の検索を絞り込むためによく使用されます。

ヒント: filter()メソッドは、 not()メソッドの反対です


構文

$(selector).filter(criteria,function(index))

Parameter Description
criteria Optional. Specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements.

Tip: To specify multiple criteria, use comma.
function(index Optional. Specifies a function to run for each element in the set. If it returns true, the element is kept. Otherwise, the element is removed.
  • index - The index position of the element in the set
Note: this is the current DOM element.

自分で試してみてください-例


返す:evenセレクターをfilter()と一緒に使用して、偶数であるすべての<p>要素を返します。


クラスが「intro」でIDが「outro」のすべての<p>要素を返す方法。


、<div>要素内のクラス「intro」を持つすべての<p>要素を返す方法。


要素を使用してIDが「intro」の<p>要素を返す方法。


使用関数を使用して、内部に2つの<span>要素を持つすべての<p>要素を選択する方法。


❮jQueryトラバースメソッド