JQuery 选择器 筛选器
什么是jQuery对象
参考:http://jquery.cuishifeng.cn/css.html
jQuery 对象就是通过jQuery包装DOM对象后产生的对象。jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuery 里的方法: $(“#test”).html();
$("#test").html()
//意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法
// 这段代码等同于用DOM实现代码: document.getElementById(" test ").innerHTML;
//虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DOM对象也不能使用jQuery里的方法.乱使用会报错
//约定:如果获取的是 jQuery 对象, 那么要在变量前面加上$.
var $variable = jQuery 对象
var variable = DOM 对象
$variable[0]:jquery对象转为dom对象 $("#msg").html(); $("#msg")[0].innerHTML
jquery的基础语法:$(selector).action()
引入:
<script src="jquery.min.js" type="text/javascript"></script>
一. 选择器
1.基本选择器
$("*") 所有
$("#id") id选择器
$(".class") 类选择器
$("element") 元素选择器
$(".class,p,div")多选择器
2.层级选择器
$(".outer div") 后代选择器
$(".outer>div") 子代选择器
$(".outer+div") 毗邻选择器 找下方紧挨着的兄弟
$(".outer~div") 毗邻选择器 找下方的兄弟
3.基本筛选器(:)
$("li:first") 所有li标签第一个
$("li:eq(2)") 偶数
$("li:even")
$("li:gt(1)")
$("li:lt(5)")
4.属性选择器 ([])
$('[id="div1"]')
$('[name="sb"][id="p1"]') 多重属性选择
5.表单选择器
$("[type='text']") 简写----->$(":text") 注意只适用于input标签 : $("input:checked")
二. 筛选器
1.过滤筛选器
$("li").eq(2)
$("li").first()
$("li").last()
$("ul li").hasclass("test") 判断布尔值
2.查找筛选器
$("div").children(".test") 子代选择器 div里面子代类test标签
$("div").find(".test") 后代选择器 div里面后代类test标签
$(".test").next() 下一个兄弟元素标签
$(".test").nextAll() 下面所有的元素标签
$(".test").nextUntil() 指定范围
$("div").prev() 上一个兄弟元素标签
$("div").prevAll() 上面所有兄弟元素标签
$("div").prevUntil("#end”) div开始 id标签end结束
$(".test").parent() 父类标签元素
$(".test").parents()
$(".test").parentUntil("#end")
$("div").siblings() 查找所有的兄弟元素
eg.左侧菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>left_menu</title>
<style>
.menu{
height: 500px;
width: 30%;
background-color: gainsboro;
float: left;
}
.content{
height: 500px;
width: 70%;
background-color: rebeccapurple;
float: left;
}
.title{
line-height: 50px;
background-color: #425a66;
color: forestgreen;}
.hide{
display: none;
}
</style>
</head>
<body>
<div class="outer">
<div class="menu">
<div class="item">
<div class="title">菜单一</div>
<div class="con">
<div>111</div>
<div>111</div>
<div>111</div>
</div>
</div>
<div class="item">
<div class="title">菜单二</div>
<div class="con hide">
<div>111</div>
<div>111</div>
<div>111</div>
</div>
</div>
<div class="item">
<div class="title">菜单三</div>
<div class="con hide">
<div>111</div>
<div>111</div>
<div>111</div>
</div>
</div>
</div>
<div class="content"></div>
</div>
<script src="jquery-3.2.1.js"></script>
<script>
$(".item .title").click(function () {
$(this).next().removeClass("hide").parent().siblings().children(".con").addClass("hide");
// $(this).next().removeClass("hide");
// $(this).parent().siblings().children(".con").addClass("hide");
})
</script>
</body>
</html>
参考:https://www.cnblogs.com/yuanchenqi/articles/6070667.html
https://www.cnblogs.com/chichung/p/9703618.html

浙公网安备 33010602011771号