2.1 jQuery 选择器概述
2.1.1 什么是选择器
jQuery允许通过标签名,属性名或内容对DOM元素进行快速、准确的选择。
2.1.2 选择器的优势
与传统的JavaScript获取页面元素和编写事务相比,jQuery选择器具有明显的优势,具体表现在以下两个方面:
代码更简单
完善的检测机制
示例2-1 使用JavaScript实现隔行变色
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用JavaScript实现隔行变色</title>
<style type="text/css">
body{font-size:12px;text-align:center}
#tbStu{width:260px;border:solid 1px #666;background-color:#eee}
#tbStu tr{line-height:23px}
#tbStu tr th{background-color:#ccc;color:#fff}
#tbStu .trOdd{background-color:#fff}
</style>
<script type="text/javascript">
window.onload = function(){
var oTb = document.getElementById("tbStu");
for(var i=0;i<oTb.rows.length-1;i++){
if(i%2){
oTb.rows[i].className="trOdd";
}
}
}
</script>
</head>
<body>
<table id="tbStu" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th>学号</th><th>姓名</th><th>性别</th><th>总分</th>
</tr>
<!-- 奇数偶 -->
<tr>
<td>1001</td><td>张晓明</td><td>男</td><td>320</td>
</tr>
<!-- 偶数偶 -->
<tr>
<td>1002</td><td>李明启</td><td>女</td><td>350</td>
</tr>
</tbody>
</table>
</body>
</html>
首先通过ID号获取表格元素,然后遍历表格的各行,根据行号的奇偶性,动态设置该行的背景色
循环页面的元素会影响打开速度,如果使用jQuery选择器实现上述页面效果,则需要在页面中加入一些代码,如:
代码清单2-2 使用jQuery选择器实现隔行变色
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery选择器实现隔行变色</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.3.1.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
#tbStu{width:260px;border:solid 1px #666;background-color:#eee}
#tbStu tr{line-height:23px}
#tbStu tr th{background-color:#ccc;color:#fff}
#tbStu .trOdd{background-color:#fff}
</style>
<script type="text/javascript">
$(function(){
$("#tbStu tr:nth-child(even)").addClass("trOdd");
});
</script>
</head>
<body>
<table id="tbStu" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th>学号</th><th>姓名</th><th>性别</th><th>总分</th>
</tr>
<!-- 奇数偶 -->
<tr>
<td>1001</td><td>张晓明</td><td>男</td><td>320</td>
</tr>
<!-- 偶数偶 -->
<tr>
<td>1002</td><td>李明启</td><td>女</td><td>350</td>
</tr>
</tbody>
</table>
</body>
</html>
:nth-child(2n)是css的一个选择器,对于在DOM树中有an+b-1个兄弟节点的元素,通过:nth-child可以选择哪些元素应用样式,这些兄弟节点中编号从1开始
语法:element:nth-child(an+b){style properties}
表示element 对应的元素中,第an+b(n可以是任意整数)个元素应用css样式
tr:nth-child(2n+1):表示表中奇数行将显示这个定义的样式
tr:nth-child(odd):表示表中奇数行将显示这个定义的样式
tr:nth-child(2n):表示表中偶数行将显示这个定义的样式
tr:nth-child(even):表示表中偶数行将显示这个定义的样式
2.完善的检测机制
在传统的JavaScript代码中,给页面中某元素设置事务时必须先找到该元素,然后赋予相应的属性或事件;如果该元素在页面中不存在或者被前面代码所删除,那么浏览器将提示运行出错信息, 因此,先要检测该元素是否存在,然后再运行其属性或事件代码。
在jQuery选择器定位页面元素时,无需考虑所定位的元素在页面中是否存在,即使改元素不存在,浏览器也不提示出错。
示例2-3 使用JavaScript输出文字信息
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript 代码检测页面元素</title>
<style type="text/css">
body{font-size:12px;text-align:center}
</style>
<script type="text/javascript">
window.onload = function(){
if(document.getElementById("divT")){//用于检测所定位的页面元素是否存在,如果存在,则执行下面的代码
var oDiv = document.getElementById("divT");
oDiv.innerHTML="这是一个检测页面";
}
}
</script>
</head>
<body>
</body>
</html>
示例2-4 使用jQuery输出文字信息
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery 代码检测页面元素</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.3.1.js"></script>
<script type="text/javascript">
$(function(){
$("#divT").html("这是一个检测页面");
});
</script>
</head>
<body>
</body>
</html>
2.2 jQuery 选择器详解
根据所获取页面中元素的不同,可以将jQuery选择器分为:基本选择器、层次选择器、过滤选择器、表单选择器四大类。
其中,在过滤选择器中又可分为:简单过滤选择器、内容过滤选择器、可见性过滤选择器、属性过滤选择器、子元素过滤选择器、表单对象属性过滤选择器6种
基本选择器 | 层次选择器 | 过滤选择器 | 表单选择器 |
简单过滤选择器 | |||
内容过滤选择器 | |||
可见性过滤选择器 | |||
属性过滤选择器 | |||
子元素过滤选择器 | |||
表单对象属性过滤选择器器 |
选择器 | 功能 | 返回值 |
#id | 根据给定的ID匹配一个元素 | 单个元素 |
element | 根据给定的元素名匹配所有元素 | 元素集合 |
.class | 根据给定的类匹配元素 | 元素集合 |
* | 匹配所有元素 | 元素集合 |
selector1,selectorN | 将每一个选择器匹配到的元素合并后一起返回 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery基本选择器</title>
<script language="javascript" type="text/javascript"
src="../jquery/jquery-1.3.1.js"></script>
<style type="text/css">
body {
font-size: 12px;
text-align: center
}
.clsFrame {
width: 300px;
height: 100px
}
.clsFrame div,span {
display: none;
float: left;
width: 65px;
height: 65px;
border: solid 1px #ccc;
margin: 8px
}
.clsOne {
background-color: #eee
}
</style>
<script type="text/javascript">
$(function(){//ID匹配元素
$("#divOne").css("display","block");
})
$(function(){//元素名匹配元素
$("div span").css("display","block")
})
$(function(){//类匹配元素
$(".clsFrame .clsOne").css("display","block");
})
/*$(function(){//匹配所有元素【有问题】
$("*").css("display","block");
})*/
$(function(){//合并匹配元素
$("#divOne,span").css("display","block");
})
</script>
</head>
<body>
<div class="clsFrame">
<div id="divOne">ID</div>
<div class="clsOne">CLASS</div>
<span>SPAN</span>
<span>123</span>
</div>
</body>
</html>
表2-2 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("#divOne").css("display","block"); | 显示ID为divOne的页面元素 | |
$("div span").css("display","block"); | 显示元素名为span的页面元素 | |
$(".clsFrame .clsOne").css("display","block"); | 显示类别名为clsOne的页面元素 | |
$("*").css("display","block"); | 显示页面中所有元素 | |
$("#divOne,span").css("display","block"); | 显示ID为divOne和元素名为span页面元素 |
2.2.2 层次选择器
选择器 | 功能 | 返回值 |
ancestor descendant | 根据祖先元素匹配所有的后代元素 | 元素集合 |
parent > child | 根据父元素匹配所有的子元素 | 元素集合 |
prev + next | 匹配所有紧接在prev元素后的相邻元素 | 元素集合 |
prev ~ siblings | 匹配prev元素之后的所有兄弟元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery层次选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.3.1.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
div,span{float:left;border:solid 1px #ccc;margin:8px;display:none}
.clsFraA{width:65px;height:65px}
.clsFraP{width:45px;height:45px;background-color:#eee}
.clsFraC{width:25px;height:25px;background-color:#ddd}
</style>
<script type="text/javascript">
/*$(function(){//匹配后代元素
$("#divMid").css("display","block");
$("div span").css("display","block");
});
$(function(){//匹配子元素
$("#divMid").css("display","block");
$("div>span").css("display","block");
});
$(function(){//匹配后面元素
//$("#divMid + div").css("display","block");
$("#divMid").next().css("display","block");
});
$(function(){
$("#divMid ~ div").css("display","block");
$("#divMid").nextAll().css("display","block");
});*/
$(function(){
$("#divMid").siblings("div").css("display","block")
});
</script>
</head>
<body>
<div class="clsFraA">Left</div>
<div class="clsFraA" id="divMid">
<span class="clsFraP" id="Span1">
<span class="clsFraC" id="Span2"></span>
</span>
</div>
<div class="clsFraA">Right_1</div>
<div class="clsFraA">Right_2</div>
</body>
</html>
表2-4 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("div span").css("display","block"); | 显示<div>中所有的<span> | |
$("div>span").css("display","block"); | 显示<div>中子<span>标记 | |
$("#divMid + div").css("display","block"); | 显示ID为divMid元素后的下一个<div> | |
$("#divMid ~ div").css("display","block") | 显示ID为divMid元素后的所有<div> | |
$("#divMid").siblings("div").css("display","block") | 显示ID为divMid元素的所有相邻<div> |
选择器 | 功能 | 返回值 |
first()或:first | 获取第一个元素 | 单个元素 |
last()或:last | 获取最后一个元素 | 单个元素 |
:not(selector) | 获取除给定选择器外的所有元素 | 元素集合 |
:even | 获取所有索引值为偶数的元素,索引号从0开始 | 元素集合 |
:odd | 获取所有索引值为奇数的元素,索引号从0开始 | 元素集合 |
:eq(index) | 获取指定索引值的元素,索引号从0开始 | 单个元素 |
:gt(index) | 获取所有大于给定索引值的元素,索引号从0开始 | 元素集合 |
:lt(index) | 获取所有小于给定索引值的元素,索引号从0开始 | 元素集合 |
:header | 获取所有标题类型的元素,如h1、h2 | 元素集合 |
:animated | 获取正在执行动画效果的元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery基本过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
div{width:241px;height:83px;border:solid 1px #eee}
h1{font-size:13px}
ul{list-style-type:none;padding:0px}
.DefClass, .NotClass{height:23px;width:60px;line-height:23px;float:left;border-top:solid 1px #eee;border-bottom:solid 1px #eee}
.GetFocus{width:58px;border:solid 1px #666;background-color:#eee}
#spnMove{width:238px;height:23px;line-height:23px}
</style>
<script type="text/javascript">
/*$(function(){//增加第一个元素的类别
$("li:first").addClass("GetFocus");
})
$(function(){//增加最后一个元素的类别
$("li:last").addClass("GetFocus");
})
$(function(){//增加去除所有与给定选择器匹配的元素类别
$("li:not(.NotClass)").addClass("GetFocus");
})
$(function(){//增加所有索引值为偶数的元素类别,从0开始计数
$("li:even").addClass("GetFocus");
})
$(function(){//增加所有索引值为奇数的元素类别,从0开始计数
$("li:odd").addClass("GetFocus");
})
$(function(){//增加一个给定索引值的元素类别,从0开始计数
$("li:eq(1)").addClass("GetFocus");
})
$(function(){//增加所有大于给定索引值的元素类别,从0开始计数
$("li:gt(1)").addClass("GetFocus");
})
$(function(){//增加所有小于给定索引值的元素类别,从0开始计数
$("li:lt(4)").addClass("GetFocus");
})
$(function(){//增加标题类元素类别
$("div h1").css("width","238")
$(":header").addClass("GetFocus");
})
$(function(){
animateIt();//增加动画效果元素类别
$("#spnMove:animated").addClass("GetFocus");
})
$(function(){//动画效果
$("#spnMove").slideToggle("slow",animateIt);
})*/
</script>
</head>
<body>
<div>
<h1>基本过滤选择器</h1>
<ul>
<li class="DefClass">Item 0</li>
<li class="DefClass">Item 1</li>
<li class="NotClass">Item 2</li>
<li class="DefClass">Item 3</li>
</ul>
<span id="spnMove">Span Move</span>
</div>
</body>
</html>
关键代码 | 功能描述 | 页面效果 |
$("li:first").addClass("GetFocus"); | 增加第一个元素的类别 | |
$("li:last").addClass("GetFocus"); | 增加最后一个元素的类别 | |
$("li:not(.NotClass)").addClass("GetFocus"); | 增加去除所有与给定选择器匹配的元素类别 | |
$("li:even").addClass("GetFocus"); | 增加所有索引值为偶数的元素类别,从0开始计数 | |
$("li:odd").addClass("GetFocus"); | 增加所有索引值为奇数的元素类别,从0开始计数 | |
$("li:eq(1)").addClass("GetFocus"); | 增加一个给定索引值的元素类别,从0开始计数 | |
$("li:gt(1)").addClass("GetFocus"); | 增加所有大于给定索引值的元素类别,从0开始计数 | |
$("li:lt(4)").addClass("GetFocus"); | 增加所有小于给定索引值的元素类别,从0开始计数 | |
$(":header").addClass("GetFocus"); | 增加标题类元素类别 | |
$("#spnMove:animated").addClass("GetFocus"); | 增加动画效果元素类别 |
2.2.4 内容过滤选择器
选择器 | 功能 | 返回值 |
:contains(text) | 获取包含给定文本的元素 | 元素集合 |
:empty | 获取所有不包含子元素或者文本的空元素 | 元素集合 |
:has(selector) | 获取含有选择器所匹配的元素的元素 | 元素集合 |
:parent | 获取含有子元素或者文本的元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery内容过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
div{float:left;border:solid 1px #ccc;margin:8px;width:65px;height:65px;display:none}
span{float:left;border:solid 1px #ccc;margin:8px;width:45px;height:45px;background-color:#eee}
</style>
<script type="text/javascript">
/*$(function(){//显示包含给定文本的元素
$("div:contains('A')").css("display","block");
});
$(function(){//显示所有不包含子元素或者文本的空元素
$("div:empty").css("display","block");
})
$(function(){//显示含有选择器所匹配的元素
$("div:has(span)").css("display","block");
})*/
$(function(){//显示含有子元素或者文本的元素
$("div:parent").css("display","block");
})
</script>
</head>
<body>
<div>
ABCD
</div>
<div>
A
</div>
<div>
<span>12</span>
</div>
<div>EFaH</div>
<div></div>
</body>
</html>
表 2-8 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("div:contains('A')").css("display","block"); | 显示包含给定文本"A"的元素 | |
$("div:empty").css("display","block"); | 显示所有不包含子元素或者文本的空元素 | |
$("div:has(span)").css("display","block"); | 显示含有<span>标记的元素 | |
$("div:parent").css("display","block"); | 显示含有子元素或者文本的元素 |
选择器 | 功能 | 返回值 |
:hidden | 获取所有不可见元素,或者type为hidden的元素 | 元素集合 |
:visible | 获取所有的可见元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery可见性过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
div,span{float:left;border:solid 1px #ccc;margin:8px;width:65px;height:65px}
.GetFocus{border:solid 1px #666;background-color:#eee}
</style>
<script type="text/javascript">
/*$(function(){//增加所有可见元素类别
$("span:hidden").show();
$("div:visible").addClass("GetFocus");
})*/
$(function(){//增加所有不可见元素类别
$("span:hidden").show().addClass("GetFocus");
})
</script>
</head>
<body>
<span style="display:none">Hidden</span>
<div>Visible</div>
</body>
</html>
表2-10 页面执行结果
关键代码 | 功能描述 | 页面效果 |
$("div:visible").addClass("GetFocus"); | 增加所有可见元素类别 | |
$("span:hidden").show().addClass("GetFocus"); | 增加所有不可见元素类别 |
选择器 | 功能 | 返回值 |
[attribute] | 获取包含给定属性的元素 | 元素集合 |
[attribute=value] | 获取等于给定的属性是某个特定值的元素 | 元素集合 |
[attribute!=value] | 获取不等于给定的属性是某个特定值的元素 | 元素集合 |
[attribuge^=value] | 获取给定的属性是以某些值开始的元素 | 元素集合 |
[attribuge$=value] | 获取给定的属性是以某些值结尾的元素 | 元素集合 |
[attribute*=value] | 获取给定的属性是以包含某些值的元素 | 元素集合 |
[selector1][selector2][selector3] | 获取满足多个条件的复合属性的元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery属性过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
div{float:left;border:solid 1px #ccc;margin:8px;width:65px;height:65px;display:none}
</style>
<script type="text/javascript">
/*$(function(){//显示所有含有id属性的元素
$("[id]").show(3000)
})
$(function(){
//显示所有属性title的值是"A"的元素
$("div[title='A']").show(3000);
})
$(function(){
//显示所有属性title的值不是"A"的元素
$("div[title!='A']").show(3000);
})
$(function(){
//显示所有属性title的值以"A"开始的元素
$("div[title^='A']").show(3000);
})
$(function(){
//显示所有属性title的值以"C"结束的元素
$("div[title$='C']").show(3000);
})
$(function(){
//显示所有属性title的值中含有"B"的元素
$("div[title*='B']").show(3000);
})*/
$(function(){
//显示所有属性title的值中含有"B"且属性值id的值是"divAB"的元素
$("div[id='divAB'][title*='B']").show(3000);
})
</script>
</head>
<body>
<div id="divID">ID</div>
<div title="A">Title A</div>
<div id="divAB" title="AB">ID<br/>Title AB</div>
<div title="ABC">Title ABC</div>
</body>
</html>
表2-12 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("[id]").show(3000) | 显示所有含有id属性的元素 | |
$("div[title='A']").show(3000); | 显示所有属性title的值是“A”的元素 | |
$("div[title!='A']").show(3000); | 显示所有属性title的值不是"A"的元素 | |
$("div[title^='A']").show(3000); | 显示所有属性title的值以"A"开始的元素 | |
$("div[title$='C']").show(3000); | 显示所有属性title的值以“C”结束的元素 | |
$("div[title*='B']").show(3000); | 显示所有属性title的值中含有“B”的元素 | |
$("div[id='divAB'][title*='B']").show(3000); | 显示所有属性title的值中含有"B'且属性id的值是"divAB"的元素 |
选择器 | 功能 | 返回值 |
:nth-child(eq|even|odd|index) | 获取每个父元素下的特定位置元素,索引号从1开始 | 元素集合 |
:first-child | 获取每个父元素下的第一个子元素 | 元素集合 |
:last-child | 获取每个父元素下的最后一个子元素 | 元素集合 |
:only-child | 获取每个父元素下的仅有一个子元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery子元素过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
ul{width:241px;list-style-type:none;padding:0px}
ul li{height:23px;width:60px;line-height:23px;float:left;border-top:solid 1px #eee;border-bottom:solid 1px #eee;margin-bottom:5px}
.GetFocus{width:58px;border:solid 1px #666;background-color:#eee}
</style>
<script type="text/javascript">
/*$(function(){//增加每个父元素下的第2个子元素类别
$("li:nth-child(2)").addClass("GetFocus");
})
$(function(){//增加每个父元素下的第一个子元素类别
$("li:first-child").addClass("GetFocus");
})
$(function(){//增加每个父元素下的最后一个子元素类别
$("li:last-child").addClass("GetFocus");
})*/
$(function(){//增加每个父元素下只有一个子元素类别
$("li:only-child").addClass("GetFocus");
})
</script>
</head>
<body>
<ul>
<li>Item 1-0</li>
<li>Item 1-1</li>
<li>Item 1-2</li>
<li>Item 1-3</li>
</ul>
<ul>
<li>Item 2-0</li>
<li>Item 2-1</li>
<li>Item 2-2</li>
<li>Item 2-3</li>
</ul>
<ul>
<li>Item 3-0</li>
</ul>
</body>
</html>
表2-14 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("li:nth-child(2)").addClass("GetFocus"); | 增加每个父元素下的第二个子元素类别 | |
$("li:first-child").addClass("GetFocus"); | 增加每个父元素下的第一个子元素类别 | |
$("li:last-child").addClass("GetFocus"); | 增加每个父元素下的最后一个子元素类别 | |
$("li:only-child").addClass("GetFocus"); | 增加每个父元素下的仅有一个子元素类别 |
选择器 | 功能 | 返回值 |
:enable | 获取表单中所有属性为可用的元素 | 元素集合 |
:disable | 获取表单中所有属性为不可用的元素 | 元素集合 |
:checked | 获取表单中所有被选中的元素 | 元素集合 |
:selected | 获取表单中所有被选中option的元素 | 元素集合 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery子元素过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
div{display:none}
select{height:65px}
.clsIpt{width:100px;padding:3px}
.GetFocus{border:solid 1px #666;background-color:#eee}
</style>
<script type="text/javascript">
/*$(function(){//增加表单中所有属性为可用的元素类别
$("#divA").show(3000);
$("#form1 input:enabled").addClass("GetFocus")
})
$(function(){//增加表单中所有属性为不可用的元素类别
$("#divA").show(3000);
$("#form1 input:disabled").addClass("GetFocus")
})
$(function(){//增加表单中所有被选中的元素类别
$("#divB").show(3000);
$("#form1 input:checked").addClass("GetFocus");
})*/
$(function(){//显示表单中所有被选中option的元素内容
$("#divC").show(3000);
$("#Span2").html("选中项是:"+$("select option:selected").text());
})
</script>
</head>
<body>
<form id="form1" style="width:241px">
<div id="divA">
<input type="text" value="可用文本框" class="clsIpt"/>
<input type="text" disabled="disabled" value="不可用文本框" class="clsIpt"/>
</div>
<div id="divB">
<input type="checkbox" checked="checked" value="1">选中
<input type="checkbox" value="0">未选中
</div>
<div id="divC">
<select multiple="multiple">
<option value="0">Item 0</option>
<option value="1" selected="selected">Item 1</option>
<option value="2">Item 2</option>
<option value="3" selected="selected">Item 3</option>
</select>
<span id="Span2"></span>
</div>
</form>
</body>
</html>
表2-16 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("#form1 input:enabled").addClass("GetFocus") | 增加表单中所有属性为可用的元素类别 | |
$("#form1 input:disabled").addClass("GetFocus") | 增加表单中所有属性为不可用的元素类别 | |
$("#form1 input:checked").addClass("GetFocus"); | 增加表中所有被选中的元素类别 | |
$("#Span2").html("选中项是:"+$("select option:selected").text()); | 显示表单中所有被选中option的元素内容 |
2.2.9 表单选择器
表2-17 表单选择器语法
选择器 | 功能 | 返回值 |
:input | 获取所有input textarea select | 元素集合 |
:text | 获取所有单行文本框 | 元素集合 |
:password | 获取所有密码框 | 元素集合 |
:radio | 获取所有单选按钮 | 元素集合 |
:checkbox | 获取所有复选框 | 元素集合 |
:submit | 获取所有提交按钮 | 元素集合 |
:image | 获取所有图像域 | 元素集合 |
:reset | 获取所有重置按钮 | 元素集合 |
:button | 获取所有按钮 | 元素集合 |
:file | 获取所有文件域 | 元素集合 |
代码清单2-13 使用jQuery表单过滤选择器获取元素
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery表单过滤选择器</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:12px;text-align:center}
form{width:241px}
textarea,select,button,input,span{display:none}
.btn{border:#666 1px solid;padding:2px;width:60px;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff,EndColorStr=#Ece9D8)}
.txt{border:#666 1px solid;padding:3px}
.img{padding:2px;border:solid 1px #ccc}
.div{border:solid 1px #ccc;background-color:#eee;padding:5px}
</style>
<script type="text/javascript">
/*$(function(){//显示Input类型元素的总数量
$("#form1 div").html("表单共找出Input类型元素:"+$("#form1 :input").length);
$("#form1 div").addClass("div");
})
$(function(){//显示所有文本框对象
$("#form1 :text").show(3000);
})
$(function(){//显示所有密码框对象
$("#form1 :password").show(3000);
})
$(function(){//显示单选按钮对象
$("#form1 :radio").show(3000);
$("#form1 #Span1").show(3000);
})
$(function(){//显示所有复选框对象
$("#form1 :checkbox").show(3000);
$("#form1 #Span2").show(3000);
})
$(function(){//显示所有提交按钮对象
$("#form1 :submit").show(3000);
})
$(function(){//显示所有图片域对象
$("#form1 :image").show(3000);
})
$(function(){//显示所有重置按钮对象
$("#form1 :reset").show(3000);
})
$(function(){//显示所有按钮对象
$("#form1 :button").show(3000);
})*/
$(function(){//显示所有文件域对象
$("#form1 :file").show(3000);
})
</script>
</head>
<body>
<form id="form1">
<textarea class="txt">TextArea</textarea>
<select><option value="0">Item 0</option></select>
<input type="text" value="Text" class="txt">
<input type="password" value="PassWord" class="txt">
<input type="radio"><span id="Span1">Radio</span></input>
<input type="checkbox"/>
<span id="Span2">Checkbox</span>
<input type="submit" value="Submit" class="btn"/>
<input type="image" title="Image" src="../images/twowayAdvertTest/logo_jquery.gif" class="img"/>
<input type="reset" value="Reset" class="btn"/>
<input type="button" value="Button" class="btn"/>
<input type="file" title="File" class="txt"/>
<div id="divShow"></div>
</form>
</body>
</html>
表 2-18 页面执行效果
关键代码 | 功能描述 | 页面效果 |
$("#form1 div").html("表单共找出Input类型元素:"+$("#form1 :input").length); | 显示input类型元素的总数量 | |
$("#form1 :text").show(3000); | 显示所有文本框对象 | |
$("#form1 :password").show(3000); | 显示所有密码框对象 | |
$("#form1 :radio").show(3000); | 显示所有单选按钮 | |
$("#form1 :checkbox").show(3000); | 显示所有复选框对象 | |
$("#form1 :submit").show(3000); | 显示所有提交按钮对象 | |
$("#form1 :image").show(3000); | 显示所有图片域钮对象 | |
$("#form1 :reset").show(3000); | 显示所有重置按钮对象 | |
$("#form1 :button").show(3000); | 显示所有按钮对象 | |
$("#form1 :file").show(3000); | 显示所有文件域对象 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>导航条在项目中的应用</title>
<script language="javascript" type="text/javascript" src="../jquery/jquery-1.4.2.min.js"></script>
<style type="text/css">
body{font-size:13px}
#divFrame{border:solid 1px #666;width:301px;overflow:hidden}
#divFrame .clsHead{background-color:#eee;padding:8px;height:18px;cursor:hand}
#divFrame .clsHead h3{padding:0px;margin:0px;float:left}
#divFrame .clsHead span{float:right;margin-top:3px}
#divFrame .clsContent{padding:8px}
#divFrame .clsContent ul {list-style-type:none;margin:0px;padding:0px}
#divFrame .clsContent ul li{ float:left;width:95px;height:23px;line-height:23px}
#divFrame .clsBot{float:right;padding-top:5px;padding-bottom:5px}
.GetFocus{background-color:#eee}
</style>
<script type="text/javascript">
$(function(){//页面加载事件
$(".clsHead").click(function(){//图片单击事件
if($(".clsContent").is(":visible")){//如果内容可见
$(".clsHead span img").attr("src","../images/2/a1.gif");//改变图片
//隐藏内容
$(".clsContent").css("display","none");
}else{
$(".clsHead span img").attr("src","../images/2/a2.gif");//改变图片
//显示内容
$(".clsContent").css("display","block");
}
});
$(".clsBot > a").click(function(){//热点链接单击事件
//如果内容为"简化"字样
if($(".clsBot > a").text()=="简化"){
//隐藏index号大于4且不是最后一项的元素
$("ul li:gt(4):not(:last)").hide();
//将字符内容改为"更多"
$(".clsBot > a").text("更多");
}else{
$("ul li:gt(4):not(:last)").show().addClass("GetFocus");//显示所选元素且增加样式
//将字符内容更改为"简化"
$(".clsBot > a").text("简化")
}
});
})
</script>
</head>
<body>
<div id="divFrame">
<div class="clsHead">
<h3>图书分类</h3>
<span><img src="../images/2/a2.gif" alt=""/></span>
</div>
<div class="clsContent">
<ul>
<li><a href="#">小说</a><li>(1110)</li></li>
<li><a href="#">文艺</a><li>(230)</li></li>
<li><a href="#">青春</a><li>(1430)</li></li>
<li><a href="#">少儿</a><li>(1560)</li></li>
<li><a href="#">生活</a><li>(870)</li></li>
<li><a href="#">社科</a><li>(1460)</li></li>
<li><a href="#">管理</a><li>(1450)</li></li>
<li><a href="#">计算机</a><li>(1780)</li></li>
<li><a href="#">教育</a><li>(930)</li></li>
<li><a href="#">工具书</a><li>(3450)</li></li>
<li><a href="#">引进版</a><li>(980)</li></li>
<li><a href="#">其他类</a><li>(3230)</li></li>
</ul>
</div>
<div class="clsBot">
<a href="#">简化</a>
<img src="../images/2/a5.gif" alt=""/>
</div>
</div>
</body>
</html>
$(".clsContent").css("display","none");
$(".clsHead span img").attr("src","../images/2/a2.gif");
if($(".clsContent").is(":visible")){
其中“$(".clsContent”)获取内容元素,“is”是判断,":visible"表示是否可见,此行代码用于判断内容元素是否可见,它返回一个布尔值,如果为true,则执行if后面的语句块,否则执行else后面的语句块
if($(".clsBot > a").text()=="简化"){
其中$(".clsBot > a")获取超链接元素,text()是jQuery中一个获取元素内容的函数。
其中,“ul li”获取元素,":gt(4)"和":not(:last)"分别为两个并列的过滤选择条件,前者表示Index号大于4,后者表示不是最后一个元素,二者组合成一个过滤条件,即选Index号大于4并且不是最后一个的元素集合;hide()是jQuery中一个隐藏元素的函数。$("ul li:gt(4):not(:last)").hide();