jQuery IN ACTION 小笔记

jQuery in Action 我的读书笔记

1.      $是jQuery的别名,所以$(“#idName”),其实就是jQuery(“#idName”)

2.      选择器可以联合使用

比如,以前我只知道可以用$(“#idName”)找到ID為“idName”的那些节点,用$(“.className”)找到class为“className”的那些节点,原来可以用$(“#idName.className”)把符合两个条件的找出来,害的我以前还要大循环里做条件匹配。

所以有$("body > div:has(a)")这样的组合出来,也就省事不少。

 3.      以前我们在处理页面的时候,往往要等到整个页面加载完了才会进行处理,所以会用到onload,但是onload要等所有的东西都加载完了才执行,谁知道Flash,QuickTime这些大家伙啥时候能够完全OK呀,其实只要节点加载完了就可以执行操作了,所以jQuery有了ready这个function。

jQuery(document).ready(function() {

$("div.notLongForThisWorld").hide();

});

 书上原话是“jQuery提供一个简单的方式就是等到DOM Tree加载完(而不是等待外部资源的加载)就立即执行代码。

4.      在jQuery中自定义函数,比如jQuery中并没有disabled一组元素的函数,但是我们又很想用,比如

$("form#myForm. input.special").disable();

那么我们可以立即去定义一个

$.fn.disable = function() {

   return this.each(function() {

      if (typeof this.disabled != "undefined") this.disabled = true;

   });

}

真的很方便.

5.  选择器实例

例子                描述

   a                                      选择所有的标签 a 元素

   #speicalID                    选择那些ID值为speicalID的元素

    .specialClass             选择那些Class值为specialClass的元素

a#specialId.specialClass  这个我就不说了

p a.specialClass                 同上

 

6.      $(“form. input”)和$(“form. > input”)虽然都是有包含关系,不过前者来的更随意些,后者只能是父子关系,而不是简单的包含关系,所以祖孙关系是不行的。

7.      匹配关系一览

*           Matches any element.

E          Matches all elements with tag name E.

E F       Matches all elements with tag name F that are descendents of E.

E>F      Matches all elements with tag name F that are direct children of E.

E+F     Matches all elements with tag name F that are immediately preceded by sibling E.

E~F     Matches all elements with tag name F preceded by any sibling E.

E.C      Matches all elements with tag name E with class name C. Omitting E is the same as *.C.

E#I      Matches all elements with tag name E with the id of I. Omitting E is the same as *.I.

E[A]     Matches all elements with tag name E that have attribute A of any value.

E[A=V] Matches all elements with tag name E that have attribute A whose value is exactly V.

E[A^=V] Matches all elements with tag name E that have attribute A whose value starts with V.

E[A$=V] Matches all elements with tag name E that have attribute A whose value ends with V.

E[A!=V] Matches all elements with tag name E that have attribute A whose value does not match the value V, or

that lack attribute A completely.

E[A*=V] Matches all elements with tag name E that have attribute A whose value contains V.

 8.      选择还可以使用位置参数

比如,a:first 即匹配第一个a,相应还有odd,even,last-child(包含关系时)

    :first The first match within the context. li a:first returns the first link that is a

              descendant of a list item.

   :last  The last match within the context. li a:first returns the last link that is a

             descendant of a list item.

   :first-child The first child element. lifers-child returns the first list item of each list.

   :last-child  The last child element. li:first-child returns the last list item of each list.

   :only-child  Returns all elements that have no siblings.

   :nth-child(n) The nth child element. li:nth-child(2) returns the second list item of each

                          list.

  :nth-child(even|odd) Even or odd children. li:nth-child(even) returns the even list items of each

                           list.

  :nth-child(Xn+Y) The nth child element computed by the supplied formula. If Y is 0, it may be omitted.

  li:nth-child(3n) returns every 3rd list item, whereas li:nthchild(5n+1) returns the item after every 5th element.

  :even Even elements. li:even returns every even list item.

  :odd Odd elements. li:odd returns every even list item.

  :eq(n) The nth matching element.

  :get(n) Matching elements after and excluding the nth matching element.

  :lt(n) Matching elements before and excluding the nth matching element.

 

9.      几个内容函数的区别

.html() 返回带有完整的html标签等

.text()  返回文本

.val()   返回属性value的值

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/554557/viewspace-628653/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/554557/viewspace-628653/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
in action系列的又一经典,介绍JQuery框架的入门和提升。 JQueryjQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多javascript高手加入其team,包括来自德国的Jörn Zaefferer,罗马尼亚的Stefan Petre等等。   jQuery是继prototype之后又一个优秀的Javascrīpt框架。其宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。   它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。   jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。   jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。   jQuery能够使用户的html页保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需定义id即可。   Jquery是继prototype之后又一个优秀的Javascrīpt框架。对prototype我使用不多,简单了解过。但使用上jquery之后,马上被她的优雅吸引住了。有人使用这样的一比喻来比较prototype和jquery:prototype就像Java,而jquery就像ruby.实际上我比较喜欢java(少接触Ruby 罢了)但是jquery的简单的实用的确有相当大的吸引力啊!在项目里我把jquery作为自己唯一的框架类包。使用其间也有一点点心得,其实这些心得,在jquery的文档上面也可能有讲,不过还是记下来,以备忘罢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值