筛选
eq(index|-index)
first()
last()
hasClass(class)
filter(expr|obj|ele|fn)
is(expr|obj|ele|fn)
map(callback)
has(expr|ele)
not(expr|ele|fn)
slice(start,[end])
主要用于找子元素
$(“ul>li”);
eq(); 获取子元素里面的其中某一个 根据索引来获取
参数:index -index -1 开始
$("ul>li").eq(0);
//等价于
$("ul>li:first");
$("ul>li:nth-child(1)");
$("ul>li").eq(-1)
first() 获取第一个子元素
last() 获取最后一个子元素
$("ul>li").first()
$("ul>li").last()
hasClass 根据元素的类名称来进行过滤
hasClass 是jquery的方法
如果要使用该方法 将ele js对象转化为jquery对象
console.log($("ul>li").eq(2).hasClass("box"));
//返回值为 true false
var dom=$("ul>li").filter(function (index,ele){
//转化为jquery对象 $(ele)转化为jquery对象 $(ele)
if($(ele).hasClass("box")){
return ".box";
return $(ele);
}
}
filter() 过滤 没有过滤的条件 selector
$("ul>li").filter(".box")
is() 判断当前元素是什么
is返回值 true/false
$("ul>li").filter(function (index,ele){
console.log(index,ele);
if($(ele).is("#li3"))
{
console.log("找到了");
}
});
$("ul>li").is($(".box"))
map将数组映射为一个新集合
jquery 对象集合
也可以把数组当作元素的遍历来使用
var newdom=$("ul>li").map(function (index,ele){
console.log(index, ele);
return $(ele).html();
};
//映射完成之后 转化为array
newdom.get();
map映射两种使用
1,将一个集合映射为一个新的集合
2.map可以作为遍历来使用
get() 获取当前索引的对象 返回值是js对象
$("ul>li")[0]
$("ul>li").get(0)
//get(index) 返回js对象
//eq(index) 返回的是jquery对象
has() 过滤元素 把当前需要的过滤出来 不需要的去除
参数:可以是选择器
//指 的是子父关系 保留的是父
$("ul>li").has(".child")
slice 参数是start end
console.log($("ul>li").slice(0, 2));
查找
children([expr])
closest(e|o|e)1.7*
find(e|o|e)
next([expr])
nextAll([expr])
nextUntil([e|e][,f])
offsetParent()
parent([expr])
parents([expr])
parentsUntil([e|e][,f])
prev([expr])
prevAll([expr])
prevUntil([e|e][,f])
siblings([expr])
children 获取子元素的 获取所有的子集元素(直接子集)
children 参数expr 选择器 可以作为简单过滤
$(".menu").children(".box");
$(".menu>li");
$(".menu li");
find 查找
$(".menu").find(".box");//expr
$(".menu").find($(".box"));//jquery对象
$(".menu").find($(".box").get(0));//ele
next 获取当前匹配元素的下一个
nextAll 获取当前匹配元素之后的所有元素
$(".menu").find(".box").next();
console.log($(".menu").find(".box").next(".fli"));
$(".menu").find(".box").nextAll();
$(".menu").find(".box").nextAll(".fli");
父级元素获取
offsetParent() 该方法返回的父元素是定位的
找到第一个被定位的父节点
parent 获取直接父元素 parents
$(".childmenu").offsetParent();
$(".childmenu").parents(".menu");
获取当前匹配元素之前的元素
prev([expr]) 之前的一个
prevAll([expr]) 之前的所有
prevUntil([e|e][,f])
console.log($(".box").prev()); //同级元素前一个
siblings 同胞兄弟元素
$(".box").siblings();//不带参数 指获取所有的同胞兄弟
串联
add(e|e|h|o[,c])1.9*
addBack()1.9+
contents()
end()
add() 给jquery对象添加新的对象
$("ul>li").add($("p"));
$("ul>li").add("p");
$("ul>li").eq(1).nextAll().addBack();
contents 获取当前元素的所有节点 包含文本
$("ul").contents();
end 方法是回到上一次破坏性修改 上一次修改jquery对象
$("ul").find(".box").end();