使用jQuery去掉<a>标签

针对特定的id下的所有记录匹配
$("#tbody a").contents().unwrap();

可以添加到函数及循环来使用,还可以向外扩展不仅限于a标签

$("#tbody p").contents().unwrap();
$("#tbody span").contents().unwrap();
$("#tbody div").contents().unwrap();




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jquery是继prototype之后又一个优秀的Javascrīpt框架。它是轻量级的js库(压缩后只有21k) ,它兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。jQuery使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。jQuery能够使用户的html页保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需定义id即可。 目录 简介 找到你了! Jquery对象 代替body标签的onload 事件机制 同一函数实现get\set ajax 渐入淡出 plugin 历史版本 1、新的事件.on() .off() 3、动画的改进 1.42版介绍 JQuery插件 让Dreamweaver支持提示代码功能 引入JQuery简介 找到你了! Jquery对象 代替body标签的onload 事件机制 同一函数实现get\set ajax 渐入淡出 plugin历史版本1、新的事件.on() .off()3、动画的改进1.42版介绍JQuery插件让Dreamweaver支持提示代码功能引入JQuery展开编辑本段简介   jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多javascript高手加入其team,包括来自德国的Jörn Zaefferer,罗马尼亚的Stefan Petre等等。jQuery是继prototype之后又一个优秀的Javascrīpt框架。其宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。   由于目前高校基本尚未开JavaScript的相关课程,目前jQuery的学习,使用,研究都仅限于在职Web程序员之间。   用jq的前提,首先要引用一个有jq的文件   [removed][removed]   这个是jquery官方最新的地址。可用在自己网站里加个这个,就能使用jquery了。 但仍然建议下载到本地服务器上。 编辑本段找到你了!   在编写js库代码时候,你一定经常和“$”美元符号打交道吧?无论prototype还是DWR都使用了$代替频繁的document.getElementById()操作。jQuery也这样做了,但是,它的功能远非如此,瞧瞧以下的jQuery代码,你就会发现它的美丽:   代码   var someElement = $("#myId");   看起来比其他两个框架的要多了一个#,好,看看下面的用法:   代码   $("div p"); // (1)   $("div.container"); // (2)   $("div #msg"); // (3)   $("table a",context); // (4)   在prototype里看过这样的写法吗?第一行代码得到所有div标签下的p元素。第二行代码得到class 为container的div元素,第三行代码得到标签下面id为msg的div元素(不过最好别这样写,因为jQuery需要遍历所有的div元素,对于带id的元素,直接用$("#id"))。第四行代码得到context为上下文的table里面所有的链接元素。   如果你熟悉CSS,你会觉得这些写法很眼熟!对了。正是。看出奥妙了吧。jQuery就是如此强大,你可以轻易地找到DOM中的任何元素,而这也是jQuery设计之初query的真实含义(查询)。 编辑本段Jquery对象   jquery提供了很多便利的函数,如each(fn),但是使用这些函数的前提是:你使用的对象是Jquery对象。使一个Dom对象成为一个Jquery对象很简单,通过下面一些方式(只是一部分):   代码   var a = $("#cid");   var b = $("<p>hello</p>");   var c = document.createElement("table");   var tb = $(c); 编辑本段代替body标签的onload   这个惯例,也许是除了$()之外,用得最多的地方了。下面一段代码:   代码   $(document).ready(function(){   alert("hello");   });(1)   <body>(2)   <body>这里的alert('hello');要等到页面全部加载完毕才执行,注意是全部加载,包括dom,图片等其它资源。   而$(document).ready(function(){   alert("hello");   });(1)   当dom加载完就可以执行了。   代码1同时做到表现和逻辑分离。并且可以在不同的js文件中做相同的操作,即$(document).ready (fn)可以在一个页面中重复出现,而不会冲突。基本上Jquery的很多plugin都是利用这个特性,正因为这个特性,多个plugin共同使用起来,在初始化时不会发生冲突。   当使用jquery时,推荐使用代码1。 编辑本段事件机制   我们大量使用的事件可能就是button的onclick了。以前习惯在input 元素上写onclick = "fn()",使用jquery可以使javascrīpt代码与html代码分离,保持HTML的清洁,还可以很轻松地绑定事件,甚至你可以不知道“事件”这个名词。   代码   $(document).ready(function()   {   $("#clear").click(function(){   alert("i am about to clear the table");   });   $("form[12]").submit(validate);   });   function validate(){   //do some form validation   } 编辑本段同一函数实现get\set   代码   $("selector").load(url,data,function(response,status,xhr))   该方法是最简单的从服务器获取数据的方法。它几乎与 $.get(url, data, success) 等价,不同的是它不是全局函数,并且它拥有隐式的回调函数。当侦测到成功的响应时(比如,当 textStatus 为 "success" 或 "notmodified" 时),.load() 将匹配元素的 HTML 内容设置为返回的数据。这意味着该方法的大多数使用会非常简单。 编辑本段ajax   这是一个ajax横行的时代。多少人,了不了解ajax的都跟着用上一把。呵。使用jquery实现ajax同样异常简单   代码   (1)   $.get("search. do",{id:1},rend);   function rend(xml){   alert(xml);   }   (2)   $.post("search. do",{id:1},rend);   function rend(xml){   alert(xml);   }   (3)   $("#msg").ajaxStart(function(){   this.html("正在加载。。。。");   });   (4)   $("#msg").ajaxSuccess(function(){   this.html("加载完成!");   });   这些都是较常用的方法,get和post用法一样。第一个参数是异步请求的url,第二个为参数,第三个回调方法。   (4)的方法会在指定的Dom对象上绑定响应ajax执行的事件。   (5)同步加载数据。发送请求时锁住浏览器。需要锁定用户交互操作时使用同步方式。   var html = $.ajax({   url: "some.php",   async: false   }).responseText;   (6) 保存数据到服务器,成功时显示信息。   $.ajax({   type: "POST",   url: "some.php",   data: "name=John&location=Boston",   success: function(msg){   alert( "Data Saved: " + msg );   }   }); 编辑本段渐入淡出   代码   $("#msg").show("fast");   $("#msg").hide("slow");   $("#msg").fadeIn();   $("#msg").fadeOut();   没错,上面两行代码已经分别实现了一个id为Msg的jquery对象的渐入和淡出。做一个像Gmail一样的动态加载通知条,用jquery就那么简单。两个函数接受的参数除了快慢等,还可以接收整型,作为渐入或淡出的完成时间,单位为MS。 编辑本段plugin   这也是一个插件的时代。   jquery插件给我的感觉清一色的清洁,简单。如Jtip,要使用它的功能,只需要在你的元素的class上加上Jtip,并引入jtip.js及其样式即可以了。其他事情插件全包。我喜欢jquery的一个重要原因是发现她已经有了很多很好,很精彩的插件。   写得很烂。可能大家看不出jquery的好处。嗯,光听是没用的,试用一下吧。你会发觉很有趣。   暂时告一段落吧。待有新的发现再来分享。   加一些Jquery的资源:   http://www.visualjquery. com/index.xml 很好的API查询站点   http://jquery. com/demo/thickbox/ 知道lightBox吧,看看Jquery是怎样实现相同的东西   http://jquery. org. cn/visual/cn/index.xml //不错的JQUERY 中文学习 推荐   微软的visual studio 2008 sp1支持对jquery的动态提示,只要在代码页导入对应的vsdoc脚本就可以。   目前为止,jQuery的最新版本为1.7.2。 编辑本段历史版本   jQuery 1.0(2006年8月):该库的第一个稳定版本,已经具有了对CSS选择符、事件处理和AJAX交互的稳健支持。   jQuery 1.1(2007年1月):这一版大幅简化了API。许多较少使用的方法被合并,减少了需要掌握和解释的方法数量。   jQuery 1.1.3(2007年7月):这次小版本变化包含了对jQuery选择符引擎执行速度的显著提升。从这个版本开始,jQuery的性能达到了Prototype、Mootools以及Dojo等同类JavaScript库的水平。   jQuery 1.2(2007年9月):这一版去掉了对XPath选择符的支持,原因是相对于CSS语法它已经变得多余了。这一版能够支持对效果的更灵活定制,而且借助新增的命名空间事件,也使插件开发变得更容易。   jQuery UI(2007年9月):这个新的插件套件是作为曾经流行但已过时的Interface插件的替代项目而发布的。jQuery UI中包含大量预定义好的部件(widget),以及一组用于构建高级元素(例如可拖放的界面元素)的工具。   jQuery 1.2.6(2008年5月):这一版主要是将Brandon Aaron开发的流行的Dimensions插件的功能移植到了核心库中。   jQuery 1.3(2009年1月):这一版使用了全新的选择符引擎Sizzle,库的性能也因此有了极大提升。这一版正式支持事件委托特性。   jQuery 1.3.2(2009年2月):这次小版本升级进一步提升了库的性能,例如改进了:visible/:hidden选择符、.height()/.width()方法的底层处理机制。另外,也支持查询的元素按文档顺序返回。   jQuery 1.4(2010年1月14号对)代码库进行了内部重写组织,开始建立一些风格规范。老的core.js文件被分为attribute.js, css.js, data.js, manipulation.js, traversing.js和queue.js;CSS和attribute的逻辑分离。   jQuery 1.5(2011年1月31日):该版本修复了83个bug,解决了460个问题。重大改进有:重写了Ajax模块;新增延缓对像(Deferred Objects);jQuery替身——jQuery.sub();增强了遍历相邻节点的性能;jQuery开发团队构建系统的改进。   1.4重要新特性:   ·常用方法的性能大幅提升:重写了大部分较早期的函数;   ·更容易使用的设置函数(setter function):为所有对象新增了许多易用的设置函数;   ·对Ajax的改进:引入了许多Ajax和JSON处理方面的更新,包括HTML5元素的序列化;   ·attribute(改进了.attr()的性能)、jQuery()核心函数、CSS(.css()性能有两倍提升)、特效和事件、DOM操作等也有显著改进   1.5   美国时间1月31日John Resig在jQuery官方博客发表文章,宣布jQuery 1.5正式版已经如期开发完成,可以下载使用。压缩版本jQuery Minified29KB,不压缩版本jQuery Regular(用于阅读和调试)207KB。由于jQuery已经成为目前最流行的JavaScript库,得到广泛的支持,新版本的发布当然非常引人注目。   重要变化:   1. Ajax重写Ajax模块完全进行了重写。新增一个jXHR对象,为不同浏览器内置的XMLHttpRequest提供了一致的超集。对于XMLHttpRequest之外的传输机制,比如JSONP请求,jXHR对象也可以进行处理。(详情可以参见:jQuery.ajax文档)   此外,系统的可扩展性大大增强,可以附加各种数据处理器、过滤器和传输机制,为开发新的Ajax插件提供了方便。   2. 延迟对象   延迟对象(Deferred Object,jQuery.Deferred对象)是一个可链接的(chainable)实用工具对象,实现了Promise接口,可以在回调队列中注册多个回调、调用回调队列并转发任何同步/异步函数的成败状态。正如Using Deferreds in jQuery 1.5一文中说明的,其结果是在jQuery中能够将依赖于某个任务(事件)结果的逻辑与任务本身解耦了。这一点在JavaScript中其实并不新鲜,Mochikit和Dojo等已经实现有些日子了。由于jQuery 1.5的Ajax模块内置使用了延迟对象,因此现在通过jQuery编写Ajax程序将自动获得这一功能。   开发人员借此可以使用无法立即获得的返回值(如异步Ajax请求的返回结果),而且第一次能够附加多个事件处理器。   例如,使用了新的jQuery内部Ajax API就可以实现下面的代码了:   // Assign handlers immediately after making the request, // and remember the jxhr object for this request var jxhr = $.ajax({ url: "example.php" }) .success(function() { alert("success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); });// perform other work here ... // Set another completion function for the request above jxhr.complete(function(){ alert("second complete"); });   此外,使用jQuery.Deferred还可以开发自己的延迟对象。更多详情参见:延迟对象文档。   3. jQuery.sub()   jQuery 1.5提供了一种创建和修改jQuery副本的方式。可以用来添加不向外部公开的方法,或者对jQuery的某些方法进行重新定义以提供新功能,或者提供更好的封装、避免名称空间冲突。当然,也可以用来开发插件,但Resig强烈建议在开发插件之前,先考虑jQuery UI widget工厂。   值得注意的是,sub函数并不提供真正的隔离,所有方法、数据、调用仍然依靠jQuery本身来支持。   4. 遍历性能提高   在新版本中.children(), .pre(), .next()几个常用的遍历函数性能有了显著提高。   5. 内部开发系统   John Resig还特别提到了jQuery团队内部开发系统的两点改变:一是服务器端用Node.js替换了老的Java/Rhino系统,使得团队可以专注于JavaScript环境的新变化;二是所用的代码优化程序从Google Closure切换到UglifyJS,新工具的压缩效果非常令人满意。   有意思的是,此前UglifyJS开发者曾经公布过自己的测试结果,表明对jQuery的压缩结果UglifyJS要比Closure略大(都在72KB左右),但运行速度快得多,而且Closure不太安全。看来,这段时间UglifyJS的进展也很快啊。   jQuery 1.5.2 在1.5的基础上修正的大量的bug   1,7b   2011年09月29日jQuery 1.7 的第一个 beta 测试版本,该版本修复了超过 50 个的问题以及带来一些新特性。   2011年11月-04日jQuery1.7正式版发布。    新版本包含了很多新的特征,特别提升了事件委派时的性能尤其是在IE7下。   新增及改进项:   1. 新的事件 APIs: .on() and .off();   2. 提升了事件委派时的性能有了大幅度的提升,尤其是在IE7下;   3. 更好的在 IE 6/7/8 上支持 HTML5;   4. 切换动画更加直观;   5. 匿名模块定义 AWD   6. jQuery.Deferred   7. jQuery.isNumeric()   被删除的方法:   event.layerX and event.layerY   jQuery.isNaN()   2012年03月24日jQuery 1.7.2正式版发布。    该版本在1.7.1的基础上修复了大量的bug,并改进了部分功能。而相比于1.7.2 RC1,只修复了一个bug。值得注意的是:如果你正在使用jQuery Mobile,请使用最新的jQuery 1.7.2和jQuery Mobile 1.1这两个版本,因为之前的jQuery Mobile版本还基于jQuery core 1.7.1或更早的版本。 编辑本段1、新的事件.on() .off()   旧的 API(jQuery 1.7之前) 新的 API(jQuery 1.7) $(elems).bind(events, fn) $(elems).on(events, fn) $(elems).bind(events, { mydata: 42 }, fn) $(elems).on(events, { mydata: 42 }, fn) $(elems).unbind(events, fn) $(elems).off(events, fn) $(elems).delegate(events, selector, fn) $(elems).on(events, selector, fn) $(elems).undelegate(events, selector, fn) $(elems).off(events, selector, fn) $(selector).live(events, fn) $(document).on(events, selector, fn) $(selector).die(events, fn) $(document).off(events, selector, fn) 编辑本段3、动画的改进   在jQuery 1.7版之前,如果你在完成前停止动画,它可以创建的情况下被动画的元素永远不会返回到其全尺寸,它基本上停留在高度,这是停止动画时。 我们记住的原始尺寸固定,动画开始之前,让他们以后可以使用。 这消除了一大烦恼,尤其是在使用一定的动画切换的。 编辑本段1.42版介绍   jQuer1.4.2版本修复了1.4版本的一些错误和优化了一些不错的改进,速度比1.4.1版本又提升 了一倍,该版本对性能做了一些改进,同时增加了一些api ( .delegate() and .undelegate(). ),右图是JQuery各个版本的性能比较:   jquery1.42新特性:   加了两个新方法:.delegate() 和.undelegate(). 是对.live() and .die() 的补充. 这两个方法对特定的事件的起到简化。   范例:   $("table").delegate("td", "hover", function(){ $(this).toggleClass("hover"); });   等于使用 .live():   $("table").each(function(){ $("td", this).live("hover", function(){ $(this).toggleClass("hover"); }); });   另外,以下代码中,.live() 基本上等同于 .delegate().   $(document).delegate("td", "hover", function(){ $(this).toggleClass("hover"); });   变更   大量代码进行了重写, 提升了性能及修正了一些长期存在的问题。   提升性能   每次开发新的jquery版本,我们都努力去持续优化性能,以保证你能用到最高性能的javascript代码。   在Taskspeed benchmark 的测试中,1.4.2比1.4.1快1倍,比1.3.2快2倍。   主要是在下面4个方面进行了提升:   .bind() 和 .unbind().   .empty(), .remove(), 和 .html().   插入单个DOM 节点到 document.   $("body")   在测试用例中,比较多的用到了 $("body"), .bind(), .unbind()方法,因此测试结果提升比较明显。   v重写事件   event handlers 不再作为一个对象属性保存在 jQuery的内部对象里。现在是保存在一个内部的对象数组里。v 现在可以通过调用.data("events") , 将会返回一个对象包含的所有事件类型。   现在可以使用不同的数据、命名空间、事件类型绑定在同一个handler   在一个handler清除自己之后,事件handler会继续执行   不用关联数据或者命名空间到事件handler   不用再使用代理方法   事件执行的顺序在所有浏览器中得到保证,Google Chrome 中出现的对象循环逻辑问题已经得到解决. 编辑本段JQuery插件   【基础】   a)样式   很 多人会认为样式是个很复杂的东西,需要沉着冷静的心态加上非凡的审美观才能设计出赏心悦目的UI,抛开图片设计不说,其实css也就是那么些属 性:position,margin,padding,width,height,left,top,float,border,background…   UI设计的漂亮与否在很大程度上依赖于设计人员对配色的把握和整体效果的协调。   b)脚本   我们同样需要对javascript有着深刻的理解,对dom, xhr, Regex, call-apply, prototype等都应该有一定的了解。   有人会说要这些有啥用啊,对dom的操作其实通过getElementById, getElementsByTagName以及其他的API都可以轻松的完成,这话是没错,当思路确定后,思想才是重点,一段代码是精华还是糟粕很容易就 可以区分出来,究其原因还是取决你自己   【实践】   jQuery开发或使用,更多的灵感是来自实践,而不是copy||paste(奉行拿来主义的同学可以离开了)。   那么在这里我会用一个简单的例子来阐述jQuery插件开发的流程,能否举一反三就看各位看官了。   【目的】   开发一个插件之前我们需要对自己的目的有一个清醒的认识,有很明确的方向感,那么此次我作为示例插件的目的,就是呈现一个用于UI的Slider – 滑动条,常年从事于或暂时专注于win32开发的同学应该比较了解。   【草图】   真正动手编码之前我们还需要有一个草图来描述自己插件的“长相”(事件驱动或API封装的可以忽略)。   很多的同学在做UI开发前往往会忙于搜集各种小图片(非精通ps或iconworkshop人士),其实漂亮的图标的确可以美化我们的UI,不过我一般的处理方式是编写易于扩展的css,前期的UI呈现尽量少使用图片,多用线条完成。   【编码】   开发jQuery UI/Effect 插件在很多时候都需要与UI交互,因此在呈现上需要提供Html tree来绘制我们的插件,最终通过js dom来输出,那么在绘制简单的dom结构的时候我会直接用js来完成,不过如果嵌套比较复杂的话,我们还是应该先用html来完成,然后转变成js输 出。   【扩展】   有的时候用户却不是那么容易满足,于是有人高呼:“我要自己设置value,为什么不提供这个功能?”。   那么这时我们就需要为用户公开一个方法,用于设置jSlider的value,首先考虑的是作为方法需要一个作用对象(jSlider),那么此时我又不 想将作用对象作为参数传入,那么我们还是将这个方法作为插件来开发,我们将方法命名为setSliderValue,开放2个参数,v(value值)和 callback(设置完成后的回调函数)。   【插件】   其实网上已经有成百上千种插件了,应该可以满足大家的需求了。   【小结】   通篇到这里就结束了,简单的介绍了一款jQuery插件的开发流程,以及开发中应该注意的细节,那么在下一篇的文章中我会向大家介绍如何打造一个通用型的 自动完成 插件。 编辑本段让Dreamweaver支持提示代码功能   要让Dreamweaver支持jQuery自动提示代码功能,方法很简单,下载一个插件—jQuery_API.mxp,以及cs4的jQuery语法提示插件 (详细步骤见参考资料 [1])。 在Dreamweaver里依次选择“命令” -> “扩展管理” -> “安装扩展” -> …,就会自动安装了。   成功后重启Dreamweaver,就大功告成了。 编辑本段引入JQuery   在head标签内加入这个 [removed][removed]   这里的src是你的jQuery库文件的位置,可以到jquery官网下载。   这样就引入了jquery,就可以使用了。在代码中,遇到$一般就表示用了jquery。为什么要说一般呢?因为除了jquery,javascript还有一个库,用的也是$符(property库)。   jquery也给出了在遇到这种情况的解决办法,所以jquery是个很强大、开放、友好的js库。   下面举例:   [removed]   $(document).ready(function(){   //write your code here   })   [removed]   上面代码中$(document).ready(function()中的$就是jquery的简写,可以用jquery代替。这个ready函数是在DOM就绪后发生,他比传统的javascript方法更合理。   jquery在选取节点方面非常强大,jquery有一系列的选择器可供使用,非常简洁、高效。   基本选择器(3种):   $("标签名"),如$("p")是选取了所有的p标签节点   $("#id名"),如$("#test")是选取了id为test的标签节点   $(".class名"),如$(".test")是选取了所有class为test的标签节点   上面的$("标签名")和$(".class名")返回的都是所有满足的节点,至于进一步筛选可以添加一些函数,如eq,gt,lt等等。    当然,jquery还有很多选择器
jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组元素。 jQuery 的核心功能都是通过这个函数实现的。 jQuery中的一切都构建于这个函数之上,或者说都是在以某种方式使用这个函数。这个函数最基本的用法就是向它传递一个表达式(通常由 CSS 选择器组成),然后根据这个表达式来查找所有匹配的元素。 默认情况下, 如果没有指定context参数,$()将在当前的 HTML 文档中查找 DOM 元素;如果指定了 context 参数,如一个 DOM 元素集或 jQuery 对象,那就会在这个 context 中查找。 参考 Selectors 获取更多用于 expression 参数的 CSS 语法的信息。 -------------------------------------------------------------------------------- This function accepts a string containing a CSS selector which is then used to match a set of elements. The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements. By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of that context. See Selectors for the allowed CSS syntax for expressions. 返回值 jQuery 参数 expression (String) : 用来查找的字符串 context (Element, jQuery) : (可选) 作为待查找的 DOM 元素集、文档或 jQuery 对象。 示例 找到所有 p 元素,并且这些元素都必须是 div 元素的子元素。 HTML 代码: <p>one</p> <div><p>two</p></div> <p>three</p> jQuery 代码: $("div > p"); 结果: [ <p>two</p> ] -------------------------------------------------------------------------------- 在文档的第一个表单中,查找所有的单选按钮(即: type 值为 radio 的 input 元素)。 jQuery 代码: $("input:radio", document.forms[0]); -------------------------------------------------------------------------------- 在一个由 AJAX 返回的 XML 文档中,查找所有的 div 元素。 jQuery 代码: $("div", xml.responseXML); jQuery(html)jQuery(html) 根据提供的原始 HTML 标记字符串,动态创建由 jQuery 对象包装的 DOM 元素。 你可以传递一个手写的 HTML 字符串,或者由某些模板引擎或插件创建的字符串,也可以是通过 AJAX 加载过来的字符串。但是在你创建 input 元素的时会有限制,可以参考第二个示例。当然这个字符串可以包含斜杠 (比如一个图像地址),还有反斜杠。当你创建单个元素时,请使用闭合标签或 XHTML 格式。例如,创建一个 span ,可以用 $("<span/>") 或 $("<span></span>") ,但不推荐 $("<span>") -------------------------------------------------------------------------------- Create DOM elements on-the-fly from the provided String of raw HTML. You can pass in plain HTML Strings written by hand, create them using some template engine or plugin, or load them via AJAX. There are limitations when creating input elements, see the second example. Also when passing strings that may include slashes (such as an image path), escape the slashes. When creating single elements use the closing tag or XHTML format. For example, to create a span use $("<span/>") or $("<span></span>") instead of without the closing slash/tag. 返回值 jQuery 参数 html (String) : 用于动态创建DOM元素的HTML标记字符串 示例 动态创建一个 div 元素(以及其中的所有内容),并将它追加到 body 元素中。在这个函数的内部,是通过临时创建一个元素,并将这个元素的 innerHTML 属性设置为给定的标记字符串,来实现标记到 DOM 元素转换的。所以,这个函数既有灵活性,也有局限性。 jQuery 代码: $("<div><p>Hello</p></div>").appendTo("body"); -------------------------------------------------------------------------------- 创建一个 <input> 元素必须同时设定 type 属性。因为微软规定 <input> 元素的 type 只能写一次。 jQuery 代码: // 在 IE 中无效: $("<input>").attr("type", "checkbox"); // 在 IE 中有效: $("<input type='checkbox'>"); jQuery(elements)jQuery(elements) 将一个或多个DOM元素转化为jQuery对象。 这个函数也可以接收XML文档和Window对象(虽然它们不是DOM元素)作为有效的参数。 -------------------------------------------------------------------------------- Wrap jQuery functionality around a single or multiple DOM Element(s). This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements). 返回值 jQuery 参数 elements (Element, Array<Element>) : 用于封装成jQuery对象的DOM元素 示例 设置页面背景色。 jQuery 代码: $(document.body).css( "background", "black" ); -------------------------------------------------------------------------------- 隐藏一个表单中所有元素。 jQuery 代码: $(myForm.elements).hide() jQuery(callback)jQuery(callback) $(document).ready()的简写。 允许你绑定一个在DOM文档载入完成后执行的函数。这个函数的作用如同$(document).ready()一样,只不过用这个函数时,需要把页面中所有需要在 DOM 加载完成时执行的$()操作符都包装到其中来。从技术上来说,这个函数是可链接的--但真正以这种方式链接的情况并不多。 你可以在一个页面中使用任意多个$(document).ready事件。 参考 ready(Function) 获取更多 ready 事件的信息。 -------------------------------------------------------------------------------- A shorthand for $(document).ready(). Allows you to bind a function to be executed when the DOM document has finished loading. This function behaves just like $(document).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready to be operated on. While this function is, technically, chainable - there really isn't much use for chaining against it. You can have as many $(document).ready events on your page as you like. See ready(Function) for details about the ready event. 返回值 jQuery 参数 callback (Function) : 当DOM加载完成后要执行的函数 示例 当DOM加载完成后,执行其中的函数。 jQuery 代码: $(function(){ // Document is ready }); -------------------------------------------------------------------------------- Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias. jQuery 代码: jQuery(function($) { // Your code using failsafe $ alias here... }); jQuery 对象访问 each(callback)each(callback) 以每一个匹配的元素作为上下文来执行一个函数。 意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。 而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整形)。 返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。 -------------------------------------------------------------------------------- Execute a function within the context of every matched element. This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element. Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index). Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop). 返回值 jQuery 参数 callback (Function) : 对于每个匹配的元素所要执行的函数 示例 迭代两个图像,并设置它们的 src 属性。注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。 HTML 代码: <img/><img/> jQuery 代码: $("img").each(function(i){ this.src = "test" + i + ".jpg"; }); 结果: [ <img src="test0.jpg" />, <img src="test1.jpg" /> ] -------------------------------------------------------------------------------- 如果你想得到 jQuery对象,可以使用 $(this) 函数。 jQuery 代码: $("img").each(function(){ $(this).toggleClass("example"); }); -------------------------------------------------------------------------------- 你可以使用 'return' 来提前跳出 each() 循环。 HTML 代码: <button>Change colors</button> <span></span> <div></div> <div></div> <div></div> <div></div> <div id="stop">Stop here</div> <div></div> <div></div> <div></div> jQuery 代码: $("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); });size()size() jQuery 对象中元素的个数。 这个函数的返回值与 jQuery 对象的'length' 属性一致。 -------------------------------------------------------------------------------- The number of elements in the jQuery object. This returns the same number as the 'length' property of the jQuery object. 返回值 Number 示例 计算文档中所有图片数量 HTML 代码: <img src="test1.jpg"/> <img src="test2.jpg"/> jQuery 代码: $("img").size(); 结果: 2 lengthlength jQuery 对象中元素的个数。 当前匹配的元素个数。 size 将返回相同的值。 -------------------------------------------------------------------------------- The number of elements in the jQuery object. The number of elements currently matched. The size function will return the same value. 返回值 Number 示例 计算文档中所有图片数量 HTML 代码: <img src="test1.jpg"/> <img src="test2.jpg"/> jQuery 代码: $("img").length; 结果: 2 get()get() 取得所有匹配的 DOM 元素集合。 这是取得所有匹配元素的一种向后兼容的方式(不同于jQuery对象,而实际上是元素数组)。 如果你想要直接操作 DOM 对象而不是 jQuery 对象,这个函数非常有用。 -------------------------------------------------------------------------------- Access all matched DOM elements. This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements). It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions. 返回值 Array<Element> 示例 选择文档中所有图像作为元素数组,并用数组内建的 reverse 方法将数组反向。 HTML 代码: <img src="test1.jpg"/> <img src="test2.jpg"/> jQuery 代码: $("img").get().reverse(); 结果: [ <img src="test2.jpg"/> <img src="test1.jpg"/> ] get(index)get(index) 取得其中一个匹配的元素。 num表示取得第几个匹配的元素。 这能够让你选择一个实际的DOM 元素并且对他直接操作,而不是通过 jQuery 函数。$(this).get(0)与$(this)[0]等价。 -------------------------------------------------------------------------------- Access a single matched DOM element at a specified index in the matched set. This allows you to extract the actual DOM element and operate on it directly without necessarily using jQuery functionality on it. This function called as $(this).get(0) is the equivalent of using square bracket notation on the jQuery object itself like $(this)[0]. 返回值 Element 参数 index (Number) :取得第 index 个位置上的元素 示例 HTML 代码: <img src="test1.jpg"/> <img src="test2.jpg"/> jQuery 代码: $("img").get(0); 结果: [ <img src="test1.jpg"/> ] index(subject)index(subject) 搜索与参数表示的对象匹配的元素,并返回相应元素的索引值值。 如果找到了匹配的元素,从0开始返回;如果没有找到匹配的元素,返回-1。 -------------------------------------------------------------------------------- Searches every matched element for the object and returns the index of the element, if found, starting with zero. Returns -1 if the object wasn't found. 返回值 Number 参数 subject (Element) : 要搜索的对象 示例 返回ID值为foobar的元素的索引值值。 HTML 代码: <div id="foobar"><b></b><span id="foo"></span></div> jQuery 代码: $("*").index($('#foobar')[0]) 结果: 5 插件机制 jQuery.fn.extend(object)jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法(通常用来制作插件)。 查看这里Plugins/Authoring可以获取更多信息。 -------------------------------------------------------------------------------- Extends the jQuery element set to provide new methods (used to make a typical jQuery plugin). Can be used to add functions into the to add plugin methods (plugins). 返回值 jQuery 参数 object (Object) :用来扩充 jQuery 对象。 示例 增加两个插件方法。 jQuery 代码: jQuery.fn.extend({ check: function() { return this.each(function() { this.checked = true; }); }, uncheck: function() { return this.each(function() { this.checked = false; }); } }); 结果: $("input[@type=checkbox]").check(); $("input[@type=radio]").uncheck(); jQuery.extend(object)jQuery.extend(object) 扩展jQuery对象本身。 用来在jQuery命名空间上增加新函数。 查看 'jQuery.fn.extend' 获取更多添加插件的信息。 -------------------------------------------------------------------------------- Extends the jQuery object itself. Can be used to add functions into the jQuery namespace. See 'jQuery.fn.extend' for more information on using this method to add Plugins. 返回值 jQuery 参数 object (Object) : 用以扩展 jQuery 对象 示例 在jQuery命名空间上增加两个函数。 jQuery 代码: jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, max: function(a, b) { return a > b ? a : b; } }); 结果: jQuery.min(2,3); // => 2 jQuery.max(4,5); // => 5 多库共存 jQuery.noConflict()jQuery.noConflict() 运行这个函数将变量$的控制权让渡给第一个实现它的那个库。 这有助于确保jQuery不会与其他库的$对象发生冲突。在运行这个函数后,就只能使用jQuery变量访问jQuery对象。例如,在要用到$("div p")的地方,就必须换成jQuery("div p")。 -------------------------------------------------------------------------------- Run this function to give control of the $ variable back to whichever library first implemented it. This helps to make sure that jQuery doesn't conflict with the $ object of other libraries. By using this function, you will only be able to access jQuery using the 'jQuery' variable. For example, where you used to do $("div p"), you now must do jQuery("div p"). 返回值 jQuery 示例 将$引用的对象映射回原始的对象。 jQuery 代码: jQuery.noConflict(); // 使用 jQuery jQuery("div p").hide(); // 使用其他库的 $() $("content").style.display = 'none'; -------------------------------------------------------------------------------- 恢复使用别名$,然后创建并执行一个函数,在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中,原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。 jQuery 代码: jQuery.noConflict(); (function($) { $(function() { // 使用 $ 作为 jQuery 别名的代码 }); })(jQuery); // 其他用 $ 作为别名的库的代码 -------------------------------------------------------------------------------- 创建一个新的别名用以在接下来的库中使用jQuery对象。 jQuery 代码: var j = jQuery.noConflict(); // 基于 jQuery 的代码 j("div p").hide(); // 基于其他库的 $() 代码 $("content").style.display = 'none'; jQuery.noConflict(extreme)jQuery.noConflict(extreme) 将$和jQuery的控制权都交还给原来的库。用之前请考虑清楚! 这是相对于简单的 noConflict 方法更极端的版本,因为这将完全重新定义jQuery。这通常用于一种极端的情况,比如你想要将jQuery嵌入一个高度冲突的环境。注意:调用此方法后极有可能导致插件失效。 -------------------------------------------------------------------------------- Revert control of both the $ and jQuery variables to their original owners. Use with discretion. This is a more-extreme version of the simple noConflict method, as this one will completely undo what jQuery has introduced. This is to be used in an extreme case where you'd like to embed jQuery into a high-conflict environment. NOTE: It's very likely that plugins won't work after this particular method has been called. 返回值 jQuery 参数 extreme (Boolean) : 传入 true 来允许彻底将jQuery变量还原 示例 完全将 jQuery 移到一个新的命名空间。 jQuery 代码: var dom = {}; dom.query = jQuery.noConflict(true); 结果: // 新 jQuery 的代码 dom.query("div p").hide(); // 另一个库 $() 的代码 $("content").style.display = 'none'; // 另一个版本 jQuery 的代码 jQuery("div > p").hide(); 选择器基本 #id#id 根据给定的ID匹配一个元素。 -------------------------------------------------------------------------------- Matches a single element with the given id attribute. 返回值 Element 参数 id (String) : 用于搜索的,通过元素的 id 属性中给定的值 示例 查找 ID 为"myDiv"的元素。 HTML 代码: <div id="notMe"><p>id="notMe"</p></div> <div id="myDiv">id="myDiv"</div> jQuery 代码: $("#myDiv"); 结果: [ <div id="myDiv">id="myDiv"</div> ] elementelement 根据给定的元素名匹配所有元素 -------------------------------------------------------------------------------- Matches all elements with the given name. 返回值 Array<Element> 参数 element (String) : 一个用于搜索的元素。指向 DOM 节点的标签名。 示例 查找一个 DIV 元素。 HTML 代码: <div>DIV1</div> <div>DIV2</div> <span>SPAN</span> jQuery 代码: $("div"); 结果: [ <div>DIV1</div>, <div>DIV2</div> ] .class.class 根据给定的类匹配元素。 -------------------------------------------------------------------------------- Matches all elements with the given class. 返回值 Array<Element> 参数 class (String) : 一个用以搜索的类。一个元素可以有多个类,只要有一个符合就能被匹配到。 示例 查找所有类是 "myClass" 的元素. HTML 代码: <div class="notMe">div class="notMe"</div> <div class="myClass">div class="myClass"</div> <span class="myClass">span class="myClass"</span> jQuery 代码: $(".myClass"); 结果: [ <div class="myClass">div class="myClass"</div>, <span class="myClass">span class="myClass"</span> ] ** 匹配所有元素 多用于结合上下文来搜索。 -------------------------------------------------------------------------------- Matches all elements. Most useful when combined with a context to search in. 返回值 Array<Element> 示例 找到每一个元素 HTML 代码: <div>DIV</div> <span>SPAN</span> <p>P</p> jQuery 代码: $("*") 结果: [ <div>DIV</div>, <span>SPAN</span>, <p>P</p> ] selector1,selector2,selectorNselector1,selector2,selectorN 将每一个选择器匹配到的元素合并后一起返回。 你可以指定任意多个选择器,并将匹配到的元素合并到一个结果内。 -------------------------------------------------------------------------------- Matches the combined results of all the specified selectors. You can specify any number of selectors to combine into a single result. 返回值 Array<Element> 参数 selector1 (Selector) : 一个有效的选择器 selector2 (Selector) : 另一个有效的选择器 selectorN (Selector) : (可选) 任意多个有效选择器 示例 找到匹配任意一个类的元素。 HTML 代码: <div>div</div> <p class="myClass">p class="myClass"</p> <span>span</span> <p class="notMyClass">p class="notMyClass"</p> jQuery 代码: $("div,span,p.myClass") 结果: [ <div>div</div>, <p class="myClass">p class="myClass"</p>, <span>span</span> ] 层级 ancestor descendantancestor descendant 在给定的祖先元素下匹配所有的后代元素 -------------------------------------------------------------------------------- Matches all descendant elements specified by descendant of elements specified by ancestor. 返回值 Array<Element> 参数 ancestor (Selector) : 任何有效选择器 descendant (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的后代元素 示例 找到表单中所有的 input 元素 HTML 代码: <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> jQuery 代码: $("form input") 结果: [ <input name="name" />, <input name="newsletter" /> ] parent > childparent > child 在给定的父元素下匹配所有的子元素 -------------------------------------------------------------------------------- Matches all child elements specified by child of elements specified by parent. 返回值 Array<Element> 参数 parent (Selector) : 任何有效选择器 child (Selector) : 用以匹配元素的选择器,并且它是第一个选择器的子元素 示例 匹配表单中所有的子级input元素。 HTML 代码: <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> jQuery 代码: $("form > input") 结果: [ <input name="name" /> ] prev + nextprev + next 匹配所有紧接在 prev 元素后的 next 元素 -------------------------------------------------------------------------------- Matches all next elements specified by next that are next to elements specified by prev. 返回值 Array<Element> 参数 prev (Selector) : 任何有效选择器 next (Selector) :一个有效选择器并且紧接着第一个选择器 示例 匹配所有跟在 label 后面的 input 元素 HTML 代码: <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> jQuery 代码: $("label + input") 结果: [ <input name="name" />, <input name="newsletter" /> ] prev ~ siblingsprev ~ siblings 匹配 prev 元素之后的所有 siblings 元素 -------------------------------------------------------------------------------- Matches all sibling elements after the "prev" element that match the filtering "siblings" selector. 返回值 Array<Element> 参数 prev (Selector) : 任何有效选择器 siblings (Selector) : 一个选择器,并且它作为第一个选择器的同辈 示例 找到所有与表单同辈的 input 元素 HTML 代码: <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> jQuery 代码: $("form ~ input") 结果: [ <input name="none" /> ] 简单 :first:first 匹配找到的第一个元素 -------------------------------------------------------------------------------- Matches the first selected element. 返回值 Element 示例 查找表格的第一行 HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:first") 结果: [ <tr><td>Header 1</td></tr> ] :last:last 匹配找到的最后一个元素 -------------------------------------------------------------------------------- Matches the last selected element. 返回值 Element 示例 查找表格的最后一行 HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:last") 结果: [ <tr><td>Value 2</td></tr> ] :not(selector):not(selector) 去除所有与给定选择器匹配的元素 -------------------------------------------------------------------------------- Removes all elements matching the given selector. 返回值 Array<Element> 参数 selector (Selector) : 用于筛选的选择器 示例 查找所有未选中的 input 元素 HTML 代码: <input name="apple" /> <input name="flower" checked="checked" /> jQuery 代码: $("input:not(:checked)") 结果: [ <input name="apple" /> ] :even:even 匹配所有索引值为偶数的元素,从 0 开始计数 -------------------------------------------------------------------------------- Matches even elements, zero-indexed. 返回值 Array<Element> 示例 查找表格的1、3、5...行(即索引值0、2、4...) HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:even") 结果: [ <tr><td>Header 1</td></tr>, <tr><td>Value 2</td></tr> ] :odd:odd 匹配所有索引值为奇数的元素,从 0 开始计数 -------------------------------------------------------------------------------- Matches odd elements, zero-indexed. 返回值 Array<Element> 示例 查找表格的2、4、6行(即索引值1、3、5...) HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:odd") 结果: [ <tr><td>Value 1</td></tr> ] :eq(index):eq(index) 匹配一个给定索引值的元素 -------------------------------------------------------------------------------- Matches a single element by its index. 返回值 Element 参数 index (Number) : 从 0 开始计数 示例 查找第二行 HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:eq(1)") 结果: [ <tr><td>Value 1</td></tr> ] :gt(index):gt(index) 匹配所有大于给定索引值的元素 -------------------------------------------------------------------------------- Matches all elements with an index above the given one. 返回值 Array<Element> 参数 index (Number) : 从 0 开始计数 示例 查找第二第三行,即索引值是1和2,也就是比0大 HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:gt(0)") 结果: [ <tr><td>Value 1</td></tr>, <tr><td>Value 2</td></tr> ] :lt(index):lt(index) 匹配所有小于给定索引值的元素 -------------------------------------------------------------------------------- Matches all elements with an index below the given one. 返回值 Array<Element> 参数 index (Number) : 从 0 开始计数 示例 查找第一第二行,即索引值是0和1,也就是比2小 HTML 代码: <table> <tr><td>Header 1</td></tr> <tr><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:lt(2)") 结果: [ <tr><td>Header 1</td></tr>, <tr><td>Value 1</td></tr> ] :header:header 匹配如 h1, h2, h3之类的标题元素 -------------------------------------------------------------------------------- Matches all elements that are headers, like h1, h2, h3 and so on. 返回值 Array<Element> 示例 给页面内所有标题加上背景色 HTML 代码: <h1>Header 1</h1> <p>Contents 1</p> <h2>Header 2</h2> <p>Contents 2</p> jQuery 代码: $(":header").css("background", "#EEE"); 结果: [ <h1 style="background:#EEE;">Header 1</h1>, <h2 style="background:#EEE;">Header 2</h2> ] :animated:animated 匹配所有没有在执行动画效果中的元素 -------------------------------------------------------------------------------- Matches all elements that are currently being animated. 返回值 Array<Element> 示例 只有对不在执行动画效果的元素执行一个动画特效 HTML 代码: <button id="run">Run</button><div></div> jQuery 代码: $("#run").click(function(){ $("div:not(:animated)").animate({ left: "+20" }, 1000); }); 内容 :contains(text):contains(text) 匹配包含给定文本的元素 -------------------------------------------------------------------------------- Matches elements which contain the given text. 返回值 Array<Element> 参数 text (String) : 一个用以查找的字符串 示例 查找所有包含 "John" 的 div 元素 HTML 代码: <div>John Resig</div> <div>George Martin</div> <div>Malcom John Sinclair</div> <div>J. Ohn jQuery 代码: $("div:contains('John')") 结果: [ <div>John Resig</div>, <div>Malcom John Sinclair</div> ] :empty:empty 匹配所有不包含子元素或者文本的空元素 -------------------------------------------------------------------------------- Matches all elements that are empty, be it elements or text. 返回值 Array<Element> 示例 查找所有不包含子元素或者文本的空元素 HTML 代码: <table> <tr><td>Value 1</td><td></td></tr> <tr><td>Value 2</td><td></td></tr> </table> jQuery 代码: $("td:empty") 结果: [ <td></td>, <td></td> ] :has(selector):has(selector) 匹配含有选择器所匹配的元素的元素 -------------------------------------------------------------------------------- Matches elements which contain at least one element that matches the specified selector. 返回值 Array<Element> 参数 selector (Selector) : 一个用于筛选的选择器 示例 给所有包含 p 元素的 div 元素添加一个 text 类 HTML 代码: <div><p>Hello</p></div> <div>Hello again!</div> jQuery 代码: $("div:has(p)").addClass("test"); 结果: [ <div class="test"><p>Hello</p></div> ] :parent:parent 匹配含有子元素或者文本的元素 -------------------------------------------------------------------------------- Matches all elements that are parents - they have child elements, including text. 返回值 Array<Element> 示例 查找所有含有子元素或者文本的 td 元素 HTML 代码: <table> <tr><td>Value 1</td><td></td></tr> <tr><td>Value 2</td><td></td></tr> </table> jQuery 代码: $("td:parent") 结果: [ <td>Value 1</td>, <td>Value 1</td> ] 可见性 :hidden:hidden 匹配所有的不可见元素,input 元素的 type 属性为 "hidden" 的话也会被匹配到 -------------------------------------------------------------------------------- Matches all elements that are hidden, or input elements of type "hidden". 返回值 Array<Element> 示例 查找所有不可见的 tr 元素 HTML 代码: <table> <tr style="display:none"><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:hidden") 结果: [ <tr style="display:none"><td>Value 1</td></tr> ] :visible:visible 匹配所有的可见元素 -------------------------------------------------------------------------------- Matches all elements that are visible. 返回值 Array<Element> 示例 查找所有可见的 tr 元素 HTML 代码: <table> <tr style="display:none"><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:visible") 结果: [ <tr><td>Value 2</td></tr> ] 属性 [attribute][attribute] 匹配包含给定属性的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute. 返回值 Array<Element> 参数 attribute (String) : 属性名 示例 查找所有含有 id 属性的 div 元素 HTML 代码: <div> <p>Hello!</p> </div> <div id="test2"></div> jQuery 代码: $("div[id]") 结果: [ <div id="test2"></div> ] [attribute=value][attribute=value] 匹配给定的属性是某个特定值的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute with a certain value. 返回值 Array<Element> 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 属性是 newsletter 的 input 元素 HTML 代码: '<input type="checkbox" name="newsletter" value="Hot Fuzz" /> <input type="checkbox" name="newsletter" value="Cold Fusion" /> <input type="checkbox" name="accept" value="Evil Plans" /> jQuery 代码: $("input[name='newsletter']").attr("checked", true); 结果: [ <input type="checkbox" name="newsletter" value="Hot Fuzz" checked="true" />, <input type="checkbox" name="newsletter" value="Cold Fusion" checked="true" /> ] [attribute!=value][attribute!=value] 匹配给定的属性是不包含某个特定值的元素 -------------------------------------------------------------------------------- Matches elements that don't have the specified attribute with a certain value. 返回值 Array<Element> 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 属性不是 newsletter 的 input 元素 HTML 代码: '<input type="checkbox" name="newsletter" value="Hot Fuzz" /> <input type="checkbox" name="newsletter" value="Cold Fusion" /> <input type="checkbox" name="accept" value="Evil Plans" /> jQuery 代码: $("input[name!='newsletter']").attr("checked", true); 结果: [ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ] [attribute^=value][attribute^=value] 匹配给定的属性是以某些值开始的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it starts with a certain value. 返回值 Array<Element> 参数 attribute (String) : 属性名 value ( String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 以 'news' 开始的 input 元素 HTML 代码: <input name="newsletter" /> <input name="milkman" /> <input name="newsboy" /> jQuery 代码: $("input[name^='news']") 结果: [ <input name="newsletter" />, <input name="newsboy" /> ] [attribute$=value][attribute$=value] 匹配给定的属性是以某些值结尾的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it ends with a certain value. 返回值 Array<Element> 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 以 'letter' 结尾的 input 元素 HTML 代码: <input name="newsletter" /> <input name="milkman" /> <input name="jobletter" /> jQuery 代码: $("input[name$='letter']") 结果: [ <input name="newsletter" />, <input name="jobletter" /> ] [attribute*=value][attribute*=value] 匹配给定的属性是以包含某些值的元素 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it contains a certain value. 返回值 Array<Element> 参数 attribute (String) : 属性名 value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。 示例 查找所有 name 包含 'man' 的 input 元素 HTML 代码: <input name="man-news" /> <input name="milkman" /> <input name="letterman2" /> <input name="newmilk" /> jQuery 代码: $("input[name*='man']") 结果: [ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ] [selector1][selector2][selectorN][selector1][selector2][selectorN] 复合属性选择器,需要同时满足多个条件时使用。 -------------------------------------------------------------------------------- Matches elements that have the specified attribute and it contains a certain value. 返回值 Array<Element> 参数 selector1 (Selector) : 属性选择器 selector2 (Selector) : 另一个属性选择器,用以进一步缩小范围 selectorN (Selector) : 任意多个属性选择器 示例 找到所有含有 id 属性,并且它的 name 属性是以 man 结尾的 HTML 代码: <input id="man-news" name="man-news" /> <input name="milkman" /> <input id="letterman" name="new-letterman" /> <input name="newmilk" /> jQuery 代码: $("input[id][name$='man']") 结果: [ <input id="letterman" name="new-letterman" /> ] 子元素 :nth-child(index/even/odd/equation):nth-child(index/even/odd/equation) 匹配其父元素下的第N个子或奇偶元素 ':eq(index)' 只匹配一个元素,而这个将为每一个父元素匹配子元素。:nth-child从1开始的,而:eq()是从0算起的! 可以使用: nth-child(even) :nth-child(odd) :nth-child(3n) :nth-child(2) :nth-child(3n+1) :nth-child(3n+2) -------------------------------------------------------------------------------- Matches the nth-child of its parent. While ':eq(index)' matches only a single element, this matches more then one: One for each parent. The specified index is one-indexed, in contrast to :eq() which starst at zero. 返回值 Array<Element> 参数 index (Number) : 要匹配元素的序号,从1开始 示例 在每个 ul 查找第 2 个li HTML 代码: <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul> jQuery 代码: $("ul li:nth-child(2)") 结果: [ <li>Karl</li>, <li>Tane</li> ] :first-child:first-child 匹配第一个子元素 ':first' 只匹配一个元素,而此选择符将为每个父元素匹配一个子元素 -------------------------------------------------------------------------------- Matches the first child of its parent. While ':first' matches only a single element, this matches more then one: One for each parent. 返回值 Array<Element> 示例 在每个 ul 中查找第一个 li HTML 代码: <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul> jQuery 代码: $("ul li:first-child") 结果: [ <li>John</li>, <li>Glen</li> ] :last-child:last-child 匹配最后一个子元素 ':last'只匹配一个元素,而此选择符将为每个父元素匹配一个子元素 -------------------------------------------------------------------------------- Matches the last child of its parent. While ':last' matches only a single element, this matches more then one: One for each parent. 返回值 Array<Element> 示例 在每个 ul 中查找最后一个 li HTML 代码: <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul> jQuery 代码: $("ul li:last-child") 结果: [ <li>Brandon</li>, <li>Ralph</li> ] :only-child:only-child 如果某个元素是父元素中唯一的子元素,那将会被匹配 如果父元素中含有其他元素,那将不会被匹配。 -------------------------------------------------------------------------------- Matches the only child of its parent. If the parent has other child elements, nothing is matched. 返回值 Array<Element> 示例 在 ul 中查找是唯一子元素的 li HTML 代码: <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> jQuery 代码: $("ul li:only-child") 结果: [ <li>Glen</li> ] 表单 :input:input 匹配所有 input, textarea, select 和 button 元素 -------------------------------------------------------------------------------- Matches all input, textarea, select and button elements. 返回值 Array<Element> 示例 查找所有的input元素 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":input") 结果: [ <input type="text" />, <input type="checkbox" />, <input type="radio" />, <input type="image" />, <input type="file" />, <input type="submit" />, <input type="reset" />, <input type="password" />, <input type="button" /> ] :text:text 匹配所有的单行文本框 -------------------------------------------------------------------------------- Matches all input elements of type text. 返回值 Array<Element> 示例 查找所有文本框 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":text") 结果: [ <input type="text" /> ] :password:password 匹配所有密码框 -------------------------------------------------------------------------------- Matches all input elements of type password. 返回值 Array<Element> 示例 查找所有密码框 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":password") 结果: [ <input type="password" /> ] :radio:radio 匹配所有单选按钮 -------------------------------------------------------------------------------- Matches all input elements of type radio. 返回值 Array<Element> 示例 查找所有单选按钮 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":radio") 结果: [ <input type="radio" /> ] :checkbox:checkbox 匹配所有复选框 -------------------------------------------------------------------------------- Matches all input elements of type checkbox. 返回值 Array<Element> 示例 查找所有复选框 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":checkbox") 结果: [ <input type="checkbox" /> ] :submit:submit 匹配所有提交按钮 -------------------------------------------------------------------------------- Matches all input elements of type submit. 返回值 Array<Element> 示例 查找所有提交按钮 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":submit") 结果: [ <input type="submit" /> ] :image:image 匹配所有图像域 -------------------------------------------------------------------------------- Matches all input elements of type image. 返回值 Array<Element> 示例 匹配所有图像域 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":image") 结果: [ <input type="image" /> ] :reset:reset 匹配所有重置按钮 -------------------------------------------------------------------------------- Matches all input elements of type reset. 返回值 Array<Element> 示例 查找所有重置按钮 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":reset") 结果: [ <input type="reset" /> ] :button:button 匹配所有按钮 -------------------------------------------------------------------------------- Matches all input elements of type button. 返回值 Array<Element> 示例 查找所有按钮. HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":button") 结果: [ <input type="button" />,<button></button> ] :file:file 匹配所有文件域 -------------------------------------------------------------------------------- Matches all input elements of type file. 返回值 Array<Element> 示例 查找所有文件域 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> jQuery 代码: $(":file") 结果: [ <input type="file" /> ] :hidden:hidden 匹配所有不可见元素,或者type为hidden的元素 -------------------------------------------------------------------------------- Matches all elements that are hidden, or input elements of type "hidden". 返回值 Array<Element> 示例 查找隐藏的 tr HTML 代码: <table> <tr style="display:none"><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> jQuery 代码: $("tr:hidden") 结果: [ <tr style="display:none"><td>Value 1</td></tr> ] -------------------------------------------------------------------------------- 匹配type为hidden的元素 HTML 代码: <form> <input type="text" name="email" /> <input type="hidden" name="id" /> </form> jQuery 代码: $("input:hidden") 结果: [ <input type="hidden" name="id" /> ] 表单对象属性 :enabled:enabled 匹配所有可用元素 -------------------------------------------------------------------------------- Matches all elements that are enabled. 返回值 Array<Element> 示例 查找所有可用的input元素 HTML 代码: <form> <input name="email" disabled="disabled" /> <input name="id" /> </form> jQuery 代码: $("input:enabled") 结果: [ <input name="id" /> ] :disabled:disabled 匹配所有不可用元素 -------------------------------------------------------------------------------- Matches all elements that are disabled. 返回值 Array<Element> 示例 查找所有不可用的input元素 HTML 代码: <form> <input name="email" disabled="disabled" /> <input name="id" /> </form> jQuery 代码: $("input:disabled") 结果: [ <input name="email" disabled="disabled" /> ] :checked:checked 匹配所有选中的复选框元素 -------------------------------------------------------------------------------- Matches all elements that are checked. 返回值 Array<Element> 示例 查找所有选中的复选框元素 HTML 代码: <form> <input type="checkbox" name="newsletter" checked="checked" value="Daily" /> <input type="checkbox" name="newsletter" value="Weekly" /> <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> </form> jQuery 代码: $("input:checked") 结果: [ <input type="checkbox" name="newsletter" checked="checked" value="Daily" />, <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ] :selected:selected 匹配所有选中的选项元素 -------------------------------------------------------------------------------- Matches all elements that are selected. 返回值 Array<Element> 示例 查找所有选中的选项元素 HTML 代码: <select> <option value="1">Flowers</option> <option value="2" selected="selected">Gardens</option> <option value="3">Trees</option> </select> jQuery 代码: $("select option:selected") 结果: [ <option value="2" selected="selected">Gardens</option> ] -------------------------------------------------------------------------------- Finds all option elements that are selected. HTML 代码: <select multiple="multiple"> <option value="1">Flowers</option> <option value="2" selected="selected">Gardens</option> <option value="3" selected="selected">Trees</option> </select> jQuery 代码: $("select option:selected") 结果: [ <option value="2" selected="selected">Gardens</option>, <option value="3" selected="selected">Trees</option> ] 属性属性 attr(name)attr(name) 取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined 。 -------------------------------------------------------------------------------- Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned. 返回值 Object 参数 name (String) : 属性名称 示例 返回文档中第一个图像的src属性值。 HTML 代码: <img src="test.jpg"/> jQuery 代码: $("img").attr("src"); 结果: test.jpg attr(properties)attr(properties) 将一个“名/值”形式的对象设置为所有匹配元素的属性。 这是一种在所有匹配元素中批量设置很多属性的最佳方式。 注意,如果你要设置对象的class属性,你必须使用'className' 作为属性名。或者你可以直接使用.addClass( class ) 和 .removeClass( class ). -------------------------------------------------------------------------------- Set a key/value object as properties to all matched elements. This serves as the best way to set a large number of properties on all matched elements. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ). 返回值 jQuery 参数 properties (Map) : 作为属性的“名/值对”对象 示例 为所有图像设置src和alt属性。 HTML 代码: <img/> jQuery 代码: $("img").attr({ src: "test.jpg", alt: "Test Image" }); 结果: [ <img src= "test.jpg" alt:="Test Image" /> ] -------------------------------------------------------------------------------- attr(key,value)attr(key,value) 为所有匹配的元素设置一个属性值。 -------------------------------------------------------------------------------- Set a single property to a value, on all matched elements. 返回值 jQuery 参数 key (String) : 属性名称 value (Object) : 属性值 示例 为所有图像设置src属性。 HTML 代码: <img/> <img/> jQuery 代码: $("img").attr("src","test.jpg"); 结果: [ <img src= "test.jpg" /> , <img src= "test.jpg" /> ] attr(key,fn)attr(key,fn) 为所有匹配的元素设置一个计算的属性值。 不提供值,而是提供一个函数,由这个函数计算的值作为属性值。 -------------------------------------------------------------------------------- Set a single property to a computed value, on all matched elements. Instead of supplying a string value as described 'above', a function is provided that computes the value. 返回值 jQuery 参数 key (String) : 属性名称 fn (Function) : 返回值的函数 范围:当前元素, 参数: 当前元素的索引值 示例 把src属性的值设置为title属性的值。 HTML 代码: <img src="test.jpg"/> jQuery 代码: $("img").attr("title", function() { return this.src }); 结果: <img src="test.jpg" title="test.jpg" /> removeAttr(name)removeAttr(name) 从每一个匹配的元素中删除一个属性 -------------------------------------------------------------------------------- Remove an attribute from each of the matched elements. 返回值 jQuery 参数 name (String) : 要删除的属性名 示例 将文档中图像的src属性删除 HTML 代码: <img src="test.jpg"/> jQuery 代码: $("img").removeAttr("src"); 结果: [ <img /> ] 类 addClass(class)addClass(class) 为每个匹配的元素添加指定的类名。 -------------------------------------------------------------------------------- Adds the specified class(es) to each of the set of matched elements. 返回值 jQuery 参数 class (String) : 一个或多个要添加到元素中的CSS类名,请用空格分开 示例 为匹配的元素加上 'selected' 类 HTML 代码: <p>Hello</p> jQuery 代码: $("p").addClass("selected"); 结果: [ <p class="selected">Hello</p> ] -------------------------------------------------------------------------------- 为匹配的元素加上 selected highlight 类 HTML 代码: <p>Hello</p> jQuery 代码: $("p").addClass("selected highlight"); 结果: [ <p class="selected highlight">Hello</p> ] removeClass(class)removeClass(class) 从所有匹配的元素中删除全部或者指定的类。 -------------------------------------------------------------------------------- Removes all or the specified class(es) from the set of matched elements. 返回值 jQuery 参数 class (String) : (可选) 一个或多个要删除的CSS类名,请用空格分开 示例 从匹配的元素中删除 'selected' 类 HTML 代码: <p class="selected first">Hello</p> jQuery 代码: $("p").removeClass("selected"); 结果: [ <p>Hello</p> ] -------------------------------------------------------------------------------- 删除匹配元素的所有类 HTML 代码: <p class="selected first">Hello</p> jQuery 代码: $("p").removeClass(); 结果: [ <p>Hello</p> ] toggleClass(class)toggleClass(class) 如果存在(不存在)就删除(添加)一个类。 -------------------------------------------------------------------------------- Adds the specified class if it is not present, removes the specified class if it is present. 返回值 jQuery 参数 class (String) :CSS类名 示例 为匹配的元素切换 'selected' 类 HTML 代码: <p>Hello</p><p class="selected">Hello Again</p> jQuery 代码: $("p").toggleClass("selected"); 结果: [ <p class="selected">Hello</p>, <p>Hello Again</p> ] Html代码 html()html() 取得第一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档。 -------------------------------------------------------------------------------- Get the html contents of the first matched element. This property is not available on XML documents (although it will work for XHTML documents). 返回值 String 示例 HTML 代码: <div><p>Hello</p></div> jQuery 代码: $("div").html(); 结果: Hello html(val)html(val) 设置每一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档。 -------------------------------------------------------------------------------- Set the html contents of every matched element. This property is not available on XML documents (although it will work for XHTML documents). 返回值 jQuery 参数 val (String) : 用于设定HTML内容的值 示例 HTML 代码: <div></div> jQuery 代码: $("div").html("<p>Hello Again</p>"); 结果: [ <div><p>Hello Again</p></div> ] 文本 text()text() 取得所有匹配元素的内容。 结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。 -------------------------------------------------------------------------------- Get the text contents of all matched elements. The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents. 返回值 String 示例 HTML 代码: <p><b>Test</b> Paragraph.</p><p>Paraparagraph</p> jQuery 代码: $("p").text(); 结果: Test Paragraph.Paraparagraph text(val)text(val) 设置所有匹配元素的文本内容 与 html() 类似, 但将编码 HTML (将 "<" 和 ">" 替换成相应的HTML实体). -------------------------------------------------------------------------------- Set the text contents of all matched elements. Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities). 返回值 jQuery 参数 val (String) : 用于设置元素内容的文本 示例 HTML 代码: <p>Test Paragraph.</p> jQuery 代码: $("p").text("<b>Some</b> new text."); 结果: [ <p><b>Some</b> new text.</p> ] 值 val()val() 获得第一个匹配元素的当前值。 在 jQuery 1.2 中,可以返回任意元素的值了。包括select。如果多选,将返回一个数组,其包含所选的值。 -------------------------------------------------------------------------------- Get the content of the value attribute of the first matched element. In jQuery 1.2, a value is now returned for all elements, including selects. For multiple selects an array of values is returned. 返回值 String,Array 示例 获得单个select的值和多选select的值。 HTML 代码: <p></p><br/> <select id="single"> <option>Single</option> <option>Single2</option> </select> <select id="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select> jQuery 代码: $("p").append( "<b>Single:</b> " + $("#single").val() + " <b>Multiple:</b> " + $("#multiple").val().join(", ") ); 结果: [ <p><b>Single:</b>Single<b>Multiple:</b>Multiple, Multiple3</p>] -------------------------------------------------------------------------------- 获取文本框中的值 HTML 代码: <input type="text" value="some text"/> jQuery 代码: $("input").val(); 结果: some text val(val)val(val) 设置每一个匹配元素的值。 在 jQuery 1.2, 这也可以为select元件赋值 -------------------------------------------------------------------------------- Set the value attribute of every matched element. In jQuery 1.2, this is also able to set the value of select elements, but selecting the appropriate options. 返回值 jQuery 参数 val (String) : 要设置的值。 示例 设定文本框的值 HTML 代码: <input type="text"/> jQuery 代码: $("input").val("hello world!"); val(val)val(val) check,select,radio等都能使用为之赋值 返回值 jQuery 参数 val (Array<String>) : 用于 check/select 的值 示例 设定一个select和一个多选的select的值 HTML 代码: <select id="single"> <option>Single</option> <option>Single2</option> </select> <select id="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select><br/> <input type="checkbox" value="check1"/> check1 <input type="checkbox" value="check2"/> check2 <input type="radio" value="radio1"/> radio1 <input type="radio" value="radio2"/> radio2 jQuery 代码: $("#single").val("Single2"); $("#multiple").val(["Multiple2", "Multiple3"]); $("input").val(["check2", "radio1"]); 筛选过滤 eq(index)eq(index) 获取第N个元素 这个元素的位置是从0算起。 -------------------------------------------------------------------------------- Reduce the set of matched elements to a single element. The position of the element in the set of matched elements starts at 0 and goes to length - 1. 返回值 jQuery 参数 index (Integer) :元素在jQuery对象中的索引 示例 获取匹配的第二个元素 HTML 代码: <p> This is just a test.</p> <p> So is this</p> jQuery 代码: $("p").eq(1) 结果: [ <p> So is this</p> ] hasClass(class)hasClass(class) 检查当前的元素是否含有某个特定的类,如果有,则返回true。 这其实就是 is("." + class)。 -------------------------------------------------------------------------------- Checks the current selection against a class and returns true, if at least one element of the selection has the given class. This is an alternative to is("." + class). 返回值 Boolean 参数 class (String) : 用于匹配的类名 示例 给包含有某个类的元素进行一个动画。 HTML 代码: <div class="protected"></div><div></div> jQuery 代码: $("div").click(function(){ if ( $(this).hasClass("protected") ) $(this) .animate({ left: -10 }) .animate({ left: 10 }) .animate({ left: -10 }) .animate({ left: 10 }) .animate({ left: 0 }); }); filter(expr)filter(expr) 筛选出与指定表达式匹配的元素集合。 这个方法用于缩小匹配的范围。用逗号分隔多个表达式 -------------------------------------------------------------------------------- Removes all elements from the set of matched elements that do not match the specified expression(s). This method is used to narrow down the results of a search. Provide a comma-separated list of expressions to apply multiple filters at once. 返回值 jQuery 参数 expr (Expression) : 表达式 示例 保留带有select类的元素 HTML 代码: <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p> jQuery 代码: $("p").filter(".selected") 结果: [ <p class="selected">And Again</p> ] -------------------------------------------------------------------------------- 保留第一个以及带有select类的元素 HTML 代码: <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p> jQuery 代码: $("p").filter(".selected, :first") 结果: [ <p>Hello</p>, <p class="selected">And Again</p> ] filter(fn)filter(fn) 筛选出与指定函数返回值匹配的元素集合 这个函数内部将对每个对象计算一次 (正如 '$.each'). 如果调用的函数返回false则这个元素被删除,否则就会保留。 -------------------------------------------------------------------------------- Removes all elements from the set of matched elements that does not match the specified function. The function is called with a context equal to the current element (just like '$.each'). If the function returns false, then the element is removed - anything else and the element is kept. 返回值 jQuery 参数 fn (Function) : 传递进filter的函数 示例 保留子元素中不含有ol的元素。 HTML 代码: <p><ol><li>Hello</li></ol></p><p>How are you?</p> jQuery 代码: $("p").filter(function(index) { return $("ol", this).length == 0; }); 结果: [ <p>How are you?</p> ] is(expr)is(expr) 用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true。 如果没有元素符合,或者表达式无效,都返回'false'. 'filter' 内部实际也是在调用这个函数,所以,filter()函数原有的规则在这里也适用。 -------------------------------------------------------------------------------- Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression. If no element fits, or the expression is not valid, then the response will be 'false'. 'filter' is used internally, therefore all rules that apply there apply here, as well. 返回值 Boolean 参数 expr (String) :用于筛选的表达式 示例 由于input元素的父元素是一个表单元素,所以返回true。 HTML 代码: <form><input type="checkbox" /></form> jQuery 代码: $("input[type='checkbox']").parent().is("form") 结果: true map(callback)map(callback) 将一组元素转换成其他数组(不论是否是元素数组) 你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用'$.map()'来方便的建立。 -------------------------------------------------------------------------------- Translate a set of elements into another set of values (which may, or may not, be elements). You could use this to build lists of values, attributes, css values - or even perform special, custom, selector transformations. This is provided as a convenience method for using '$.map()'. 返回值 jQuery 参数 callback (Function) : 给每个元素执行的函数 示例 把form中的每

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值