一:筛选中的过滤方法有哪些?
first() 获取匹配的第一个元素
<style>
.cc {
box-shadow: 0px 0px 50px yellow;
}
</style>
<script>
$(function() {
//first() 获取匹配的第一个元素
console.log($("img").first());
//添加样式
console.log($("img").first().addClass("cc"));
})
last() 获取匹配的最后一个元素
<style>
.aa {
box-shadow: 0px 0px 50px orchid;
}
</style>
<script>
$(function() {
//last() 获取匹配的最后一个元素
console.log($("img").last());
//添加样式
console.log($("img").last().addClass("aa"));
})
eq(N) 根据下标获取匹配的元素
//eq(N) 根据下标获取匹配的元素
console.log($("img").eq(2));
filter(selector) 筛选出与指定表达式匹配的元素集合
<style>
.bb {
border: 10px solid blue;
}
.aa {
box-shadow: 0px 0px 50px orchid;
}
</style>
<script>
$(function() {
//filter(selector) 筛选出与指定表达式匹配的元素集合
console.log($("img").filter(".bb"));
//添加样式
console.log($("img").filter(".bb").addClass("aa"));
})
has(selector) 筛选出包含特定特点的元素的集合
<style>
.aa {
box-shadow: 0px 0px 50px orchid;
}
</style>
<script>
$(function() {
//has(selector) 筛选出包含特定特点的元素的集合
console.log($("div").has("img"));
//添加样式
//选中div (里面有img,img要有class属性为aa)
console.log($("div").has("img").addClass("aa"));