jQuery筛选

筛选:eq()、first()、last()、hasClass()、children()、filter()、has()、not()、next()、nextAll()、nextUntil()、parent()、parents():

eq():获取当前链式操作中第N个jQuery对象,从0开始:
<p> This is just a test.</p> <p> So is this</p>
$("p").eq(1)
<p> So is this</p>

first():获取匹配的第一个元素:
$('li').first()

last():获取匹配的最后一个元素:
$('li').last()

hasClass():检查当前的元素是否含有某个特定的类:
$(this).hasClass("classname")

children():查找指定元素中的每个子元素:
$("div").children()

filter():筛选出与指定条件匹配的元素集合:
筛选出类名为UserClick的p元素集合:
$("p").filter(".UserClick")

has():保留包含特定后代的元素,去掉那些不含有指定后代的元素:
<ul>
<li>list item 1</li>
<li>list item 2
<ul>
<li>list item 2-a</li>
<li>list item 2-b</li>
</ul>
</li>
<li>list item 4</li>
</ul>

给含有ul的li加上背景色:
$('li').has('ul').css('background-color', 'red');

not():从匹配元素的集合中删除与指定条件匹配的元素:
$("p").not( $("#selected")[0] )

next():找出同辈元素紧邻的后一个元素:
$("p").next()

扫描二维码关注公众号,回复: 431133 查看本文章

prev():找出同辈元素紧邻的前一个元素:
$("p").prev()

nextAll():查找当前元素之后所有的同辈元素:
$("div").nextAll()

prevAll():查找当前元素之前所有的同辈元素:
$("div").prevAll()

nextUntil():查找当前元素之后所有的同辈元素,直到遇到匹配的那个元素为止:
$('#diva').nextUntil('#divc') //查找#diva与divc之间的所有同辈元素

parent():查找父元素,例子:
$("p").parent()

parents():查找span元素所有是p元素的父元素:
$("span").parents("p")

猜你喜欢

转载自www.cnblogs.com/miaoxingxiaoQ/p/9020021.html