jQuery中的事件,jQuery搜索框自动补全功用插件完成

by admin on 2019年3月15日

方今用nodeclub完毕股票的输加入关贸总协定组织键字自动补全股票音讯实行搜索效果,原先用jQuery-ui,结果jQuery-ui库太大,所以考虑用别的插件,最终甄选使用autocomplete.js,控件不难用着方便。留下记录

登录界面 login.jsp:

第⑥章 jQuery中的事件,第⑥章jQuery事件

  1.加载DOM

  jQuery中,在$(document).ready()方法内注册的事件,只要DOM就绪就会被实施,此时或然成分的关系文件未下载完。

  jQuery中的
load()方法,会在要素的onload事件中绑定三个处理函数。比如$(window).load(function(){…}),等价于JavaScript中的window.onload=function(){…},该措施供给等网页全部因素都加载完(包涵管理文件)。

  2.风波绑定

  在文档装载完事后,能够为因素绑定事件来达成都部队分操作。可以接纳bind()方法来对匹配成分实行一定的轩然大波绑定。

  语法: bind(type,[data],fn);

home88一必发 1<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-2-3</title> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <link rel=”stylesheet”
type=”text/css” href=”../../css/style.css” /> <script
type=”text/javascript”> $(function(){ $(“#panel
h5.head”).bind(“click”,function(){ var $content = $(this).next();
if($content.is(“:visible”)){ $content.hide(); }else{ $content.show(); }
}) }) </script> </head> <body> <div id=”panel”>
<h5 class=”head”>什么是jQuery?</h5> <div
class=”content”>
jQuery是继Prototype之后又二个卓越的JavaScript库,它是二个由 John Resig
创造于2007年5月的开源项目。jQuery凭借简洁的语法和跨平台的包容性,相当的大地简化了JavaScript开发人士遍历HTML文书档案、操作DOM、处总管件、执行动画和开销Ajax。它非凡而又优雅的代码风格改变了JavaScript程序员的布置性思路和编写程序的章程。
</div> </div> </body> </html> View Code
home88一必发 2<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-2-4</title> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <link rel=”stylesheet”
type=”text/css” href=”../../css/style.css” /> <script
type=”text/javascript”> $(function(){ $(“#panel
h5.head”).bind(“mouseover”,function(){ $(this).next().show(); });
$(“#panel h5.head”).bind(“mouseout”,function(){ $(this).next().hide();
}) }) </script> </head> <body> <div id=”panel”>
<h5 class=”head”>什么是jQuery?</h5> <div
class=”content”>
jQuery是继Prototype之后又四个佳绩的JavaScript库,它是2个由 John Resig
创造于二零零七年十一月的开源项目。jQuery凭借简洁的语法和跨平台的包容性,非常大地简化了JavaScript开发人士遍历HTML文书档案、操作DOM、处管事人件、执行动画和开发Ajax。它非凡而又优雅的代码风格改变了JavaScript程序员的规划思路和编写程序的措施。
</div> </div> </body> </html> View Code

  3.合成事件

  jQuery中有一个合成事件,hover()方法与toggle()方法。

  hover() 语法:hover(enter,leave);  用来效仿光标悬停事件。

home88一必发 3<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-3-1</title> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <link rel=”stylesheet”
type=”text/css” href=”../../css/style.css” /> <script
type=”text/javascript”> $(function(){ $(“#panel
h5.head”).hover(function(){ $(this).next().show(); },function(){
$(this).next().hide(); }) }) </script> </head> <body>
<div id=”panel”> <h5 class=”head”>什么是jQuery?</h5>
<div class=”content”>
jQuery是继Prototype之后又3个卓越的JavaScript库,它是四个由 John Resig
成立于二〇〇六年四月的开源项目。jQuery凭借简洁的语法和跨平台的包容性,十分的大地简化了JavaScript开发人士遍历HTML文书档案、操作DOM、处总管件、执行动画和付出Ajax。它独特而又优雅的代码风格改变了JavaScript程序员的宏图思路和编写程序的办法。
</div> </div> </body> </html> View Code

  toggle()语法:toggle(fn1,fn2,…fnN); 用来效仿鼠标三番五次单击事件。

home88一必发 4<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-3-3</title> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <link rel=”stylesheet”
type=”text/css” href=”../../css/style.css” /> <script
type=”text/javascript”> $(function(){ $(“#panel
h5.head”).toggle(function(){ $(this).next().toggle(); },function(){
$(this).next().toggle(); }) }) /*$(function(){ $(“#panel
h5.head”).click(function(){ $(this).next().toggle(); }) })*/
</script> </head> <body> <div id=”panel”> <h5
class=”head”>什么是jQuery?</h5> <div class=”content”>
jQuery是继Prototype之后又一个特出的JavaScript库,它是一个由 John Resig
创建于2005年八月的开源项目。jQuery凭借简洁的语法和跨平台的包容性,非常的大地简化了JavaScript开发职员遍历HTML文档、操作DOM、处管事人件、执行动画和支出Ajax。它特殊而又优雅的代码风格改变了JavaScript程序员的安插思路和编写程序的不二法门。
</div> </div> </body> </html> View Code

  4.轩然大波冒泡

  意思便是说,页面上有多少个要素响应同2个事件。事件会依照DOM的层次结构像水泡一样不断往上至顶。

home88一必发 5<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-4-1</title> <style type=”text/css”> * {
margin: 0; padding: 0; } body { font-size: 13px; line-height: 130%;
padding: 60px; } #content { width: 220px; border: 1px solid #0050D0;
background: #96E555; } span { width: 200px; margin: 10px; background:
#666666; cursor: pointer; color: white; display: block; } p { width:
200px; background: #888; color: white; height: 16px; } </style>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { // 为span成分绑定click事件
$(‘span’).bind(“click”, function () { var txt = $(‘#msg’).html() +
“<p>内层span成分被点击.<p/>”; $(‘#msg’).html(txt); }); //
为div成分绑定click事件 $(‘#content’).bind(“click”, function () { var
txt = $(‘#msg’).html() + “<p>外层div成分被点击.<p/>”;
$(‘#msg’).html(txt); }); // 为body成分绑定click事件
$(“body”).bind(“click”, function () { var txt = $(‘#msg’).html() +
“<p>body成分被点击.<p/>”; $(‘#msg’).html(txt); }); })
</script> </head> <body> <div id=”content”>
外层div元素 <span>内层span元素</span> 外层div元素
</div> <div id=”msg”> </div> </body>
</html> View Code

  甘休冒泡

home88一必发 6<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Panel</title> <style type=”text/css”> * {
margin: 0; padding: 0; } body { font-size: 13px; line-height: 130%;
padding: 60px; } #content { width: 220px; border: 1px solid #0050D0;
background: #96E555; } span { width: 200px; margin: 10px; background:
#666666; cursor: pointer; color: white; display: block; } p { width:
200px; background: #888; color: white; height: 16px; } </style>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { // 为span成分绑定click事件
$(‘span’).bind(“click”, function (event) { var txt = $(‘#msg’).html() +
“<p>内层span成分被点击.<p/>”; $(‘#msg’).html(txt);
event.stopPropagation(); // 阻止事件冒泡 }); // 为div成分绑定click事件
$(‘#content’).bind(“click”, function (event) { var txt =
$(‘#msg’).html() + “<p>外层div成分被点击.<p/>”;
$(‘#msg’).html(txt); event.stopPropagation(); // 阻止事件冒泡 }); //
为body成分绑定click事件 $(“body”).bind(“click”, function () { var txt =
$(‘#msg’).html() + “<p>body成分被点击.<p/>”;
$(‘#msg’).html(txt); }); }) </script> </head> <body>
<div id=”content”> 外层div元素
<span>内层span元素</span> 外层div元素 </div> <div
id=”msg”> </div> </body> </html> View Code
home88一必发 7<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-4-4</title> <style type=”text/css”> * {
margin: 0; padding: 0; } body { font-size: 13px; line-height: 130%;
padding: 60px; } #content { width: 220px; border: 1px solid #0050D0;
background: #96E555; } span { width: 200px; margin: 10px; background:
#666666; cursor: pointer; color: white; display: block; } p { width:
200px; background: #888; color: white; height: 16px; } </style>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { // 为span成分绑定click事件
$(‘span’).bind(“click”, function (event) { var txt = $(‘#msg’).html() +
“<p>内层span成分被点击.<p/>”; $(‘#msg’).html(txt); return
false; }); // 为div成分绑定click事件 $(‘#content’).bind(“click”,
function (event) { var txt = $(‘#msg’).html() +
“<p>外层div成分被点击.<p/>”; $(‘#msg’).html(txt); return
false; }); // 为body成分绑定click事件 $(“body”).bind(“click”, function
() { var txt = $(‘#msg’).html() + “<p>body成分被点击.<p/>”;
$(‘#msg’).html(txt); }); }) </script> </head> <body>
<div id=”content”> 外层div元素
<span>内层span元素</span> 外层div元素 </div> <div
id=”msg”> </div> </body> </html> View Code

  阻止暗许行为

home88一必发 8<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title></title> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(“#sub”).bind(“click”,
function (event) { var username = $(“#username”).val(); //获取成分的值
if (username == “”) { //判断值是还是不是为空
$(“#msg”).html(“<p>文本框的值不可能为空.</p>”); //提醒新闻event.preventDefault(); //阻止暗中同意行为 ( 表单提交 ) } }) })
</script> </head> <body> <form
action=”test.html”> 用户名:<input type=”text” id=”username” />
<br /> <input type=”submit” value=”提交” id=”sub” />
</form> <div id=”msg”> </div> </body>
</html> View Code
home88一必发 9<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title></title> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(“#sub”).bind(“click”,
function (event) { var username = $(“#username”).val(); //获取成分的值
if (username == “”) { //判断值是不是为空
$(“#msg”).html(“<p>文本框的值无法为空.</p>”); //提示消息return false; } }) }) </script> </head> <body>
<form action=”test.html”> 用户名:<input type=”text”
id=”username” /> <br /> <input type=”submit” value=”提交”
id=”sub” /> </form> <div id=”msg”> </div>
</body> </html> View
Code

  5.事变指标的性质

  jQuery对事件指标常用的属性举行了打包。

  (1)event.type 可以取得事件的花色

home88一必发 10<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head>
<title></title> <meta http-equiv=”Content-Type”
content=”text/html; charset=utf-8″ /> <!– 引入jQuery –>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script> $(function () {
$(“a”).click(function (event) { alert(event.type); //获取事件类型 return
false; //阻止链接跳转 }); }) </script> </head> <body>
<a href=’jQuery中的事件,jQuery搜索框自动补全功用插件完成。 me .</a> </body>
</html> View Code

  (2)event.target 能够取得出发事件的成分

home88一必发 11<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head>
<title></title> <meta http-equiv=”Content-Type”
content=”text/html; charset=utf-8″ /> <!– 引入jQuery –>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script> $(function () {
$(“a[href= (event) {
alert(event.target.href); //获取触发事件的<a>成分的href属性值
return false; //阻止链接跳转 }); }) </script> </head>
<body> <a href=’ me .</a>
</body> </html> View
Code

  (3)event.pageX和event.pageY 能够赢得光标相对于页面包车型大巴x坐标与y坐标。

home88一必发 12<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head>
<title></title> <meta http-equiv=”Content-Type”
content=”text/html; charset=utf-8″ /> <!– 引入jQuery –>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script> $(function () {
$(“a”).click(function (event) { alert(“Current mouse position: ” +
event.pageX + “, ” + event.pageY); //获取鼠标当前相对于页面包车型地铁坐标 return
false; //阻止链接跳转 }); }) </script> </head> <body>
<a href=’ me .</a> </body>
</html> View Code

  (4)event.which
能够在鼠标单击事件中拿走鼠标的左中右键,也得以博得键盘键。

home88一必发 13jQuery中的事件,jQuery搜索框自动补全功用插件完成。<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head>
<title></title> <meta http-equiv=”Content-Type”
content=”text/html; charset=utf-8″ /> <!– 引入jQuery –>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script> $(function(){
$(“a”).mousedown(function(e){ alert(e.which) // 1 = 鼠标左键 left; 2 =
鼠标中键; 3 = 鼠标右键 return false;//阻止链接跳转 }) }) </script>
</head> <body> <a href=’ me
.</a> </body> </html> View Code
home88一必发 14<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head>
<title></title> <meta http-equiv=”Content-Type”
content=”text/html; charset=utf-8″ /> <!– 引入jQuery –>
<script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script> $(function () {
$(“input”).keyup(function (e) { alert(e.which); }) }) </script>
</head> <body> <input /> </body> </html>
View Code

  6.移除事件

  unbind([type],[data])方法用来移除事件。

home88一必发 15<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-6-2</title> <style type=”text/css”> * {
margin: 0; padding: 0; } body { font-size: 13px; line-height: 130%;
padding: 60px; } p { width: 200px; background: #888; color: white;
height: 16px; } </style> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(‘#btn’).bind(“click”,
function () { $(‘#test’).append(“<p>小编的绑定函数1</p>”);
}).bind(“click”, function () {
$(‘#test’).append(“<p>作者的绑定函数2</p>”); }).bind(“click”,
function () { $(‘#test’).append(“<p>作者的绑定函数3</p>”);
}); $(‘#delAll’).click(function () { $(‘#btn’).unbind(“click”); }); })
</script> </head> <body> <button id=”btn”>
点击笔者</button> <div id=”test”> </div> <button
id=”delAll”> 删除全部事件</button> </body> </html>
View Code
home88一必发 16<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Panel</title> <style type=”text/css”> * {
margin: 0; padding: 0; } body { font-size: 13px; line-height: 130%;
padding: 60px; } p { width: 200px; background: #888; color: white;
height: 16px; } </style> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(‘#btn’).bind(“click”,
myFun1 = function () {
$(‘#test’).append(“<p>笔者的绑定函数1</p>”); }).bind(“click”,
myFun2 = function () {
$(‘#test’).append(“<p>笔者的绑定函数2</p>”); }).bind(“click”,
myFun3 = function () {
$(‘#test’).append(“<p>笔者的绑定函数3</p>”); });
$(‘#delTwo’).click(function () { $(‘#btn’).unbind(“click”, myFun2);
}); }) </script> </head> <body> <button
id=”btn”> 点击作者</button> <div id=”test”> </div>
<button id=”delTwo”> 删除第一个事件</button> </body>
</html> View Code

  one(type,[data],fn)方法能够为成分绑定处理函数,当处理函数触发二回后及时删除。

home88一必发 17<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>4-6-4</title> <style type=”text/css”> * {
margin: 0; padding: 0; } body { font-size: 13px; line-height: 130%;
padding: 60px; } p { width: 200px; background: #888; color: white;
height: 16px; } </style> <script
src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(‘#btn’).one(“click”,
function () { $(‘#test’).append(“<p>笔者的绑定函数1</p>”);
}).one(“click”, function () {
$(‘#test’).append(“<p>作者的绑定函数2</p>”); }).one(“click”,
function () { $(‘#test’).append(“<p>笔者的绑定函数3</p>”);
}); }) </script> </head> <body> <button
id=”btn”> 点击作者</button> <div id=”test”> </div>
</body> </html> View
Code

  7.模仿操作

  jQuery中能够利用trigger()方法成功臣模范拟操作。

home88一必发 18<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title></title> <style type=”text/css”> * { margin:
0; padding: 0; } body { font-size: 13px; line-height: 130%; padding:
60px; } p { width: 200px; background: #888; color: white; height: 16px;
} </style> <script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(‘#btn’).bind(“click”,
function () { $(‘#test’).append(“<p>我的绑定函数1</p>”);
}).bind(“click”, function () {
$(‘#test’).append(“<p>作者的绑定函数2</p>”); }).bind(“click”,
function () { $(‘#test’).append(“<p>小编的绑定函数3</p>”);
}); $(‘#btn’).trigger(“click”); }) </script> </head>
<body> <button id=”btn”> 点击我</button> <div
id=”test”> </div> </body> </html> View Code
home88一必发 19<!DOCTYPE
html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <meta
http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title></title> <style type=”text/css”> * { margin:
0; padding: 0; } body { font-size: 13px; line-height: 130%; padding:
60px; } p { width: 200px; background: #888; color: white; height: 16px;
} </style> <script src=”../../scripts/jquery-1.3.1.js”
type=”text/javascript”></script> <script
type=”text/javascript”> $(function () { $(‘#btn’).bind(“myClick”,
function (event, message1, message2) { $(‘#test’).append(“<p>” +
message1 + message2 + “</p>”); }); $(‘#btn’).click(function () {
$(this).trigger(“myClick”, [“作者的自定义”, “事件”]);
}).trigger(“myClick”, [“小编的自定义”, “事件”]); }) </script>
</head> <body> <button id=”btn”> 点击我</button>
<div id=”test”> </div> </body> </html> View Code

 PS:参考文献《锋利的jQuery》

详解Jquery 选择器,详解jquery选择器

1   概述

 本篇小说为穿插小说,ASP.NET MVC种类如今写了如下几篇:

  • 详解google Chrome浏览器(理论篇)
  • 详解Google Chrome浏览器(操作篇)(上)
  • 详解Google Chrome浏览器(操作篇)(下)
  • .NET 开发环境搭建
  • 详解ASP.NET MVC 路由
  • 详解ASP.NET MVC 控制器

写该篇小说重要目的是为接下去的ASP.NET MVC
类别,如页面之间传值,ADO.NET和EF,Bootstrap等打基础,本篇作品相比基础,但正如健全,卓殊适合基础飞快扫除文盲。

2   基本选取器

2.1  一览表

home88一必发 20

2.2  示例代码

 (1)id选择器

将id为lastname成分的背景观设置为天蓝

home88一必发 21 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“#lastname”).css(“background-color”, “#0000ff”); 11 12 }); 13 14
</script> 15 </head> 16 <body> 17 <div
id=”lastname”>id为lastname的采纳器</div> 18 </body> 19
</html> View Code

(2)类选取器

将class为intro成分的背景观设置为铁黄

home88一必发 22 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“.intro”).css(“background-color”, “#0000ff”); 11 12 }); 13 14
</script> 15 </head> 16 <body> 17 <div
class=”intro”>div选用器测试</div> 18 <p
class=”intro”>p测试选取器</p> 19 </body> 20 </html>
View Code

(3)成分选拔器

将p元素的背景象设置为深橙

home88一必发 23 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“p”).css(“background-color”, “#0000ff”); 11 12 }); 13 14
</script> 15 </head> 16 <body> 17
<p>p测试选拔器</p> 18 </body> 19 </html> View Code

(4)全部选用器

遍历body下的拥有因素,将其背景观设置为朱红

home88一必发 24 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10 $(“body
*”).css(“background-color”, “#0000ff”); 11 12 }); 13 14
</script> 15 </head> 16 <body> 17
<div>选用器测试</div> 18 <p>p成分</p> 19
</body> 20 </html> View
Code

(5)并列采纳器

 将成分p和因素div背景观设置为黄铜色

home88一必发 25 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“p,div”).css(“background-color”, “#0000ff”); 11 12 }); 13 14
</script> 15 </head> 16 <body> 17
<div>选取器测试</div> 18 <p>p成分</p> 19
</body> 20 </html> View
Code

3   层次选拔器

3.1 一览表

home88一必发 26

3.2 示例代码

(1)parent>child(直系子元素,即间接下一代成分)

设置div成分的首先代成分为span的因素的背景象为普鲁士蓝

home88一必发 27 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div>span”).css(“background-color”, “#0000ff”); 11 12 }); 13 14
</script> 15 </head> 16 <body> 17 <div> 18
<span>DOM树,DIV第一代</span> 19 <p> 20
<span>DOM树,第二代</span> 21 </p> 22
<span>DOM树,DIV第一代</span> 23 </div> 24
</body> 25 </html> View
Code

测试结果:

home88一必发 28

结果分析:根据如上代码画出的DOM树如下,能够很明显看出,DIV有多个平昔孩子,即首先代span,p,span,代码中div>span,表示div下的一向第2代span,由此,测试结果就简单精通了。

home88一必发 29

(2)prev+next(prev成分的下2个兄弟成分,等同于next()方法)

安装类为intro成分的下三个兄弟成分背景观为浅绿

home88一必发 30 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“.intro+div”).css(“background-color”, “#0000ff”); 11
//$(“.intro”).next(“div”).css(“background-color”, “#0000ff”); 12 13 });
14 15 </script> 16 </head> 17 <body> 18
<div>1</div> 19 <p class=”intro”>2</p> 20
<div>3</div> 21 <div>4</div> 22 <span
class=”item”>5</span> 23 <div>6</div> 24
</body> 25 </html> View
Code

测试结果:

home88一必发 31

结果分析:依照如上代码画出DOM树如下图,测试结果肯定。

home88一必发 32

(3)prev~siblings(prev成分的全体兄弟成分,等同于nextAll()方法)

 设置类为intro成分之后的所有兄弟成分为div元素的背景象为浅米灰

home88一必发 33 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“.intro~div”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G0</div> 17 <div class=”intro”>G1</div> 18
<div>G2</div> 19 <span>G3</span> 20
<div>G4</div> 21 </body> 22 </html> View Code

测试结果:

home88一必发 34

分析测试结果:依据如上代码画出DOM树如下图,测试结果肯定。

home88一必发 35

4   过滤选拔器

4.1 基本过滤选拔器

4.1.1  一览表

home88一必发 36

4.1.2  代码示例

(1):first(采纳第一个成分)

home88一必发 37 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“span:first”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<span>G1</span> 17 <span>G2</span> 18
<span>G3</span> 19 </body> 20 </html> View Code

测试结果:

home88一必发 38

(2):last(选拔最终2个成分)

home88一必发 39 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“span:last”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<span>G1</span> 17 <span>G2</span> 18
<span>G3</span> 19 </body> 20 </html> View Code

测试结果:

home88一必发 40

(3):not(取非成分)

home88一必发 41 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:not(.wrap)”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G1</div> 17 <div class=”wrap”>G2</div> 18
</body> 19 </html> View
Code

不过,请留意上面包车型客车代码:当G1所在div和G2所在div是父子关系时,G1和G2都会变色。

home88一必发 421
<div> 2     G1    <div class=”wrap”>G2</div> 3
</div> View Code

(4):even(索引为偶数,索引 index从0先导)

home88一必发 43 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:even”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G1</div> 17 <div>G2</div> 18
<div>G3</div> 19 <div>G4</div> 20 </body>
21 </html> View Code

测试结果:

home88一必发 44

(5):odd(索引为奇数,索引 index从0开头)

home88一必发 45 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:odd”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G1</div> 17 <div>G2</div> 18
<div>G3</div> 19 <div>G4</div> 20 </body>
21 </html> View Code

测试结果:

home88一必发 46

(6):eq(x)(取钦命索引的因素,x为从0开首的目录)

安装索引为2的div成分背景为灰绿

home88一必发 47 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:eq(2)”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G1</div> 17 <div>G2</div> 18
<div>G3</div> 19 <div>G4</div> 20 </body>
21 </html> View Code

测试结果:

home88一必发 48

(7):lt(x))(取小于钦赐索引的因素,x为从0开端的目录)

home88一必发 49 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:lt(2)”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G1</div> 17 <div>G2</div> 18
<div>G3</div> 19 <div>G4</div> 20 </body>
21 </html> View Code

测试结果:

home88一必发 50

(8):gt(x))(取大于钦定索引的要素,x为从0开始的目录)

home88一必发 51 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:gt(2)”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<div>G1</div> 17 <div>G2</div> 18
<div>G3</div> 19 <div>G4</div> 20 </body>
21 </html> View Code

测试结果:

home88一必发 52

(8):header(取h1-h6标题成分)

home88一必发 53 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“:header”).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 </head> 15 <body> 16
<h1>测试H1标题</h1> 17 <div>G1</div> 18
<div>G2</div> 19 <div>G3</div> 20
<h2>测试h2标题</h2> 21 <h3>测试h3标题</h3> 22
<h4>测试h4标题</h4> 23 <div>G4</div> 24
<h5>测试h5标题</h5> 25 <h6>测试h6标题</h6> 26
</body> 27 </html> View
Code

测试结果:

home88一必发 54

(9):animated(全体动画成分)

home88一必发 55 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10 function
aniDiv() { 11 $(“#box”).animate({ width: 300 }, “slow”); 12
$(“#box”).animate({ width: 100 }, “slow”, aniDiv); 13 } 14 aniDiv(); 15
$(“.btn1”).click(function () { 16 $(“:animated”).css(“background-color”,
“#0000ff”); 17 }); 18 }); 19 20 </script> 21 <style> 22 div
{ 23 background: #98bf21; 24 height: 40px; 25 width: 100px; 26
position: relative; 27 margin-bottom: 5px; 28 } 29 </style> 30
</head> 31 <body> 32 <div></div> 33 <div
id=”box”></div> 34 <div></div> 35 <button
class=”btn1″>Mark animated element</button> 36 </body> 37
</html> View Code

测试结果:

 home88一必发 56

4.2 内容过滤采纳器

4.2.1 一览表

home88一必发 57

4.2.2 示例代码

(1):contains(text)(取包蕴text文本的要素)

home88一必发 58 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div:contains(“G2”)’).css(“background-color”, “#0000ff”); 11 }); 12
13 </script> 14 15 </head> 16 <body> 17
<div>G1</div> 18 <div>G2</div> 19
<div>G3</div> 20 </body> 21 </html> View Code

测试结果:

home88一必发 59

(2):empty(取不含有子成分或文本为空的因素)

home88一必发 60 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div:empty’).html(‘没有内容’); 11 }); 12 13 </script> 14 15
</head> 16 <body> 17 <div>G1</div> 18
<div>G2</div> 19 <div>G3</div> 20
<div></div> 21 </body> 22 </html> View Code

测试结果:

home88一必发 61

(3) :has(selector)(取选拔器匹配的要素)

纵使span不是div的直系子成分,也会收效

home88一必发 62 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10 //
为含有span元素的div添加边框 11 $(‘div:has(span)’).css(‘border’, ‘1px
solid #000’); 12 }); 13 14 </script> 15 16 </head> 17
<body> 18 <div> 19 <h2> 20
A     <span>B</span> 21 </h2> 22 </div> 23
</body> 24 </html> View
Code

测试结果:

home88一必发 63

(4):parent(取包涵子元素或文本的因素)

home88一必发 64 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10 $(‘ol
li:parent’).css(‘border’, ‘1px solid #000’); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17 <ol> 18
<li></li> 19 <li>A</li> 20 <li></li>
21 <li>D</li> 22 </ol> 23 </body> 24
</html> View Code

测试结果:

home88一必发 65

4.3 可知性过滤选拔器

4.3.1 一览表

home88一必发 66

4.3.2 示例代码

(1):hidden(取不可知的成分)

匹配display:none,<input type=”hidden”
/>,visibility:hidden,capacity:0元素

home88一必发 67 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div:hidden’).show(500); 11 alert($(‘input:hidden’).val()); 12 }); 13
</script> 14 <style type=”text/css”> 15 div         16 { 17
margin: 10px; 18 width: 200px; 19 height: 40px; 20 border: 1px solid
#FF0000; 21 display:block; 22 } 23 24 .hid-1         25 { 26 display:
none; 27 } 28 29 .hid-2         30 { 31 visibility: hidden; 32 } 33 34
</style> 35 </head> 36 <body> 37 <div
class=”hid-1″>display: none</div> 38 <div
class=”hid-2″>visibility: hidden</div> 39 <input
type=”hidden” value=”hello” /> 40 </body> 41 </html>
View Code

测试结果:

home88一必发 68

(2):visible(取可知的要素)

home88一必发 69 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div:visible’).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17 <div
style=”display:none”>G1</div> 18 <div>G2</div> 19
<div style=”display:none”>G3</div> 20
<div>G4</div> 21 22 </body> 23 </html> 24 25 26
View Code

测试结果:

home88一必发 70

4.4 属性过滤选拔器

4.4.1 一览表

home88一必发 71

4.4.2 代码示例

(1)[attribute](取拥有attribute属性的因素)

home88一必发 72 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div[class]home88一必发,’).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17
<div>G1</div> 18 <div>G2</div> 19
<div>G3</div> 20 <div class=”div4″>G4</div> 21
22 </body> 23 </html> 24 25 26 View Code

测试结果:

home88一必发 73

(2)[attribute = value](取attribute属性值等于value)

home88一必发 74 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div[class=div3]’).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17
<div>G1</div> 18 <div>G2</div> 19 <div
class=”div3″>G3</div> 20 <div class=”div4″>G4</div>
21 22 </body> 23 </html> 24 25 26 View Code

测试结果:

home88一必发 75

(3) [attribute != value](取attribute属性值不等于value的要素)

home88一必发 76 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div[class!=div3]’).css(“background-color”, “#0000ff”); 11 }); 12
13 </script> 14 15 </head> 16 <body> 17
<div>G1</div> 18 <div>G2</div> 19 <div
class=”div3″>G3</div> 20 <div class=”div4″>G4</div>
21 22 </body> 23 </html> 24 25 26 View Code

测试结果:

home88一必发 77

(4)[attribute $= value](attribute属性值以value结束)

home88一必发 78 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘[id$=div]’).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17 <div
id=”first_div”>G1</div> 18 <div
id=”second_div”>G2</div> 19 <div class=”div3″
title=”3div”>G3</div> 20 <div class=”div4″>G4</div>
21 22 </body> 23 </html> View Code

测试结果:

home88一必发 79

(5))[attribute^= value](attribute属性值以value起初)

home88一必发 80 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘[id^=first]’).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17 <div
id=”first_div”>G1</div> 18 <div
id=”second_div”>G2</div> 19 <div class=”div3″
title=”3div”>G3</div> 20 <div class=”div4″>G4</div>
21 22 </body> 23 </html> 24 25 26 View Code

测试结果:

home88一必发 81

(6)[attribute *= value](attribute属性值包蕴value值)

home88一必发 82 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘[id*=first]’).css(“background-color”, “#0000ff”); 11 }); 12 13
</script> 14 15 </head> 16 <body> 17 <div
id=”first_div”>G1</div> 18 <div
id=”second_div”>G2</div> 19 <div class=”div3″
title=”3div”>G3</div> 20 <div class=”div4″>G4</div>
21 22 </body> 23 </html> View Code

测试结果:

home88一必发 83

诠释:在性质选拔器中,^$符号和正则表达式的开首终结符号表示的意义是如出一辙的,*混淆匹配,类似于sql中的like
‘%str%’。

(7)[selector1][selector2](复合型属性过滤器,同时满足多个标准)

home88一必发 84 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘div[class=div3][title=3div]’).css(“background-color”,
“#0000ff”); 11 }); 12 13 </script> 14 15 </head> 16
<body> 17 <div>G1</div> 18 <div>G2</div>
19 <div class=”div3″ title=”3div”>G3</div> 20 <div
class=”div4″>G4</div> 21 22 </body> 23 </html> 24
25 26 View Code

测试结果:

home88一必发 85

4.5 子成分过滤选拔器

4.5.1 一览表

home88一必发 86

4.5.2 代码示例

(1)first-child(表示非凡的首先个因素)和last-child(表示十分的最后1个子元素)

 需求我们留意的是,:fisrst和:last再次来到的都以单个成分,而:first-child和:last-child再次来到的都以集合成分。举个
例子:div:first再次回到的是成套DOM文书档案中率先个div成分,而div:first-child是回来全部div成分下的第一个成分合并后的集
合。

home88一必发 87 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:first-child”).css(“background-color”, “#B2E0FF”); 11
$(“div:last-child”).css(“background-color”, “red”); 12 }); 13
</script> 14 </head> 15 <body> 16 <div> 17
<div>1</div> 18 <div>2</div> 19
<p>3</p> 20 </div> 21 <div>4</div> 22
<div>last</div> 23 </body> 24 </html> View Code

测试结果:

home88一必发 88

(3)only-child(当有些成分有且仅有1个子成分时,:only-child才会收效)

home88一必发 89 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“div:only-child”).css(“background-color”, “red”); 11 }); 12
</script> 13 </head> 14 <body> 15 <div> 16
<div>1</div> 17 <div>2</div> 18
<p>3</p> 19 </div> 20 <div>4</div> 21
<div>last 22 <div>ddd</div> 23 </div> 24
</body> 25 </html> View
Code

测试结果:

home88一必发 90

(4)nth-child

见到那么些就回想英文单词里的,fourth, fifth,
sixth……,nth表示第n个,:nth-child就表示第n个child成分。要留心的是,这儿的n不像eq(x)、gt(x)或lt(x)是从
0早先的,它是从1方始的,英文里好像也平昔不zeroth那样的序号词吗。

:nth-child有二种用法:

1) :nth-child(x),获取第x个子成分
2)
:nth-child(even)和:nth-child(odd),从1始发,获取第偶数个要素或第奇数个要素

3) :nth-child(xn+y),x>=0,y>=0。例如x = 3, y =
0时就是3n,表示取第壹n个要素(n>=0)。实际上xn+y是下边两种的通项式。(当x=0,y>=0时,等同于:hth-
child(x);当x=2,y=0时,等同于nth-child(even);当x=2,y=1时,等同于:nth-child(odd))

4.6 表单对象属性过滤选拔器

4.6.1 一览表

home88一必发 91

4.6.2 代码示例

(1):enabled和:disabled(取可用或不可用成分)

:enabled和:diabled的匹配范围包括input, select, textarea

home88一必发 92 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(‘:enabled’).css(‘border’, ‘1px solid #FF0000’); 11
$(‘:disabled’).css(‘border’, ‘1px solid #0000FF’); 12 }); 13 14
</script> 15 16 </head> 17 <body> 18 <div> 19
<input type=”text” value=”可用的文本框” /> 20 </div> 21
<div> 22 <input type=”text” disabled=”disabled”
value=”不可用的文本框” /> 23 </div> 24 <div> 25
<textarea disabled=”disabled”>不可用的文本域</textarea> 26
</div> 27 <div> 28 <select disabled=”disabled”> 29
<option>English</option> 30
<option>简体中文</option> 31 </select> 32 </div>
33 </body> 34 </html> View Code

测试结果:

home88一必发 93

(2):checked(取选中的单选框或复选框成分)

home88一必发 94 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“.btn1”).click(function () { 11 $(“:checked”).hide(); 12 }); 13 }); 14
</script> 15 16 </head> 17 <body> 18 <input
type=”radio” name=”sex” value=”male” checked=”checked” /> Male 19
<br /> 20 <input type=”radio” name=”sex” value=”female” />
Female 21 <br /> 22 I have a bike: 23 <input type=”checkbox”
name=”vehicle” value=”Bike” /> 24 <br /> 25 I have a car: 26
<input type=”checkbox” name=”vehicle” value=”Car” checked=”checked”
/> 27 <br /> 28 I have an airplane: 29 <input
type=”checkbox” name=”vehicle” value=”Airplane” /> 30 <button
class=”btn1″>Hide Checked Options</button> 31 32 </body>
33 </html> View Code

(3):selected(取下拉列表被选中的因素)

home88一必发 95 1
<html> 2 <head> 3 <script type=”text/javascript”
src=”/jquery/jquery.js”></script> 4 <script
type=”text/javascript”> 5 $(document).ready(function(){ 6
$(“.btn1”).click(function(){ 7 $(“:selected”).hide(); 8 }); 9 }); 10
</script> 11 </head> 12 <body> 13 14 <select
multiple=”multiple”> 15 <option>Volvo</option> 16
<option selected=”selected”>Saab</option> 17
<option>Mercedes</option> 18
<option>Audi</option> 19 </select> 20 <br /> 21
<button class=”btn1″>Hide Selected</button> 22 </body>
23 </html> View Code

5   表单选取器

5.1 一览表

home88一必发 96

5.2 测试代码

(1):input()(选取具有input成分)

home88一必发 97 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“:input”).css(“background-color”, “#B2E0FF”); 11 }); 12
</script> 13 </head> 14 <body> 15 <form
action=””> 16 Name: <input type=”text” name=”user” /> 17 <br
/> 18 Password: <input type=”password” name=”password” /> 19
<br /> 20 <button type=”button”>Useless
Button</button> 21 <input type=”button” value=”Another useless
button” /> 22 <br /> 23 <input type=”reset” value=”Reset”
/> 24 <input type=”submit” value=”Submit” /> 25 <br /> 26
</form> 27 </body> 28 </html> View Code

测试结果:

home88一必发 98

(2):text(选取全部text成分)

home88一必发 99 1
<!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta
name=”viewport” content=”width=device-width” /> 6 <script
src=”~/Scripts/jquery-1.10.2.js”></script> 7
<title>JQuery函数</title> 8 <script
type=”text/javascript”> 9 $(document).ready(function () { 10
$(“:text”).css(“background-color”, “#B2E0FF”); 11 }); 12
</script> 13 </head> 14 <body> 15 <form
action=””> 16 Name: <input type=”text” name=”user” /> 17 <br
/> 18 Password: <input type=”password” name=”password” /> 19
<br /> 20 <button type=”button”>Useless
Button</button> 21 <input type=”button” value=”Another useless
button” /> 22 <br /> 23 <input type=”reset” value=”Reset”
/> 24 <input type=”submit” value=”Submit” /> 25 <br /> 26
</form> 27 </body> 28 </html> View Code

测试结果:

home88一必发 100

(3):select和:button

(4)别的表单成分比较不难,在此不列举。

6   参考文献

【01】

【02】

【03】

【04】

 

接纳器,详解jquery选取器 1 概述
本篇小说为穿插小说,ASP.NET MVC种类如今写了之类几篇: 详解google
Chrome浏览器(理论篇) 详解…

源码如下:

 

jquery中的事件 通过prepend添加的剧情能够得到匹配事件不

你好!
1.
对此prepend()添加的成分,或许动态变化的因素,一般选择live()实行事件钦定;2.
对此元素的toggle()事件,使用live()绑定事件时,能够通过先为其钦命click()事件,然后在click()事件中装置该因素的toggle()事件,最后只要自动触发3遍click()事件就能够了。那么些进度差不多是那样的:live()—>钦点click()事件—>达成该因素的toggle()事件的逻辑(此时click()事件不可能触发toggle()中的任何函数)—>trigger(“click”),触发toggle()中的函数。
$(“#list .message input”).live(“click”,function(){ $(this).toggle(
function(){
$(this).parent(this).after(“<div>评论列表</div>”); },
function(){ $(this).parent(this).next(this).remove(); }
).trigger(‘click’);});
 

 

[java] view
plain copy

jquery 事件中的事件难题

试一下在tr的风云里加3个断定,看<a>有没有主题.有就重临.
 

jQuery中的事件,第肆章jQuery事件 1.加载DOM
jQuery中,在$(document).ready()方法内注册的事件,只要DOM就绪就会被实施,此时恐怕成分的关…

 

 

<!doctype html>
<html lang=”en-US”>
<head>
  <meta http-equiv=”Content-Type”
content=”text/html;charset=utf-8″>
  <title>Input Autocomplete Suggestions Demo</title>
  <link rel=”stylesheet” type=”text/css” media=”all”
href=”style.css”>
  <script type=”text/javascript”
src=”js/codedata.js”></script>
  <script type=”text/javascript”
src=”js/jquery-1.9.1.min.js”></script>
  <script type=”text/javascript”
src=”js/jquery.autocomplete.min.js”></script>
  <script type=”text/javascript”
src=”js/currency-autocomplete.js”></script>
</head>

  1. <%@ page language=”java” import=”java.util.*” contentType=”text/html; charset=utf-8″%>  
  2. <%  
  3.    String path = request.getContextPath();  
  4.    String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  5. %>  
  6. <html>  
  7.     <head>  
  8.         <!– Page title –>  
  9.         <title>imooc – Login</title>  
  10.         <!– End of Page title –>  
  11.         <!– Libraries –>  
  12.         <link type=”text/css” href=”css/login.css” rel=”stylesheet” />      
  13.         <link type=”text/css” href=”css/smoothness/jquery-ui-1.7.2.custom.html” rel=”stylesheet” />     
  14.         <script type=”text/javascript” src=”js/jquery-1.3.2.min.js”></script>  
  15.         <script type=”text/javascript” src=”js/easyTooltip.js”></script>  
  16.         <script type=”text/javascript” src=”js/jquery-ui-1.7.2.custom.min.js”></script>  
  17.         <!– End of Libraries –>   
  18.     </head>  
  19.     <body>  
  20.     <div id=”container”>  
  21.         <div class=”logo”>  
  22.             <a href=”#”><img src=”logo.png” alt=”” /></a>  
  23.         </div>  
  24.         <div id=”box”>  
  25.             <form action=”dologin.jsp” method=”post”>  
  26.             <p class=”main”>  
  27.                 <label>用户名: </label>  
  28.                 <input name=”username” value=”” />   
  29.                 <label>密码: </label>  
  30.                 <input type=”password” name=”password” value=””>    
  31.             </p>  
  32.             <p class=”space”>  
  33.                 <input type=”submit” value=”登录” class=”login” style=”cursor: pointer;”/>  
  34.             </p>  
  35.             </form>  
  36.         </div>  
  37.     </div>  
  38.     </body>  
  39. </html>  

<body>
  <div id=”w”>
    <div id=”content”>
      <h1>World Currencies Autocomplete Search</h1>
      <p>Just start typing and results will be supplied from the
JavaScript. Check out <a
href=”
Autocomplete</a> on Github.</a></p>
      
      <div id=”searchfield”>
        <form><input type=”text” name=”currency”
class=”biginput” id=”autocomplete”></form>
      </div><!– @end #searchfield –>
      
      <div id=”outputbox”>
        <p id=”outputcontent”>Choose a currency and the results
will display here.</p>
      </div>
    </div><!– @end #content –>
  </div><!– @end #w –>
</body>
</html>

对登录的响应 dologin.jsp:

 

 

 

 

具体源码地址  

[java] view
plain copy

 

  1. <%@ page language=”java” import=”java.util.*” contentType=”text/html; charset=utf-8″%>  
  2. <%  
  3.   String path = request.getContextPath();  
  4.   String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  5.   String username =””;  
  6.   String password =””;  
  7.   request.setCharacterEncoding(“utf-8”);//防止汉语乱码  
  8.     
  9.   username = request.getParameter(“username”);  
  10.   password = request.getParameter(“password”);  
  11.     
  12.   //假设用户和密码都等于admin,则登录成功  
  13.   if(“admin”.equals(username)&&”admin”.equals(password))  
  14.   {  
  15.      session.setAttribute(“loginUser”, username);  
  16.      request.getRequestDispatcher(“login_success.jsp”).forward(request, response);  
  17.        
  18.   }  
  19.   else  
  20.   {  
  21.      response.sendRedirect(“login_failure.jsp”);  
  22.   }  
  23. %>  

这边注意的是:

 

 
 request传送音信供给设置编码格局,request.setCharacterEncoding(“utf-8”);

 

   request.getRequestDispatcher("login_success.jsp")取得一个RequestDispatcher,一个请求分发器对象,它的方法forward(request,response)作用是将请求转发到login_success.jsp,request封装了请求信息,response封装了响应信息。注意是forward方式,这种方式只转发请求,不产生新的request对象。如下图:

home88一必发 101

   成功界面:login_success.jsp

[java] view
plain
copy

 

  1. <%@ page language=”java” import=”java.util.*” contentType=”text/html; charset=utf-8″%>  
  2. <%  
  3.    String path = request.getContextPath();  
  4.    String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  5. %>  
  6. <html>  
  7.     <head>  
  8.         <!– Page title –>  
  9.         <title>imooc – Login</title>  
  10.         <!– End of Page title –>  
  11.         <!– Libraries –>  
  12.         <link type=”text/css” href=”css/login.css” rel=”stylesheet” />      
  13.         <link type=”text/css” href=”css/smoothness/jquery-ui-1.7.2.custom.html” rel=”stylesheet” />     
  14.         <script type=”text/javascript” src=”js/jquery-1.3.2.min.js”></script>  
  15.         <script type=”text/javascript” src=”js/easyTooltip.js”></script>  
  16.         <script type=”text/javascript” src=”js/jquery-ui-1.7.2.custom.min.js”></script>  
  17.         <!– End of Libraries –>   
  18.     </head>  
  19.     <body>  
  20.     <div id=”container”>  
  21.         <div class=”logo”>  
  22.             <a href=”#”><img src=”logo.png” alt=”” /></a>  
  23.         </div>  
  24.         <div id=”box”>  
  25.         <%  
  26.             String loginUser = “”;  
  27.             if(session.getAttribute(“loginUser”) != null) {  
  28.                 loginUser = session.getAttribute(“loginUser”).toString();  
  29.             }  
  30.         %>  
  31.               
  32.             欢迎您<font color = “red”> <%=loginUser %></font>,登陆成功!  
  33.         </div>  
  34.     </div>  
  35.     </body>  
  36. </html>  

    login_failure.jsp:

[java] view
plain
copy

 

  1. <%@ page language=”java” import=”java.util.*” contentType=”text/html; charset=utf-8″%>  
  2. <%  
  3.    String path = request.getContextPath();  
  4.    String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  5. %>  
  6. <html>  
  7.     <head>  
  8.         <!– Page title –>  
  9.         <title>imooc – Login</title>  
  10.         <!– End of Page title –>  
  11.         <!– Libraries –>  
  12.         <link type=”text/css” href=”css/login.css” rel=”stylesheet” />      
  13.         <link type=”text/css” href=”css/smoothness/jquery-ui-1.7.2.custom.html” rel=”stylesheet” />     
  14.         <script type=”text/javascript” src=”js/jquery-1.3.2.min.js”></script>  
  15.         <script type=”text/javascript” src=”js/easyTooltip.js”></script>  
  16.         <script type=”text/javascript” src=”js/jquery-ui-1.7.2.custom.min.js”></script>  
  17.         <!– End of Libraries –>   
  18.     </head>  
  19.     <body>  
  20.     <div id=”container”>  
  21.         <div class=”logo”>  
  22.             <a href=”#”><img src=”logo.png” alt=”” /></a>  
  23.         </div>  
  24.         <div id=”box”>  
  25.             登录退步!检查用户大概密码  
  26.             <a href=”login.jsp”>重回登录界面</a>  
  27.         </div>  
  28.     </div>  
  29.     </body>  
  30. </html>  

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图