jQuery API | 返回首页目录 | jQuery API 中英文对照版 |
filter |
filter(expression) 从所有匹配的元素集合中删除那些与指定的表达式(可以是多个)不匹配的元素(即返回与指定表达式匹配的元素集合)。这个方法用于缩小匹配的范围。 返回值:jQuery 参数:
选择所有段落并删除那些类名不是selected的元素。 $("p").filter(".selected") HTML 代码: <p class="selected">Hello</p><p>How are you?</p> 结果: [ <p class="selected">Hello</p> ] 示例:选择所有段落并删除那些类名不是selected的元素,但不删除第一个元素。 $("p").filter([".selected", ":first"]) HTML 代码: <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p> 结果: [ <p>Hello</p>, <p class="selected">And Again</p> ] Removes all elements from the set of matched elements that do not match the specified expression(s). This method is used to narrow down the results of a search. Provide a comma-separated list of expressions to apply multiple filters at once. Return value: jQuery
Example: Selects all paragraphs and removes those without a class "selected". $("p").filter(".selected")Before: <p class="selected">Hello</p><p>How are you?</p>Result: [ <p class="selected">Hello</p> ] Example: Selects all paragraphs and removes those without class "selected" and being the first one. $("p").filter(".selected,?:first")Before: <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>Result: [ <p>Hello</p>, <p class="selected">And Again</p> ]
|