首页 > web前端 > js教程 > 正文

jQuery 1.4 15个你应该知道的新特性(译)_jquery

php中文网
发布: 2016-05-16 18:35:53
原创
1047人浏览过

首先现在你可以从这里下载最新的1.4的版本了: http://code.jquery.com/jquery-1.4.js

1. 传递属性给jQuery

1.4之前的版本中jQuery就通过"attr”方法支持了添加属性到元素集合中,"attr”方法不仅可以传递属性名和值,也可以传递包含有多个属性的对象集合。在1.4中,除了可以创建新的对象,现在它更能将属性对象作为参数传递给jQuery函数或对象自身。如你可以创建一个含有多个属性的链接元素,通过1.4代码如下:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<a/&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, { <BR&gt;   id: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;gid</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   href: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;http://www.google.com</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   title: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;google非和谐版</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   rel: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;external</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   text: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;进入Google!</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt; <BR&gt; }); </SPAN&gt;</DIV&gt;
登录后复制

你可能会注意到"text”属性并且猜测它是干吗的,因为a标签是没有"text”属性的。呵呵,当你传递某些属性时,jquery 1.4同样会检查并使用它自己的方法。因此上面的"text”属性可以让jQuery调用它的".text()”方法并将"进入Google!“作为它的唯一参数。

这里给出一个更好的例子:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<div/&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, { <BR&gt;   id: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;foo</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   css: { <BR&gt;     fontWeight: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;700</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;     color: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;green</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt; <BR&gt;   }, <BR&gt;   click: </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(){ <BR&gt;     alert(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;Foo被点击过!</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;); <BR&gt;   } <BR&gt; }); </SPAN&gt;</DIV&gt;
登录后复制

"id”属性是作为一个普通属性被添加的。但"css”和"click”属性则对应特定的jQuery方法。上面的代码在1.4之前一种写法如下:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<div/&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;) <BR&gt;   .attr(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;id</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;foo</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;) <BR&gt;   .css({ <BR&gt;     fontWeight: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;700</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;     color: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;green</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt; <BR&gt;   }) <BR&gt;   .click(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(){ <BR&gt;     alert(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;Foo被点击过!</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;); <BR&gt;   }); </SPAN&gt;</DIV&gt;
登录后复制

 

更多关于jQuery特性

2. 所有的东西都可以"until“了

1.4新增了三个对DOM操作的方法,他们分别是"<font face="新宋体"&gt;nextUntil</font&gt;",  "<font face="新宋体"&gt;prevUntil</font&gt;"  和  "<font face="新宋体"&gt;parentsUntil</font&gt;"。 这些方法会按照某个顺序去遍历DOM对象直到满足指定的筛选条件。假设我们有一个水果列表:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;ul</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;苹果</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;香蕉</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;葡萄</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;草莓</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;例子</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;桃子</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;ul</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;</DIV&gt;
登录后复制

如果你想选择所有在"Apple”和"Strawberry”之间的元素。代码可以这样写:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;ul li:contains(苹果)</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).nextUntil(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;:contains(梨子)</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;); <BR&gt; </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 得到 香蕉,葡萄,草莓</SPAN&gt;</DIV&gt;
登录后复制

更多关于: prevUntil, nextUntil, parentsUntil

3. 绑定多个事件

与通过jquery链绑定多个方法到一起相比,现在你可以通过一次调用来绑定所有这些事件,如:

jQuery 1.4 15个你应该知道的新特性(译)_jqueryjQuery 1.4 15个你应该知道的新特性(译)_jquery
<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN   style="max-width:90%"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;#foo).bind({ <BR&gt;   click: function() { <BR&gt;     // 具体代码 <BR&gt;   }, <BR&gt;   mouseover: function() { <BR&gt;     // 具体代码<BR&gt;   }, <BR&gt;   mouseout: function() { <BR&gt;     // 具体代码 <BR&gt;   } <BR&gt; }) </SPAN&gt;</DIV&gt;
登录后复制

 

你也可以通过 "<font face="新宋体"&gt;.one()</font&gt;"方法操作。

更多关于.bind(…)

4. Per-Property Easing

与给一个动画仅仅定义一个缓动效果相比,现在你可以给你想要添加动画效果的属性定义不同的缓动函数了。jQuery包含两个缓动函数,旋转(默认)和线性移动。如果你需要其他的效果的话,你需要去单独下载它们 !

要给每个属性指定一个缓动函数的话,你可以简单的通过属性数组来达到目的,在数组中,第一个值是你想要实现的效果值,第二个参数则是使用的缓动函数,如:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;#foo</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).animate({ <BR&gt;   left: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;500</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   top: [</SPAN&gt;<SPAN style="COLOR: #000000"&gt;500</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;easeOutBounce</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;] <BR&gt; }, </SPAN&gt;<SPAN style="COLOR: #000000"&gt;2000</SPAN&gt;<SPAN style="COLOR: #000000"&gt;); </SPAN&gt;</DIV&gt;
登录后复制

 

查看代码演示!

你也可以以键值对的形式在那些可选设置项中单独指定属性缓动函数:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;#foo</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).animate({ <BR&gt;   left: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;500</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   top: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;500</SPAN&gt;<SPAN style="COLOR: #000000"&gt; <BR&gt; }, { <BR&gt;   duration: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;2000</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, <BR&gt;   specialEasing: { <BR&gt;     top: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;easeOutBounce</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt; <BR&gt;   } <BR&gt; }); </SPAN&gt;</DIV&gt;
登录后复制

 

原文作者注 - 小小谦虚一下,这个新特性是作者James Padolsey的主意!

更多关于per-property easing

5. 新的Live事件!

1.4添加了对"submit”,"change”,"focus”,和"blur”事件的委托支持。在jQuery中,我们可以使用".live()”方法去委托事件。这在你需要注册事件到大量元素中或新的元素需要不断的被添加(使用".live()”会比不断的重新绑定事件更高效)将会很有用。

但是,注意!如果你需要委托"focus”和"blur”事件的时候,你必须使用"focusin”和"focusout”事件名称!

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;input</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).live(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;focusin</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(){<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt;具体操作</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;});</SPAN&gt;</DIV&gt;
登录后复制

 

6. 控制函数的上下文

jQuery 1.4提供了一个新的叫"proxy”的函数,它在jQuery命名空间下。该函数需要两个参数,不管是代码域还是方法名,或者是函数和其所处的代码域。t"this”关键字在JavaScrip中是相当难掌握的。有时候你可能期待它指的是你之前创建的某个对象,而不是一个你可能没想到的元素。

举例说明,假设我们有一个"app”对象,它有两个属性,一个是"clickHandler”方法, 另外一个则是一个config对象。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #0000ff"&gt;var</SPAN&gt;<SPAN style="COLOR: #000000"&gt; app </SPAN&gt;<SPAN style="COLOR: #000000"&gt;=</SPAN&gt;<SPAN style="COLOR: #000000"&gt; {<BR&gt;  config: {<BR&gt;    clickMessage: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;你好!</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  },<BR&gt;  clickHandler: </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;() {<BR&gt;    alert(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;this</SPAN&gt;<SPAN style="COLOR: #000000"&gt;.config.clickMessage);<BR&gt;  }<BR&gt;};</SPAN&gt;</DIV&gt;
登录后复制

当我们调用如"<font face="新宋体"&gt;app.clickHandler()</font&gt;"时,"<font face="新宋体"&gt;clickHandler</font&gt;"会将"app”作为其上下文。这意味着在这里"<font face="新宋体"&gt;this</font&gt;"关键字将允许其访问"<font face="新宋体"&gt;app</font&gt;"。这在我们简单调用下面函数时没什么问题:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;app.clickHandler(); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; "你好!" 消息触发</SPAN&gt;</DIV&gt;
登录后复制

让我们试试将其作为事件绑定会有什么情况:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;a</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).bind(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;click</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, app.clickHandler);</SPAN&gt;</DIV&gt;
登录后复制

 

当我们点击一个链接的时候什么都不会发生,函数不会工作。这是因为jQuery(其他任何正常的事件模型同样如此)默认会把目标元素a设置成为事件的上下文。即"this”现在其实是指刚刚被点击过的元素a。但我们实际上并不想这样子的。我们期待"this”应该被设置为"app”.在jQuery 1.4中实现这个再简单不过了:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;a</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).bind(<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;click</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;,<BR&gt;  jQuery.proxy(app, </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;clickHandler</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;)<BR&gt;);</SPAN&gt;</DIV&gt;
登录后复制

 

现在无论什么时候一个链接被点击到,"你好"的消息都会被触发了!

proxy函数会返回一个你的函数被"包装过"之后的对象,在该对象中,"this”被设置成任意你指定的对象。这在其他的情景下同样有用,如传递回调函数给其他的jQuery对象或者一些插件的时候。

更多关于<font face="新宋体" color="#0000ff"&gt;jQuery.proxy</font&gt;

7. 在动画中添加停顿

现在你可以再你的动画队列中添加停顿效果了。事实上,任何队列中都可以添加该函数,但最常用的情况应该还是在使用"fx”队列的时候。这允许你在多个动作中间添加暂停而不需要通过回调函数去调用"<font face="新宋体"&gt;setTimeout</font&gt;"。"<font face="新宋体"&gt;.delay()</font&gt;" 函数的第一个参数是你想停顿的毫秒数。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;#foo</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;)<BR&gt;  .slideDown() </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; Slide down</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;  .delay(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;200</SPAN&gt;<SPAN style="COLOR: #000000"&gt;) </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 停顿 200 ms</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;  .fadeIn(); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; Fade in</SPAN&gt;</DIV&gt;
登录后复制

如果你想给其他不是默认的"fx”的队列也添加停顿的话,这时候,你需要将队列名作为第二个参数传递给"<font face="新宋体"&gt;.delay()</font&gt;"。

更多关于jQuery特性0

8. 检查元素是否拥有某对象

通过".has()”方法,jQuery 1.4使得检查一个元素(或者集合)是不是有某对象变得相当简单。从程序角度看,它和jQuery的selector过滤器":has()”是一样的。该方法会返回在当前集合中所有包含有至少一个符合条件的元素数组。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;div</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).has(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;ul</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;);</SPAN&gt;</DIV&gt;
登录后复制

 

上面方法会返回所有包含有UL元素的DIV元素数组。在这种情况下,你可能会更倾向于简单使用选择器的过滤器("<font face="新宋体"&gt;:has()</font&gt;"), 但当你需要通过代码过滤一个数组的时候,该方法仍会很有用。

1.4同样在jQuery命名空间下添加了新的"<font face="新宋体"&gt;contains</font&gt;"函数。这是一个低级的函数,它接受两个DOM节点作为参数。它会返回一个布尔值来表示后面一个元素是否在前面一个元素中存在。如:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery.contains(document.documentElement, document.body);<BR&gt;</SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 返回true - <body&gt;在<html&gt;存在</SPAN&gt;</DIV&gt;
登录后复制

 

更多关于: 更多关于jQuery特性1, 更多关于jQuery特性2

9. 去掉元素的包装

".warp()”已经在jQuery中有段日子了,现在跟它对应的".unwrap()”方法也被加进到1.4中了。这个方法刚好跟warp()方法相反。假设有下面的DOM结构:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;div</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;p</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;Foo</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;p</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;div</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;</DIV&gt;
登录后复制

我们可以用下面的函数去掉段落元素的外层:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;p</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).unwrap();</SPAN&gt;</DIV&gt;
登录后复制

 

最终的DOM结构如下:

1 <code class="xml keyword"&gt;<strong&gt;<font color="#006699"&gt;p</font&gt;</strong&gt;&gt;Foo<code class="xml keyword"&gt;<strong&gt;<font color="#006699"&gt;p</font&gt;</strong&gt;&gt;

注意,这个方法处理比较简单,它会移掉任何元素的父节点。

更多关于jQuery特性6

10. detach() vs remove()

新的".detach()”方法允许你从DOM中移掉元素,这跟".remove()”很像。但与".remove()”有本质不同的是"detach()”不会将jQuery赋给该元素的数据也给注销掉。这包含通过".data()”和其他任意的通过jQuery的事件系统添加的事件等数据。

如果你想从DOM中移除某个元素,但你又可能需要在后面的某个时候把该元素重新添加到DOM中的时候这个函数将会很有用,这时候该元素的事件和其他的数据都会被保留下来。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #0000ff"&gt;var</SPAN&gt;<SPAN style="COLOR: #000000"&gt; foo </SPAN&gt;<SPAN style="COLOR: #000000"&gt;=</SPAN&gt;<SPAN style="COLOR: #000000"&gt; jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;#foo</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;);<BR&gt;</SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 绑定一个重要的事件</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;foo.click(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(){<BR&gt;  alert(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;Foo!</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;);<BR&gt;});<BR&gt;foo.detach(); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 从 DOM中移除事件</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; … 其他操作</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;foo.appendTo(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;body</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 将元素重新加入到 DOM</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;foo.click(); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; 弹出 "Foo!"</SPAN&gt;</DIV&gt;
登录后复制

更多关于jQuery特性7

11. index(…) 加强

jQuery 1.4中我们有两种使用".index()”方法的方式。首先,你可以仅仅将一个元素自身作为参数,这样你可以得到该元素在当前集合中所处的索引位置值。

如果不设置参数的话,现在会返回该元素的兄弟节点。因此,假设有下面的DOM结构:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;ul</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;苹果</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;香蕉</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;葡萄</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;草莓</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;例子</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;<</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;桃子</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;li</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;<BR&gt; </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;</</SPAN&gt;<SPAN style="COLOR: #800000"&gt;ul</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;&gt;</SPAN&gt;</DIV&gt;
登录后复制

当一个节点被点击的时候,你如果想找到在所有的节点集合中被单击的节点的位置,你可以这样写:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery('li').click(function(){<BR&gt;  alert( jQuery(this).index() );<BR&gt;});</SPAN&gt;</DIV&gt;
登录后复制

 

jQuery 1.4还允许你指定一个选择器作为".index()”的第一个参数,这样它会返回该元素在选择器中产生的元素集合中的位置。

最后需要注意的是,如果该元素在集合中存在的话,该函数会返回一个其位置的整数,如果不存在,则返回-1。

更多关于jQuery特性8

12. DOM 操作的方法支持回调函数

大部分对DOM进行操作的函数现在都支持传递函数作为唯一参数了(如果是".css()”和".attr()”的情况的话,它会作为第二参数)。该函数会在集合中的每个元素上都执行一次,从而确定哪些应作为该函数的实际值提供给回调函数调用。

下面给出所有支持该功能的函数列表:

通过回调函数,你可以通过"this”访问数组中的当前元素,还可以通过第一个参数得到它在数组中的位置。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;li</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).html(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(i){<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;return</SPAN&gt;<SPAN style="COLOR: #000000"&gt; </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;该列表中的索引位置: </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt; </SPAN&gt;<SPAN style="COLOR: #000000"&gt;+</SPAN&gt;<SPAN style="COLOR: #000000"&gt; i;<BR&gt;});</SPAN&gt;</DIV&gt;
登录后复制

 

同样,你也可以通过上面的某些方法得到另外一个参数,如果你调用一个setter方法(如".html()”或".attr(‘href')")你就可以直接访问当前的值了。如:

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;a</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).attr(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;href</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(i, currentHref){<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;return</SPAN&gt;<SPAN style="COLOR: #000000"&gt; currentHref </SPAN&gt;<SPAN style="COLOR: #000000"&gt;+</SPAN&gt;<SPAN style="COLOR: #000000"&gt; </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;?foo=bar</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;;<BR&gt;}); </SPAN&gt;</DIV&gt;
登录后复制

 

正如上面看到的,在使用"<font face="新宋体">.css()</font>" 和 "<font face="新宋体">.attr()</font>" 方法时,因为第一个参数需要被用作指定那些你需要设置或改变的属性名,你需要将回调函数作为第二参数。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;li</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).css(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;color</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;, </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(i, currentCssColor){<BR&gt;  </SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;return</SPAN&gt;<SPAN style="COLOR: #000000"&gt; i </SPAN&gt;<SPAN style="COLOR: #000000"&gt;%</SPAN&gt;<SPAN style="COLOR: #000000"&gt; </SPAN&gt;<SPAN style="COLOR: #000000"&gt;2</SPAN&gt;<SPAN style="COLOR: #000000"&gt; </SPAN&gt;<SPAN style="COLOR: #000000"&gt;?</SPAN&gt;<SPAN style="COLOR: #000000"&gt; </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;red</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt; : </SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;blue</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;;<BR&gt;});</SPAN&gt;</DIV&gt;
登录后复制

 

13. 对象类型判断

jQuery 1.4添加了两个新的方法(直接放到了jQuery命名空间下面)以方便我们来判断当前处理的对象的类型。

首先是方法"isEmptyObject”,顾名思义,这个函数会返回一个布尔值来表明传递的对象是不是空对象(缺乏属性--无论是对象自身还是继续的对象)。其次是方法"isPlainObject”,它会返回一个布尔值来表明操作的对象是否是一个简单的js对象(指通过"{ }"或者"new Object”创建的对象)。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery.isEmptyObject({}); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; true</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;jQuery.isEmptyObject({foo:</SPAN&gt;<SPAN style="COLOR: #000000"&gt;1</SPAN&gt;<SPAN style="COLOR: #000000"&gt;}); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; false</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;jQuery.isPlainObject({}); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; true</SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;jQuery.isPlainObject(window); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; false </SPAN&gt;<SPAN style="COLOR: #008000"&gt;<BR&gt;</SPAN&gt;<SPAN style="COLOR: #000000"&gt;jQuery.isPlainObject(jQuery()); </SPAN&gt;<SPAN style="COLOR: #008000"&gt;//</SPAN&gt;<SPAN style="COLOR: #008000"&gt; false</SPAN&gt;</DIV&gt;
登录后复制

 

更多关于: nextUntil5, nextUntil6

14. Closest(…) 增强

jQuery的".closest()”方法现在可以接受一组选择器了。如果开发人员想遍历一个元素的祖先,并在其中查找离其最近的那些符合某些特征的节点的时候,这会很有用。

另外,该函数现在也接受将上下文作为第二参数了。这意味着你现在可以控制你想在DOM中遍历的元素范围了。虽然这两种情况我们一般很少用到,不过它们在jQuery内部被很好的使用。

nextUntil7

15. 新的事件! focusIn 和focusOut

上文已经提到,为了委托"focus”和"blur”事件你需要这些新的事件,它们是"focusin”和"focusout”。这些新的事件将允许你在一个元素或其子元素获取焦点的时候进行相应操作。

<DIV&gt;<!--<br /&gt;<br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)<br /&gt;http://www.CodeHighlighter.com/<br /&gt;<br /&gt;--&gt;<SPAN style="COLOR: #000000"&gt;jQuery(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;form</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;)<BR&gt;  .focusin(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(){<BR&gt;    jQuery(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;this</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).addClass(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;focused</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;);<BR&gt;  });<BR&gt;  .focusout(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;function</SPAN&gt;<SPAN style="COLOR: #000000"&gt;(){<BR&gt;    jQuery(</SPAN&gt;<SPAN style="COLOR: #0000ff"&gt;this</SPAN&gt;<SPAN style="COLOR: #000000"&gt;).removeClass(</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;focused</SPAN&gt;<SPAN style="COLOR: #000000"&gt;'</SPAN&gt;<SPAN style="COLOR: #000000"&gt;);<BR&gt;  });</SPAN&gt;</DIV&gt;
登录后复制

 

同时应该注意的是这两个事件都不是按冒泡顺序传递,它们将按捕获顺序触发。这意味着最外层的(也即祖先)元素会在实际节点事件触发前被触发。

更多关于 nextUntil8 和 nextUntil9 事件。

大家开始玩转jQuery 1.4吧。它是目前jQuery发布的一个最有前瞻性,最多特性支持和表现最好的一个版本!

好了,本文就到这里了。我已经开始准备跟随这个改变了,我相信这些改变同样会让你印象深刻的!

如果你还没准备好,你可以看看 "parentsUntil0", 一个专门为jQuery 1.4为做的在线站点,对了,另外还是为了庆祝jQeury的四岁生日!

最后不要忘了去阅读 parentsUntil1 !

作者:parentsUntil2
出处:parentsUntil3
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号