导图社区 jQuery参考手册
这是一篇关于jQuery参考手册的思维导图,是学习和掌握jQuery的宝贵资料,无论是对于初学者系统地了解jQuery的功能和使用方法,还是对于有经验的开发者查阅和复习相关知识,都具有很高的参考价值。汇总了jQuery的复杂知识体系,有助于提高学习效率和开发效率。
编辑于2026-01-06 14:46:01jQuery参考手册
jQuery 教程
jQuery 简介
jQuery 库 - 特性
jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTML 事件函数 JavaScript 特效和动画 HTML DOM 遍历和修改 AJAX Utilities
jQuery 安装
把 jQuery 添加到您的网页
<head> <script src="jquery.js"></script> </head>
下载版 jQuery
CDN
jQuery 语法
通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行“操作”(actions)。
基础语法是:$(selector).action()
$(选择器).事件()
选择器选择元素
jQuery 选择器
选择器 实例 选取
* $("*") 所有元素
#id $("#lastname") id="lastname" 的元素
.class $(".intro") 所有 class="intro" 的元素
element $("p") 所有 <p> 元素
.class.class $(".intro.demo") 所有 class="intro" 且 class="demo" 的元素
:first $("p:first") 第一个 <p> 元素
:last $("p:last") 最后一个 <p> 元素
:even $("tr:even") 所有偶数 <tr> 元素
:odd $("tr:odd") 所有奇数 <tr> 元素
:eq(index) $("ul li:eq(3)") 列表中的第四个元素(index 从 0 开始)
:gt(no) $("ul li:gt(3)") 列出 index 大于 3 的元素
:lt(no) $("ul li:lt(3)") 列出 index 小于 3 的元素
:not(selector) $("input:not(:empty)") 所有不为空的 input 元素
:header $(":header") 所有标题元素 <h1> - <h6>
:animated 所有动画元素
:contains(text) $(":contains('W3School')") 包含指定字符串的所有元素
:empty $(":empty") 无子(元素)节点的所有元素
:hidden $("p:hidden") 所有隐藏的 <p> 元素
:visible $("table:visible") 所有可见的表格
s1,s2,s3 $("th,td,.intro") 所有带有匹配选择的元素
[attribute] $("[href]") 所有带有 href 属性的元素
[attribute=value] $("[href='#']") 所有 href 属性的值等于 "#" 的元素
[attribute!=value] $("[href!='#']") 所有 href 属性的值不等于 "#" 的元素
[attribute$=value] $("[href$='.jpg']") 所有 href 属性的值包含以 ".jpg" 结尾的元素
:input $(":input") 所有 <input> 元素
:text $(":text") 所有 type="text" 的 <input> 元素
:password $(":password") 所有 type="password" 的 <input> 元素
:radio $(":radio") 所有 type="radio" 的 <input> 元素
:checkbox $(":checkbox") 所有 type="checkbox" 的 <input> 元素
:submit $(":submit") 所有 type="submit" 的 <input> 元素
:reset $(":reset") 所有 type="reset" 的 <input> 元素
:button $(":button") 所有 type="button" 的 <input> 元素
:image $(":image") 所有 type="image" 的 <input> 元素
:file $(":file") 所有 type="file" 的 <input> 元素
:enabled $(":enabled") 所有激活的 input 元素
:disabled $(":disabled") 所有禁用的 input 元素
:selected $(":selected") 所有被选取的 input 元素
:checked $(":checked") 所有被选中的 input 元素
jQuery 事件
bind() 向匹配元素附加一个或更多事件处理器
$(document).ready(function(){ $("p").bind("click",function(){ alert("这个段落被点击了。"); }); });
$(selector).bind(event,data,function)
$(选择器).bind(事件,可选传递到函数的额外数据,函数)
在元素触发事件后执行函数
blur() 元素失去焦点时触发、或将函数绑定到指定元素的 blur 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").focus(function(){ $("input").css("background-color","#FFFFCC"); }); $("input").blur(function(){ $("input").css("background-color","#D6D6FF"); }); }); </script> </head> <body> Enter your name: <input type="text" /> <p>请在上面的输入域中点击,使其获得焦点,然后在输入域外面点击,使其失去焦点。</p> </body> </html> 
focus() 当元素获得焦点时触发、或将函数绑定到指定元素的 focus 事件
change() 元素的值发生改变时触发、或将函数绑定到指定元素的 change 事件
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".field").change(function(){ $(this).css("background-color","#FFFFCC"); }); }); </script> </head> <body> <p>在某个域被使用或改变时,它会改变颜色。</p> Enter your name: <input class="field" type="text" /> <p>Car: <select class="field" name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select> </p> </body> </html>
click() 击元素时触发、或将函数绑定到指定元素的 click 事件
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").slideToggle(); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>切换</button> </body> </html>
dblclick() 双击元素时触发、或将函数绑定到指定元素的 double click 事件
delegate() 向匹配元素的当前或未来的子元素附加一个或多个事件处理器
$("div").delegate("button","click",function(){ $("p").slideToggle(); });
$(selector).delegate(childSelector,event,data,function)
$(选择器).delegate(子选择器,事件,可选数据,函数)
live() 为当前或未来的匹配元素添加一个或多个事件处理器
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").live("click",function(){ $("p").slideToggle(); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>请点击这里</button> </body> </html>
die() 移除所有通过 live() 函数添加的事件处理程序。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").live("click",function(){ $(this).slideToggle(); }); $("button").click(function(){ $("p").die(); }); }); </script> </head> <body> <p>这是一个段落。</p> <p>这是另一个段落。</p> <p>请点击任意 p 元素,段落会消失。包括本段落。</p> <button>移除通过 live() 方法向 p 元素添加的事件处理程序</button> </body> </html>
error() 当元素遇到错误(没有正确载入)时触发、或将函数绑定到指定元素的 error 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("img").error(function(){ $("img").replaceWith("<p><b>图片未加载!</b></p>"); }); }); </script> </head> <body> <img src="errorimg.gif" /> <p>如果上面的图像没有正确地加载,会被替换为一段 "图片未加载" 的文本。</p> </body> </html> 
event.isDefaultPrevented() 返回 event 对象上是否调用了 event.preventDefault()。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a").click(function(event){ alert("Default prevented: " + event.isDefaultPrevented()); }); }); </script> </head> <body> <a href="http://w3school.com.cn/">W3School</a> <p>preventDefault() 方法将防止上面的链接打开 URL。</p> <p>请点击该链接,检查是否阻止了默认动作。</p> </body> </html>
event.pageX 相对于文档左边缘的鼠标位置。
event.pageY 相对于文档上边缘的鼠标位置。
event.preventDefault() 阻止事件的默认动作。
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a").click(function(event){ event.preventDefault(); }); }); </script> </head> <body> <a href="http://w3school.com.cn/">W3School</a> <p>preventDefault() 方法将防止上面的链接打开 URL。</p> </body> </html> 
event.result 包含由被指定事件触发的事件处理器返回的最后一个值。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(e) { return ("最后一次点击的鼠标位置是: X" +e.pageX + ", Y" + e.pageY); }); $("button").click(function(e) { $("p").html(e.result); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>请点击这里</button> </body> </html>
event.target 触发该事件的 DOM 元素。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p, button, h1, h2").click(function(event){ $("div").html("点击事件由一个 " + event.target.nodeName + " 元素触发"); }); }); </script> </head> <body> <h1>这是一个标题</h1> <h2>这是另一个标题</h2> <p>这是一个段落</p> <button>这是一个按钮</button> <p>标题、段落和按钮元素定义了一个点击事件。如果您触发了事件,下面的 div 会显示出哪个元素触发了该事件。</p> <div></div> </body> </html>
event.timeStamp 该属性返回从 1970 年 1 月 1 日到事件发生时的毫秒数。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(e){ $("span").text(e.timeStamp); }); }); </script> </head> <body> <p>对下面这个按钮的点击事件发生在 1970 年 1 月 1 日之后 <span>unknown</span> 毫秒。</p> <button>Click me</button> </body> </html>
event.type 描述事件的类型。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").bind('click dblclick mouseover mouseout',function(event){ $("div").html("事件:" + event.type); }); }); </script> </head> <body> <p>该段落定义了 click、double-click、mouseover 以及 mouseout 事件。如果您触发了其中的一个事件,下面的 div 中会显示出事件的类型。</p> <div></div> </body> </html>
event.which 指示按了哪个键或按钮。
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").keydown(function(event){ $("div").html("Key: " + event.which); }); }); </script> </head> <body> 请随意键入一些字符:<input type="text" /> <p>当您在上面的框中键入文本时,下面的 div 会显示键位序号。</p> <div /> </body> </html> 
keydown() 按下某按键时触发、或将函数绑定到指定元素的 key down 事件
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").keydown(function(){ $("input").css("background-color","#FFFFCC"); }); $("input").keyup(function(){ $("input").css("background-color","#D6D6FF"); }); }); </script> </head> <body> Enter your name: <input type="text" /> <p>当发生 keydown 和 keyup 事件时,输入域会改变颜色。请试着在其中输入内容。</p> </body> </html>
keyup() 释放某一个按键时触发、或将函数绑定到指定元素的 key up 事件
keypress() 当按钮被按下,每插入一个字符时触发、或将函数绑定到指定元素的 key press 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> i=0; $(document).ready(function(){ $("input").keypress(function(){ $("span").text(i+=1); }); }); </script> </head> <body> Enter your name: <input type="text" /> <p>Keypresses:<span>0</span></p> </body> </html> 
load() 指定的元素(及子元素)已加载时触发
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("img").load(function(){ $("div").text("图像已加载"); }); }); </script> </head> <body> <img src="/i/shanghai_lupu_bridge.jpg" /> <div>图像正在加载中 ...</div> <p><b>注释:</b>根据不同的浏览器(Firefox 和 IE),如果图像已被缓存,则也许不会触发 load 事件。</p> </body> </html>
mousedown() 按下鼠标按钮时触发、或将函数绑定到指定元素的 mouse down 事件
mouseenter() 鼠标指针进入(穿过)元素时触发、或将函数绑定到指定元素的 mouse enter 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").mouseenter(function(){ $("p").css("background-color","yellow"); }); $("p").mouseleave(function(){ $("p").css("background-color","#E9E9E4"); }); }); </script> </head> <body> <p style="background-color:#E9E9E4">请把鼠标指针移动到这个段落上。</p> </body> </html> 
mouseleave() 鼠标指针离开元素时触发、或将函数绑定到指定元素的 mouse leave 事件
mouseout() 鼠标指针从元素上移开时触发、或将函数绑定到指定元素的 mouse out 事件
mouseleave 与 mouseout 的不同
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> x=0; y=0; $(document).ready(function(){ $("div.out").mouseout(function(){ $(".out span").text(x+=1); }); $("div.leave").mouseleave(function(){ $(".leave span").text(y+=1); }); }); </script> </head> <body> <p>不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。</p> <p>只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。</p> <div class="out" style="background-color:lightgray;padding:20px;width:40%;float:left"> <h2 style="background-color:white;">被触发的 Mouseout 事件:<span></span></h2> </div> <div class="leave" style="background-color:lightgray;padding:20px;width:40%;float:right"> <h2 style="background-color:white;">被触发的 Mouseleave 事件:<span></span></h2> </div> </body> </html>
不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。 只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。
mousemove() 鼠标指针在指定的元素中移动时触发、或将函数绑定到指定元素的 mouse move 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(document).mousemove(function(e){ $("span").text(e.pageX + ", " + e.pageY); }); }); </script> </head> <body> <p>鼠标位于坐标: <span></span>.</p> </body> </html> 
mouseover() 鼠标指针位于元素上方时触发、或将函数绑定到指定元素的 mouse over 事件
mouseup() 松开鼠标按钮时触发、或将函数绑定到指定元素的 mouse up 事件
one() 向匹配元素添加事件处理器。每个元素只能触发一次该处理器。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").one("click",function(){ $(this).animate({fontSize:"+=6px"}); }); }); </script> </head> <body> <p>这是一个段落。</p> <p>这是另一个段落。</p> <p>请点击 p 元素增加其内容的文本大小。每个 p 元素只会触发一次改事件。</p> </body> </html>
ready() 文档就绪事件 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").slideToggle(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button class="btn1">Toggle</button> </body> </html>
resize() 调整浏览器窗口的大小时触发、或将函数绑定到指定元素的 resize 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> x=0; $(document).ready(function(){ $(window).resize(function() { $("span").text(x+=1); }); }); </script> </head> <body> <p>窗口大小被调整过 <span>0</span> 次。</p> <p>请试着重新调整浏览器窗口的大小。</p> </body> </html> 
scroll() 用户滚动指定的元素时触发、或将函数绑定到指定元素的 scroll 事件
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> x=0; $(document).ready(function(){ $("div").scroll(function() { $("span").text(x+=1); }); }); </script> </head> <body> <p>请试着滚动 DIV 中的文本:</p> <div style="width:200px;height:100px;overflow:scroll;">text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. <br /><br /> text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text.</div> <p>滚动了 <span>0</span> 次。</p> </body> </html>
select() textarea 或文本类型的 input 元素中的文本被选择时触发、或将函数绑定到指定元素的 select 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").select(function(){ $("input").after(" Text marked!"); }); }); </script> </head> <body> <input type="text" name="FirstName" value="Hello World" /> <p>请试着选取输入域中的文本,看看会发生什么。</p> </body> </html> 
submit() 提交表单时触发、或将函数绑定到指定元素的 submit 事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("form").submit(function(e){ alert("Submitted"); }); }); </script> </head> <body> <form name="input" action="" method="get"> First name: <input type="text" name="FirstName" value="Mickey" size="20"> <br />Last name: <input type="text" name="LastName" value="Mouse" size="20"> <br /> <input type="submit" value="Submit"> </form> </body> </html> 
toggle() 绑定两个或多个事件处理器函数,当发生轮流的 click 事件时执行。
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").toggle(function(){ $("body").css("background-color","green");}, function(){ $("body").css("background-color","red");}, function(){ $("body").css("background-color","yellow");} ); }); </script> </head> <body> <button>请点击这里,来切换不同的背景颜色</button> </body> </html> 
trigger() 所有匹配元素的指定事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").select(function(){ $("input").after("文本被选中!"); }); $("button").click(function(){ $("input").trigger("select"); }); }); </script> </head> <body> <input type="text" name="FirstName" value="Hello World" /> <br /> <button>激活 input 域的 select 事件</button> </body> </html> 
triggerHandler() 第一个被匹配元素的指定事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").select(function(){ $("input").after("发生 Input select 事件!"); }); $("button").click(function(){ $("input").triggerHandler("select"); }); }); </script> </head> <body> <input type="text" name="FirstName" value="Hello World" /> <br /> <button>激活 input 域的 select 事件</button> <p>请注意,与 trigger() 方法不同,triggerHandler() 方法不会引起所发生事件的默认行为(文本不会被选中)。</p> </body> </html> 
与 trigger() 方法不同,triggerHandler() 方法不会引起所发生事件的默认行为(文本不会被选中)
unbind() 从匹配元素移除一个被添加的事件处理器
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").click(function(){ $(this).slideToggle(); }); $("button").click(function(){ $("p").unbind(); }); }); </script> </head> <body> <p>这是一个段落。</p> <p>这是另一个段落。</p> <p>点击任何段落可以令其消失。包括本段落。</p> <button>删除 p 元素的事件处理器</button> </body> </html> 
undelegate() 从匹配元素移除一个被添加的事件处理器,现在或将来
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("body").delegate("p","click",function(){ $(this).slideToggle(); }); $("button").click(function(){ $("body").undelegate(); }); }); </script> </head> <body> <p>这是一个段落。</p> <p>这是另一个段落。</p> <p>点击任何段落可以令其消失。包括本段落。</p> <button>从所有元素删除由 delegate() 方法添加的事件处理器</button> </body> </html> 
unload() 用户离开页面时触发、或将函数绑定到指定元素的 unload 事件
jQuery 效果
animate() 对被选元素应用“自定义”的动画
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { $(".btn1").click(function(){ $("#box").animate({height:"300px"}); }); $(".btn2").click(function(){ $("#box").animate({height:"100px"}); }); }); </script> </head> <body> <div id="box" style="background:#98bf21;height:100px;width:100px;margin:6px;"> </div> <button class="btn1">Animate</button> <button class="btn2">Reset</button> </body> </html>
$(selector).animate(styles,speed,easing,callback) $(选择器).animate(样式,可选动画速度,缓动,可选后续函数)
styles
backgroundPosition borderWidth borderBottomWidth borderLeftWidth borderRightWidth borderTopWidth borderSpacing margin marginBottom marginLeft marginRight marginTop outlineWidth padding paddingBottom paddingLeft paddingRight paddingTop height width maxHeight maxWidth minHeight minWidth font fontSize bottom left right top letterSpacing wordSpacing lineHeight textIndent
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
easing
swing 开始和结束时速度较慢,中间部分速度较快。 linear 匀速
callback 可选函数
clearQueue() 对被选元素移除所有排队的函数(仍未运行的)
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#start").click(function(){ $("#box").animate({height:300},"slow"); $("#box").animate({width:300},"slow"); $("#box").queue(function () { $(this).css("background-color","red"); $(this).dequeue(); }); $("#box").animate({height:100},"slow"); $("#box").animate({width:100},"slow"); }); $("#stop").click(function(){ $("#box").clearQueue(); }); }); </script> </head> <body> <p><button id="start">Start Animation</button><button id="stop">Stop Animation</button></p> <div id="box" style="background:#98bf21;height:100px;width:100px;position:relative"> </div> </body> </html>
delay() 对被选元素的所有排队函数(仍未运行)设置延迟
fadeIn() 逐渐改变被选元素的不透明度,从隐藏到可见
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").fadeOut() }); $(".btn2").click(function(){ $("p").fadeIn(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button class="btn1">Hide</button> <button class="btn2">Show</button> </body> </html>
$(selector).fadeIn(speed,callback)
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
callback 可选函数
fadeOut() 逐渐改变被选元素的不透明度,从可见到隐藏
$(selector).fadeOut(speed,callback)
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
callback 可选函数
fadeTo() 把被选元素逐渐改变至给定的不透明度
$(selector).fadeTo(speed,opacity,callback) $(选择器).fadeTo(可选速度,透明度,可选函数)
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
opacity
0.00 与 1.00 之间的数字
callback 可选函数
hide() 隐藏被选的元素
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").hide(); }); $(".btn2").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button class="btn1">Hide</button> <button class="btn2">Show</button> </body> </html>
$(selector).hide(speed,callback)
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
callback 可选函数
dequeue() 运行被选元素的下一个排队函数
show() 显示被选的元素
queue() 显示被选元素的排队函数
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").hide(); }); $(".btn2").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button class="btn1">Hide</button> <button class="btn2">Show</button> </body> </html>
$(selector).show(speed,callback)
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
callback 可选函数
slideDown() 通过调整高度来滑动显示被选元素
$(selector).slideDown(speed,callback)
slideUp() 通过调整高度来滑动隐藏被选元素
slideToggle() 对被选元素进行滑动隐藏和滑动显示的切换
$(selector).slideDown(speed,callback)
stop() 停止在被选元素上运行动画
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#start").click(function(){ $("#box").animate({height:300},2000); $("#box").animate({height:100},2000); }); $("#stop").click(function(){ $("#box").stop(); }); }); </script> </head> <body> <p><button id="start">Start Animation</button><button id="stop">Stop Animation</button></p> <div id="box" style="background:#98bf21;height:100px;width:100px;position:relative"> </div> </body> </html>
$(selector).stop(stopAll,goToEnd) $(选择器).stop(可选停止所有,可选是否完成当前动画)
stopAll
true
goToEnd
true
toggle() 对被选元素进行隐藏和显示的切换
$(selector).toggle(speed,callback,switch)
speed
可能的值: 毫秒 (比如 1500) "slow" "normal" "fast"
callback 可选函数
switch
true - 显示所有元素 false - 隐藏所有元素
无法同时使用 speed 和 callback 参数
jQuery 文档操作
这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html()
红字为属性操作
addClass() 向匹配的元素添加指定的类名。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p:first").addClass("intro"); }); }); </script> <style type="text/css"> .intro { font-size:120%; color:red; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>向第一个 p 元素添加一个类</button> </body> </html>
after() 在匹配的元素之后插入内容。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").after("<p>Hello world!</p>"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button>在每个 p 元素后插入内容</button> </body> </html>
append() 向匹配元素集合中的每个元素结尾插入由参数指定的内容。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").append(" <b>Hello world!</b>"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>在每个 p 元素的结尾添加内容</button> </body> </html>
appendTo() 向目标结尾插入匹配元素集合中的每个元素。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("<b> Hello World!</b>").appendTo("p"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>在每个 p 元素的结尾添加内容</button> </body> </html>
$(选择器).appendTo(要绑定的元素)
attr() 设置或返回匹配元素的属性和值。
$(selector).attr(attribute)
attribute 规定要获取其值的属性。
$(selector).attr(attribute,value)
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("img").attr("width","180"); }); }); </script> </head> <body> <img src="/i/eg_smile.gif" /> <br /> <button>设置图像的 width 属性</button> </body> </html>
attribute 规定属性的名称。 value 规定属性的值。
before() 在每个匹配的元素之前插入内容。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").before("<p>Hello world!</p>"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button class="btn1">在每个段落前面插入新的段落</button> </body> </html>
clone() 创建匹配元素集合的副本。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("body").append($("p").clone()); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button>复制每个 p 元素,然后追加到 body 元素</button> </body> </html>
detach() 从 DOM 中移除匹配元素集合。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").detach(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button>删除 p 元素</button> </body> </html>
子主题
empty() 删除匹配的元素集合中所有的子节点。
remove() 移除所有匹配的元素。
removeAttr() 从所有匹配的元素中移除指定的属性。
removeClass() 从所有匹配的元素中删除全部或者指定的类。
hasClass() 检查匹配的元素是否拥有指定的类。
html() 设置或返回匹配的元素集合中的 HTML 内容。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").html("Hello <b>world!</b>"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button class="btn1">改变 p 元素的内容</button> </body> </html>
insertAfter() 把匹配的元素插入到另一个指定的元素集合的后面。
insertBefore() 把匹配的元素插入到另一个指定的元素集合的前面。
prepend() 向匹配元素集合中的每个元素开头插入由参数指定的内容。
prependTo() 向目标开头插入匹配元素集合中的每个元素。
prop() 设置或返回被选元素的属性和值。
$(selector).prop(property,value)
property 规定属性的名称。
value 规定属性的值。
用于返回属性值时,则返回第一个匹配元素的值。
replaceAll() 用匹配的元素替换所有匹配到的元素。
replaceWith() 用新内容替换匹配的元素。
text() 设置或返回匹配元素的内容。
toggleClass() 从匹配的元素中添加或删除一个类。
unwrap() 移除并替换指定元素的父元素。
val() 设置或返回匹配元素的值。
wrap() 把匹配的元素用指定的内容或元素包裹起来。
wrapAll() 把所有匹配的元素用指定的内容或元素包裹起来。
wrapinner() 将每一个匹配的元素的子内容用指定的内容或元素包裹起来。
jQuery CSS 操作
css() 设置或返回匹配元素的样式属性。
height() 设置或返回匹配元素的高度。
offset() 返回第一个匹配元素相对于文档的位置。
offsetParent() 返回最近的定位祖先元素。
position() 返回第一个匹配元素相对于父元素的位置。
scrollLeft() 设置或返回匹配元素相对滚动条左侧的偏移。
scrollTop() 设置或返回匹配元素相对滚动条顶部的偏移。
width() 设置或返回匹配元素的宽度。
jQuery Ajax
jQuery 库拥有完整的 Ajax 兼容套件。其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据。
jQuery.ajax() 执行异步 HTTP (Ajax) 请求。
.ajaxComplete() 当 Ajax 请求完成时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxError() 当 Ajax 请求完成且出现错误时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxSend() 在 Ajax 请求发送之前显示一条消息。
jQuery.ajaxSetup() 设置将来的 Ajax 请求的默认值。
.ajaxStart() 当首个 Ajax 请求完成开始时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxStop() 当所有 Ajax 请求完成时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxSuccess() 当 Ajax 请求成功完成时显示一条消息。
jQuery.get() 使用 HTTP GET 请求从服务器加载数据。
jQuery.getJSON() 使用 HTTP GET 请求从服务器加载 JSON 编码数据。
jQuery.getScript() 使用 HTTP GET 请求从服务器加载 JavaScript 文件,然后执行该文件。
.load() 从服务器加载数据,然后把返回到 HTML 放入匹配元素。
jQuery.param() 创建数组或对象的序列化表示,适合在 URL 查询字符串或 Ajax 请求中使用。
jQuery.post() 使用 HTTP POST 请求从服务器加载数据。
.serialize() 将表单内容序列化为字符串。
.serializeArray() 序列化表单元素,返回 JSON 数据结构数据。
jQuery 遍历
jQuery 遍历函数包括了用于筛选、查找和串联元素的方法。
.add() 将元素添加到匹配元素的集合中。
.andSelf() 把堆栈中之前的元素集添加到当前集合中。
.children() 获得匹配元素集合中每个元素的所有子元素。
 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <style> body { font-size:16px; font-weight:bolder; } p { margin:5px 0; } </style> </head> <body> <div> <span>Hello</span> <p class="selected">Hello Again</p> <div class="selected">And Again</div> <p>And One Last Time</p> </div> <script>$("div").children(".selected").css("color", "blue");</script> </body> </html>
.closest() 从元素本身开始,逐级向上级元素匹配,并返回最先匹配的祖先元素。
.contents() 获得匹配元素集合中每个元素的子元素,包括文本和注释节点。
.each() 对 jQuery 对象进行迭代,为每个匹配元素执行函数。
.end() 结束当前链中最近的一次筛选操作,并将匹配元素集合返回到前一次的状态。
.eq() 将匹配元素集合缩减为位于指定索引的新元素。
.filter() 将匹配元素集合缩减为匹配选择器或匹配函数返回值的新元素。
.find() 获得当前匹配元素集合中每个元素的后代,由选择器进行筛选。
.first() 将匹配元素集合缩减为集合中的第一个元素。
.has() 将匹配元素集合缩减为包含特定元素的后代的集合。
.is() 根据选择器检查当前匹配元素集合,如果存在至少一个匹配元素,则返回 true。
.last() 将匹配元素集合缩减为集合中的最后一个元素。
.map() 把当前匹配集合中的每个元素传递给函数,产生包含返回值的新 jQuery 对象。
.next() 获得匹配元素集合中每个元素紧邻的同辈元素。
.nextAll() 获得匹配元素集合中每个元素之后的所有同辈元素,由选择器进行筛选(可选)。
.nextUntil() 获得每个元素之后所有的同辈元素,直到遇到匹配选择器的元素为止。
.not() 从匹配元素集合中删除元素。
.offsetParent() 获得用于定位的第一个父元素。
.parent() 获得当前匹配元素集合中每个元素的父元素,由选择器筛选(可选)。
.parents() 获得当前匹配元素集合中每个元素的祖先元素,由选择器筛选(可选)。
.parentsUntil() 获得当前匹配元素集合中每个元素的祖先元素,直到遇到匹配选择器的元素为止。
.prev() 获得匹配元素集合中每个元素紧邻的前一个同辈元素,由选择器筛选(可选)。
.prevAll() 获得匹配元素集合中每个元素之前的所有同辈元素,由选择器进行筛选(可选)。
.prevUntil() 获得每个元素之前所有的同辈元素,直到遇到匹配选择器的元素为止。
.siblings() 获得匹配元素集合中所有元素的同辈元素,由选择器筛选(可选)。
.slice() 将匹配元素集合缩减为指定范围的子集。
jQuery 数据
这些方法允许我们将指定的 DOM 元素与任意数据相关联。
.clearQueue() 从队列中删除所有未运行的项目。
.data() 存储与匹配元素相关的任意数据。
jQuery.data() 存储与指定元素相关的任意数据。
.dequeue() 从队列最前端移除一个队列函数,并执行它。
jQuery.dequeue() 从队列最前端移除一个队列函数,并执行它。
jQuery.hasData() 存储与匹配元素相关的任意数据。
.queue() 显示或操作匹配元素所执行函数的队列。
jQuery.queue() 显示或操作匹配元素所执行函数的队列。
.removeData() 移除之前存放的数据。
jQuery.removeData() 移除之前存放的数据。
jQuery DOM 元素
.get() 获得由选择器指定的 DOM 元素。
.index() 返回指定元素相对于其他指定元素的 index 位置。
.size() 返回被 jQuery 选择器匹配的元素的数量。
 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ alert($("li").size()); }); }); </script> </head> <body> <button>输出 li 元素的数目</button> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html>
.toArray() 以数组的形式返回 jQuery 选择器匹配的元素。
jQuery 核心
jQuery() 接受一个字符串,其中包含了用于匹配元素集合的 CSS 选择器。
jQuery.noConflict() 运行这个函数将变量 $ 的控制权让渡给第一个实现它的那个库。
jQuery 属性
context 在版本 1.10 中被弃用。包含传递给 jQuery() 的原始上下文。
jquery 包含 jQuery 版本号。
显示版本号
<!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js"> </script> <script> $(document).ready(function(){ $("button").on("click",function(){ var version = $().jquery; alert("You are running jQuery version: " + version); }); }); </script> </head> <body> <button>显示版本号</button> </body> </html>
jQuery.fx.interval 改变以毫秒计的动画速率。
jQuery.fx.interval = 500;//动画帧间隔改为 500 毫秒
jQuery.fx.off 全局禁用/启用所有动画。
jQuery.fx.off = true;
jQuery.support 表示不同浏览器特性或漏洞的属性集合(用于 jQuery 内部使用)。
jQuery.support.
ajax:检查是否支持AJAX,即XMLHttpRequest。
boxModel:是否是标准盒模型。
changeBubbles:change事件是否冒泡。
checkClone:检查克隆节点时是否保留检查状态(如复选框)。
checkOn:未选中的复选框的值是否为"on"。
cors:是否支持跨域资源共享。
cssFloat:CSS的float属性名是否正确(比如旧IE用styleFloat)。
hrefNormalized:href属性是否被浏览器标准化。
htmlSerialize:是否可以通过innerHTML正确序列化HTML。
leadingWhitespace:innerHTML是否保留前导空格。
noCloneChecked:克隆复选框时是否保留checked状态。
noCloneEvent:克隆元素时是否复制事件处理程序。
opacity:是否支持opacity透明度样式。
optDisabled:下拉选项是否可被禁用。
optSelected:默认选中的选项是否保持selected属性。
scriptEval():动态添加的脚本是否立即执行。
style:是否可以通过style属性访问行内样式。
submitBubbles:submit事件是否冒泡。
tbody:是否自动添加tbody到空的table元素。
length 包含 jQuery 对象中的元素数目。
$(selector).length