jQuery学习(二)--各种选择器说明

本文详细介绍了jQuery的各种选择器,包括基本选择器、层次选择器、过滤选择器和表单选择器,并提供了相应的实例演示,帮助理解其用法。如基本选择器中的ID选择器和类选择器,层次选择器的后代选择器和直接子元素选择器,以及过滤选择器中的内容过滤和可见性过滤等。
摘要由CSDN通过智能技术生成

jQuery选择器的分类:

     ------基本选择器(basic)

     ------层次选择器(level)

     ------过滤选择器(filter)

     ------表单选择器(form)

 

基本选择器

实例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 type="text/javascript"  src="jquery-1.8.1.js">
</script>
<script type="text/javascript">
 $(function(){
  alert("id--->"+$("#test").length);
  alert("class--->"+$(".test").length);
 });
</script>
</head>

<body>
<a id="test">test1</a>
<a id="test">test1</a>
<a class="test">test2</a>
<a class="test">test2</a>
</body>
</html>

结果:id---->1  class---->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 type="text/javascript"  src="jquery-1.8.1.js">
</script>
<script type="text/javascript">
 $(function(){
  alert($("body div").length);
  alert($("body > div").length);
  alert($("a + p").length);
  alert($("a ~ p").length);
 });
</script>
</head>

<body>
 <div>
 <font>
  <div>这个div无法通过body > div得到</div>
 </font>
 </div>

 <div>
  <font>aaa</font>
 </div>

 <div>
  <font>bbb</font>
 </div>

 <a class="one"></a>
 <p></p>
 <p>a + p无法选择到这个</p>
 <p>a + p无法选择到这个</p>
</body>
</html>

运行结果:

body  div ---->4

body > div ---->3

a + p ---->1

a ~ p ----->3

 

 

 

 

 

 

 

过滤选择器

----基本过滤

----内容过滤

----可见性过滤

----属性过滤

----子元素过滤

-----表单对象属性过滤

 

基本过滤

 

内容过滤

 

可见性过滤器

实例:

<!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 type="text/javascript"  src="jquery-1.8.1.js">
</script>
<script type="text/javascript">
 $(function(){
  $("#click").click(function(){
   $(":hidden").show(1000).css("background","blue");
  });
 });
</script>
</head>

<body>
<input type="button" value="出来吧" id="click">
<div style="width:400px;height:400px;border:solid;display:none">
 看我慢慢的出来啦。。。
</div>

</body>
</html>

 

 

 

属性过滤

 

 

子元素过滤

 

 

 

表单对象属性过滤

 

 

 

表单元素过滤器

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值