1. 基本选择器

1.1 id选择器:$(#id)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div {
width: 100%;
height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
}
</style>
</head>
<body>
<div id="a"></div>
<div id="a"></div> <script>
$("#a").html("我选中你了");
</script>
</body>
</html>

执行结果:只选中第一个。

1.2 类型选择器:$(.class)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div {
width: 100%;
height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
}
</style>
</head>
<body>
<div class="a"></div>
<div class="a"></div> <script>
$(".a").html("我选中你了");
</script>
</body>
</html>

执行结果:两个div都会被选中。

1.3 元素选择器:$(element)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div, span {
width: 100%;
height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div></div>
<div></div>
<span></span>
<span></span> <script>
$("span").html("我选中你了");
</script>
</body>
</html>

执行结果:两个span元素会被选中。

1.4 全部子孙选择器:$(选择器1 *)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div, span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div><span></span></div>
<div><div></div></div>
<span></span>
<span></span> <script>
$("body *").css("border-color","red");
</script>
</body>
</html>

执行结果:body下的所有子元素(包含子元素的子元素)都被选中。

1.5 多项选择器:$(选择器1,选择器2,选择器3,选择器4)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div, span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div id="a"></div>
<div class="b"></div>
<span>
<div></div>
<div></div>
</span>
<div></div>
    <script>
$("#a,.b,span *").html("我选中你了");
</script>
</body>
</html>

上例中,$("#a,.b,span *")包含了三个选择器,#a选中id等于a的元素,.b选中class等于b的元素,span *选中所有span下的所有元素。所以执行结果为:前两个div以及span内的两个div会被选中。

1.6 基本选择器的组合应用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div, span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div class="a"></div>
<span class="a"></span>
<span id="b"></span>
<span id="c">
<a></a>
<a></a>
</span>
<script>
$("div.a, span#b, span#c *").html("我选中你了");
</script>
</body>
</html>

执行结果:class等于a的div将会被选中,id等于b的span将会被选中,id等于c的所有子元素将会被选中。

2. 层级选择器

2.1 祖孙选择器:$(选择器1 选择器2)

选择“选择器1”的子元素(包含子元素的子元素)中,匹配“选择器2”的所有元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div, span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<a>第1个</a>
<span>
<div>
<a>第2个</a>
<a>第3个</a>
</div>
<a>第4个</a>
<div>
<a>第5个</a>
<span>
<a>第6个</a>
</span>
</div>
</span>
<script>
$("span a").css("color","red");
</script>
</body>
</html>

执行结果:2-6会被选中。

2.2 父子选择器:$(选择器1>选择器2)

选择“选择器1”的子元素(必须是直属父子元素)中,符合“选择器2”的所有元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div, span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<a>第1个</a>
<span>
<div>
<a>第2个</a>
<a>第3个</a>
</div>
<a>第4个</a>
<div>
<a>第5个</a>
<span>
<a>第6个</a>
</span>
</div>
</span>
<script>
$("span>a").css("color","red");
</script>
</body>
</html>

执行结果:4,6会被选中。

2.3 兄弟选择器:$(选择器1+选择器2)

选择“选择器1”下面与“选择器1”同级的且紧接“选择器1”的符合“选择器2”的元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
}
</style>
</head>
<body>
<div id="1" class="a">
<div id="2" class="c"></div>
<div id="3" class="b"></div>
</div>
<div id="4" class="b"></div>
<div id="5" class="d"></div>
<div id="6" class="b"></div> <script>
$(".a+.b").html("我选中你了");
</script>
</body>
</html>

执行结果:id等于4的div将会被选中,而id等于6的div则不会被选中。

2.4 同级选择器:$(选择器1~选择器2)

选择“选择器1”下面与“选择器1”同级的符合“选择器2”的元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
}
</style>
</head>
<body>
<div id="1" class="a">
<div id="2" class="c"></div>
<div id="3" class="b"></div>
</div>
<div id="4" class="b"></div>
<div id="5" class="d"></div>
<div id="6" class="b"></div> <script>
$(".a~.b").html("我选中你了");
</script>
</body>
</html>

执行结果:id等于4的div和id等于6的div都会被选中。

3. 过滤选择器

3.1 基本过滤器

  • 过滤出第一个元素:first
  • 过滤出最后一个元素:last
  • 过滤出基数元素:even
  • 过滤出偶数元素:old
  • 过滤出第几个元素:eq(x),括号内填写序号,从0开始。
  • 过滤出第几个之后的元素:gt(x),括号内填写序号,过滤出来的元素不包含x位
  • 过滤出第几个之前的元素:lt(x),括号内填写序号,过滤出来的元素不包含x位
  • 过滤出所有的标题:header,即h1-h6
  • 过滤出所有的动画元素:animated,注意是jquery动画,html5和css3动画不算。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div,span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div>
<span id="0">11111</span>
<span id="1">22222</span>
<span id="2">33333</span>
<span id="3">44444</span>
<span id="4">55555</span>
<span id="5">66666</span>
</div> <script>
$("div span:first").css("background-color","red");
$("div span:last").css("background-color","gray");
$("div span:even").css("color","green");
$("div span:odd").css("color","blue");
$("div span:not(#1,#4)").css("font-weight","bold");
$("div span:eq(3)").css("padding-left","100px");
$("div span:gt(4)").css("padding-left","200px");
$("div span:lt(2)").css("padding-left","300px");
</script>
</body>
</html>

执行结果,id=0背景变红,id=5背景变灰,id=0,2,4字体颜色变绿,id=1,3,5字体颜色变蓝,id=0,2,3,5字体加粗,id=3文字右移100px,id=5文字右移200px,id=0,1文字右移300px。剩下两个过滤器不再展示。

3.2 内容过滤器:

  • :contains('x') 过滤出容包含x的元素,模糊匹配。
  • :empty 过滤出内容为空的元素,包含其他元素、包含空格、包含内容都不能算作为空。
  • :has('x') 过滤出包含x子元素的元素。
  • :parent 过滤出内容不为空的元素,包含其他元素、包含空格、包含内容都不能算作为空。(其实就是和empty相反)
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script src="jquery-3.0.0.min.js"></script>
    <style>
    div,span {
    width: 100%;
    min-height: 20px;
    text-align:center;
    border: 1px solid #555;
    margin: 5px;
    display:block;
    }
    </style>
    </head>
    <body>
    <div>
    <span id="0">11a11</span>
    <span id="1">22b22</span>
    <span id="2"><a></a></span>
    <span id="3"></span>
    <span id="4"><a>55a55</a></span>
    <span id="5"><label>66a66<label></span>
    </div> <script>
    $("div>span:contains('a')").css("color","red");
    $("div>span:empty").css("background-color","blue");
    $("div>span:has('a')").css("font-size","50px");
    $("div>span:parent").css("border-width","3px");
    </script>
    </body>
    </html>

    执行结果:红色字体id=0,4,5    蓝色背景id=3      大号字体id=4      加粗边框id=0,1,2,4,5

3.3 可见性过滤选择器:

  针对css的display属性有效。

  • :hidden 过滤出不可见元素。
  • :visible 过滤出可见元素。

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div,span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div id="a">
<div id="0" style="visibility:visible">000</div>
<div id="1" style="visibility:hidden">111</div>
<div id="2" style="display:none">222</div>
<div id="3" style="display:block">333</div>
</div> <script>
$("div#a>div:visible").css("background-color","red");
$("div#a>div:hidden").css("background-color","green");
</script>
</body>
</html>

执行结果:

<div id="a">
<div id="0" style="visibility: visible; background-color: red;">000</div>
<div id="1" style="visibility: hidden; background-color: red;">111</div>
<div id="2" style="display: none; background-color: green;">222</div>
<div id="3" style="display: block; background-color: red;">333</div>
</div>

3.4 属性过滤器

  • [attribute]过滤出包含某个属性的元素。
  • [attribute=value]过滤出某个属性等于某个值的元素(区分大小写)。
  • [attribute!=value]过滤出某个属性不等于某个值的元素(区分大小写)。
  • [attribute^=value]过滤出某个属性以某个值开头的元素(区分大小写)。
  • [attribute$=value]过滤出某个属性以某个值结尾的元素(区分大小写)。
  • [attribute*=value]过滤出某个属性包含某个值的元素(区分大小写)。
  • [属性过滤器1][属性过滤器2]属性过滤器的组合使用(and关系)。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div,span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div>
<input id="1" name="a" value="ABCD"/>
<input id="1" name="b" value="abcd"/>
<input id="2" name="a" value="XYZ"/>
<input id="2" name="b" value="xyz"/>
<input value="哈哈哈"/>
</div> <script>
$("div>input[name]").css("background-color","red");
$("div>input[name=a]").css("font-size","30px");
$("div>input[name!=a]").css("font-size","60px");
$("div>input[value^=a]").css("color","yellow");
$("div>input[value$=z]").css("color","blue");
$("div>input[value*=C]").css("color","green");
$("div>input[value^=a][value$=d").css("border-width","8px");
</script>
</body>
</html>

执行结果:1-4背景红色,1、3是30px字体,2、4、5是60px字体,2是黄色字体,4是蓝色字体,1是绿色字体,2加粗边框。

3.5 子元素过滤器

  • :nth-child(x) 如果某元素是其父元素的第x个子元素,那么该元素将被过滤出来。
  • :first-child 如果某元素是其父元素的第一个子元素,那么该元素将被过滤出来
  • :last-child 如果某元素是其父元素的最后一个子元素,那么该元素将被过滤出来
  • :only-child 如果某元素是其父元素的唯一一个个子元素,那么该元素将被过滤出来
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="jquery-3.0.0.min.js"></script>
<style>
div,span {
width: 100%;
min-height: 20px;
text-align:center;
border: 1px solid #555;
margin: 5px;
display:block;
}
</style>
</head>
<body>
<div id="123">
<div>
<div></div><div></div><div></div><div></div><div></div>
</div>
<div>
<div></div><div></div><div></div><div></div><div></div>
</div>
<div>
<div></div><div></div><div></div><div></div><div></div>
</div>
<div>
<div></div><div></div><div></div><div></div><div></div>
</div>
<div>
<div></div><div></div><div></div><div></div><div></div>
</div>
<div>
<div></div>
</div>
</div> <script>
$("div#123 div:nth-child(3)").css("background-color","red");
$("div#123 div:first-child").css("border-width","10px");
$("div#123 div:last-child").css("margin-top","100px");
$("div#123 div:only-child").css("margin-left","100px");
</script>
</body>
</html>

执行结果:各位还是自己去试试看吧。

3.6 表单元素过滤器

  常用的表单元素有:<input/><textarea/><select/><option/>。

  • :enabled 过滤出可用个的表单元素
  • :disabled 过滤出不可用的表单元素
  • :checked 过滤出被勾选了的表单元素
  • :selected 过滤出被选中了的表单元素
  • :input 过滤出所有表单元素
  • :text 过滤出type=text的input元素
  • :password 过滤出type=password 的input元素
  • :radio 过滤出type=radio 的input元素
  • :checkbox 过滤出type=checkbox 的input元素
  • :submit 过滤出type=submit 的input元素
  • :image 过滤出type=image 的input元素
  • :reset 过滤出type=reset 的input元素
  • :button 过滤出type=button 的input元素
  • :file 过滤出type=file 的input元素
  • :hidden 过滤出type=hidden 的input元素

jQuery选择器全解析的更多相关文章

  1. jQuery Ajax 全解析

    转自:http://www.cnblogs.com/qleelulu/archive/2008/04/21/1163021.html 本文地址: jQuery Ajax 全解析 本文作者:QLeelu ...

  2. jQuery Ajax 全解析(转载)

    本文地址: jQuery Ajax 全解析 本文作者:QLeelulu 转载请标明出处! jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写Java ...

  3. jQuery选择器全解

    本篇介绍jQuery的选择器,jQuery选择器按照功能上分为"选择"和"过滤",并且是配合使用的.过滤的主要作用是从前面选定的选择器中选择的内容重进行筛选. ...

  4. jQuery插件开发全解析,类级别与对象级别开发

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  5. jQuery插件开发全解析

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  6. jQuery Ajax 全解析(转)

    <!-- .ajax div{ border: solid 1px red; } --> // <![CDATA[ $(function(){ $("#btnajax&qu ...

  7. jQuery Ajax全解析

    jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 我们先来看一些简单的方法,这些方法都是对jQuery.ajax( ...

  8. (转)jQuery插件开发全解析

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  9. jQuery插件开发全解析[转]

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

随机推荐

  1. k8s 各种网络方案【转】

    网络模型有了,如何实现呢? 为了保证网络方案的标准化.扩展性和灵活性,Kubernetes 采用了 Container Networking Interface(CNI)规范. CNI 是由 Core ...

  2. QT5安装

    Windows+Qt5.3.1+VS2013安装教程 https://blog.csdn.net/two_ye/article/details/96109876 (已成功)windows下,VS201 ...

  3. 【Winform】键 盘 事 件

    private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) { , (, (, (, ( }; //回车 Backsp ...

  4. KMP【模板】 && 洛谷 P3375

    题目传送门 解题思路: 首先说KMP的作用:对于两个字符串A,B(A.size() > B.size()),求B是否是A的一个字串或B在A里的位置或A里有几个B,说白了就是字符串匹配. 下面创设 ...

  5. 单选与多选与label

    单选radio和多选checkbox是用name属性关联的 相同的name就相当于同一道题 <input type="radio" name="radio" ...

  6. JDK8中的HashMap实现原理及源码分析

    大纲 一.什么是Hash?什么是HashMap? 二.HashMap的内部实现机制 1.HashMap基本元素 ①DEFAULT_INITIAL_CAPACITY&MAXIMUM_CAPACI ...

  7. Java日志相关概述

    日志是代码调试.生产运维必备工具,基本所有软件都会有日志记录. 1.常用日志框架介绍 1.Logging jdk1.5自带日志工具类,位于java.util.logging; 2.Log4j 市场占有 ...

  8. C# 控件缩写规范

    标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...

  9. Postman配置Pre-request scripts预请求对请求进行AES加密

    1.首先,Postman的Pre-request scripts页面右边已经提供了一些模板,这些模板可以设置变量与环境变量,并使用双大括号对变量进行引用 {{info}} 2.对所有POST请求都进行 ...

  10. cf 498 B. Name That Tune

    不会不会,,,%%http://hzwer.com/5813.html #include<cstdio> #include<iostream> #include<algo ...