首页 > 语言 > JavaScript > 正文

juqery 学习之四 筛选过滤

2024-05-06 14:26:23
字体:
来源:转载
供稿:网友

eq(index)

获取第N个元素这个元素的位置是从0算起。
Reduce the set of matched elements to a single element.The position of the element in the set of matched elements starts at 0 and goes to length - 1.

返回值

jQuery

参数

index (Integer) :元素在jQuery对象中的索引

示例

获取匹配的第二个元素

HTML 代码:

<p> This is just a test.</p> <p> So is this</p>

jQuery 代码:

$("p").eq(1)

结果:

[ <p> So is this</p> ]
--------------------------------------------------------------------------------------------------------------

hasClass(class)

检查当前的元素是否含有某个特定的类,如果有,则返回true。这其实就是 is("." + class)。
Checks the current selection against a class and returns true, if at least one element of the selection has the given class.This is an alternative to is("." + class).

返回值

Boolean

参数

class (String) : 用于匹配的类名

示例

给包含有某个类的元素进行一个动画。

HTML 代码:

<div class="protected"></div><div></div>

jQuery 代码:

$("div").click(function(){
  if ( $(this).hasClass("protected") )
    $(this)
      .animate({ left: -10 })
      .animate({ left: 10 })
      .animate({ left: -10 })
      .animate({ left: 10 })
      .animate({ left: 0 });
});
--------------------------------------------------------------------------------------------------------------

filter(expr)

筛选出与指定表达式匹配的元素集合。这个方法用于缩小匹配的范围。用逗号分隔多个表达式
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.

返回值

jQuery

参数

expr (Expression) : 表达式

示例

保留带有select类的元素

HTML 代码:

<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>

jQuery 代码:

$("p").filter(".selected")

结果:

[ <p class="selected">And Again</p> ]

保留第一个以及带有select类的元素

HTML 代码:

<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选