首先现在你可以从这里下载最新的1.4的版本了: http://code.jquery.com/jquery-1.4.js
1.4之前的版本中jQuery就通过"attr”方法支持了添加属性到元素集合中,"attr”方法不仅可以传递属性名和值,也可以传递包含有多个属性的对象集合。在1.4中,除了可以创建新的对象,现在它更能将属性对象作为参数传递给jQuery函数或对象自身。如你可以创建一个含有多个属性的链接元素,通过1.4代码如下:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000"><BR> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"><a/></SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, { <BR> id: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">gid</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, <BR> href: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">http://www.google.com</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, <BR> title: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">google非和谐版</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, <BR> rel: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">external</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, <BR> text: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">进入Google!</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"> <BR> }); </SPAN></DIV>
你可能会注意到"text”属性并且猜测它是干吗的,因为a标签是没有"text”属性的。呵呵,当你传递某些属性时,jquery 1.4同样会检查并使用它自己的方法。因此上面的"text”属性可以让jQuery调用它的".text()”方法并将"进入Google!“作为它的唯一参数。
这里给出一个更好的例子:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000"> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"><div/></SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, { <BR> id: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">foo</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, <BR> css: { <BR> fontWeight: </SPAN><SPAN style="COLOR: #000000">700</SPAN><SPAN style="COLOR: #000000">, <BR> color: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">green</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"> <BR> }, <BR> click: </SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(){ <BR> alert(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">Foo被点击过!</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">); <BR> } <BR> }); </SPAN></DIV>
"id”属性是作为一个普通属性被添加的。但"css”和"click”属性则对应特定的jQuery方法。上面的代码在1.4之前一种写法如下:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000"> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"><div/></SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">) <BR> .attr(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">id</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">foo</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">) <BR> .css({ <BR> fontWeight: </SPAN><SPAN style="COLOR: #000000">700</SPAN><SPAN style="COLOR: #000000">, <BR> color: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">green</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"> <BR> }) <BR> .click(</SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(){ <BR> alert(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">Foo被点击过!</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">); <BR> }); </SPAN></DIV>
1.4新增了三个对DOM操作的方法,他们分别是"<font face="新宋体">nextUntil</font>
", "<font face="新宋体">prevUntil</font>
" 和 "<font face="新宋体">parentsUntil</font>
"。 这些方法会按照某个顺序去遍历DOM对象直到满足指定的筛选条件。假设我们有一个水果列表:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">ul</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">苹果</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">香蕉</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">葡萄</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">草莓</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">例子</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">桃子</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">ul</SPAN><SPAN style="COLOR: #0000ff">></SPAN></DIV>
如果你想选择所有在"Apple”和"Strawberry”之间的元素。代码可以这样写:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000"> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">ul li:contains(苹果)</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).nextUntil(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">:contains(梨子)</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">); <BR> </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 得到 香蕉,葡萄,草莓</SPAN></DIV>
更多关于: prevUntil, nextUntil, parentsUntil
与通过jquery链绑定多个方法到一起相比,现在你可以通过一次调用来绑定所有这些事件,如:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="max-width:90%">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">#foo).bind({ <BR> click: function() { <BR> // 具体代码 <BR> }, <BR> mouseover: function() { <BR> // 具体代码<BR> }, <BR> mouseout: function() { <BR> // 具体代码 <BR> } <BR> }) </SPAN></DIV>
你也可以通过 "<font face="新宋体">.one()</font>
"方法操作。
与给一个动画仅仅定义一个缓动效果相比,现在你可以给你想要添加动画效果的属性定义不同的缓动函数了。jQuery包含两个缓动函数,旋转(默认)和线性移动。如果你需要其他的效果的话,你需要去单独下载它们 !
要给每个属性指定一个缓动函数的话,你可以简单的通过属性数组来达到目的,在数组中,第一个值是你想要实现的效果值,第二个参数则是使用的缓动函数,如:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000"> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">#foo</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).animate({ <BR> left: </SPAN><SPAN style="COLOR: #000000">500</SPAN><SPAN style="COLOR: #000000">, <BR> top: [</SPAN><SPAN style="COLOR: #000000">500</SPAN><SPAN style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">easeOutBounce</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">] <BR> }, </SPAN><SPAN style="COLOR: #000000">2000</SPAN><SPAN style="COLOR: #000000">); </SPAN></DIV>
你也可以以键值对的形式在那些可选设置项中单独指定属性缓动函数:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000"> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">#foo</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).animate({ <BR> left: </SPAN><SPAN style="COLOR: #000000">500</SPAN><SPAN style="COLOR: #000000">, <BR> top: </SPAN><SPAN style="COLOR: #000000">500</SPAN><SPAN style="COLOR: #000000"> <BR> }, { <BR> duration: </SPAN><SPAN style="COLOR: #000000">2000</SPAN><SPAN style="COLOR: #000000">, <BR> specialEasing: { <BR> top: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">easeOutBounce</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"> <BR> } <BR> }); </SPAN></DIV>
原文作者注 - 小小谦虚一下,这个新特性是作者James Padolsey的主意!
1.4添加了对"submit”,"change”,"focus”,和"blur”事件的委托支持。在jQuery中,我们可以使用".live()”方法去委托事件。这在你需要注册事件到大量元素中或新的元素需要不断的被添加(使用".live()”会比不断的重新绑定事件更高效)将会很有用。
但是,注意!如果你需要委托"focus”和"blur”事件的时候,你必须使用"focusin”和"focusout”事件名称!
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">input</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).live(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">focusin</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(){<BR> </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">具体操作</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">});</SPAN></DIV>
jQuery 1.4提供了一个新的叫"proxy”的函数,它在jQuery命名空间下。该函数需要两个参数,不管是代码域还是方法名,或者是函数和其所处的代码域。t"this”关键字在JavaScrip中是相当难掌握的。有时候你可能期待它指的是你之前创建的某个对象,而不是一个你可能没想到的元素。
举例说明,假设我们有一个"app”对象,它有两个属性,一个是"clickHandler”方法, 另外一个则是一个config对象。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #0000ff">var</SPAN><SPAN style="COLOR: #000000"> app </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> {<BR> config: {<BR> clickMessage: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">你好!</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"><BR> },<BR> clickHandler: </SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">() {<BR> alert(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.config.clickMessage);<BR> }<BR>};</SPAN></DIV>
当我们调用如"<font face="新宋体">app.clickHandler()</font>
"时,"<font face="新宋体">clickHandler</font>
"会将"app”作为其上下文。这意味着在这里"<font face="新宋体">this</font>
"关键字将允许其访问"<font face="新宋体">app</font>
"。这在我们简单调用下面函数时没什么问题:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">app.clickHandler(); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> "你好!" 消息触发</SPAN></DIV>
让我们试试将其作为事件绑定会有什么情况:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">a</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).bind(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">click</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, app.clickHandler);</SPAN></DIV>
当我们点击一个链接的时候什么都不会发生,函数不会工作。这是因为jQuery(其他任何正常的事件模型同样如此)默认会把目标元素a设置成为事件的上下文。即"this”现在其实是指刚刚被点击过的元素a。但我们实际上并不想这样子的。我们期待"this”应该被设置为"app”.在jQuery 1.4中实现这个再简单不过了:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">a</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).bind(<BR> </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">click</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">,<BR> jQuery.proxy(app, </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">clickHandler</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)<BR>);</SPAN></DIV>
现在无论什么时候一个链接被点击到,"你好"的消息都会被触发了!
proxy函数会返回一个你的函数被"包装过"之后的对象,在该对象中,"this”被设置成任意你指定的对象。这在其他的情景下同样有用,如传递回调函数给其他的jQuery对象或者一些插件的时候。
更多关于<font face="新宋体" color="#0000ff">jQuery.proxy</font>
现在你可以再你的动画队列中添加停顿效果了。事实上,任何队列中都可以添加该函数,但最常用的情况应该还是在使用"fx”队列的时候。这允许你在多个动作中间添加暂停而不需要通过回调函数去调用"<font face="新宋体">setTimeout</font>
"。"<font face="新宋体">.delay()</font>
" 函数的第一个参数是你想停顿的毫秒数。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">#foo</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)<BR> .slideDown() </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> Slide down</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"> .delay(</SPAN><SPAN style="COLOR: #000000">200</SPAN><SPAN style="COLOR: #000000">) </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 停顿 200 ms</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"> .fadeIn(); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> Fade in</SPAN></DIV>
如果你想给其他不是默认的"fx”的队列也添加停顿的话,这时候,你需要将队列名作为第二个参数传递给"<font face="新宋体">.delay()</font>
"。
通过".has()”方法,jQuery 1.4使得检查一个元素(或者集合)是不是有某对象变得相当简单。从程序角度看,它和jQuery的selector过滤器":has()”是一样的。该方法会返回在当前集合中所有包含有至少一个符合条件的元素数组。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">div</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).has(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">ul</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">);</SPAN></DIV>
上面方法会返回所有包含有UL元素的DIV元素数组。在这种情况下,你可能会更倾向于简单使用选择器的过滤器("<font face="新宋体">:has()</font>
"), 但当你需要通过代码过滤一个数组的时候,该方法仍会很有用。
1.4同样在jQuery命名空间下添加了新的"<font face="新宋体">contains</font>
"函数。这是一个低级的函数,它接受两个DOM节点作为参数。它会返回一个布尔值来表示后面一个元素是否在前面一个元素中存在。如:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery.contains(document.documentElement, document.body);<BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 返回true - <body>在<html>存在</SPAN></DIV>
更多关于: 更多关于jQuery特性1, 更多关于jQuery特性2
".warp()”已经在jQuery中有段日子了,现在跟它对应的".unwrap()”方法也被加进到1.4中了。这个方法刚好跟warp()方法相反。假设有下面的DOM结构:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">div</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">p</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">Foo</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">p</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">div</SPAN><SPAN style="COLOR: #0000ff">></SPAN></DIV>
我们可以用下面的函数去掉段落元素的外层:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">p</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).unwrap();</SPAN></DIV>
最终的DOM结构如下:
1 |
<code class="xml keyword"><strong><font color="#006699">p</font></strong> >Foo <code class="xml keyword"><strong><font color="#006699">p</font></strong>> |
注意,这个方法处理比较简单,它会移掉任何元素的父节点。
新的".detach()”方法允许你从DOM中移掉元素,这跟".remove()”很像。但与".remove()”有本质不同的是"detach()”不会将jQuery赋给该元素的数据也给注销掉。这包含通过".data()”和其他任意的通过jQuery的事件系统添加的事件等数据。
如果你想从DOM中移除某个元素,但你又可能需要在后面的某个时候把该元素重新添加到DOM中的时候这个函数将会很有用,这时候该元素的事件和其他的数据都会被保留下来。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #0000ff">var</SPAN><SPAN style="COLOR: #000000"> foo </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">#foo</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 绑定一个重要的事件</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">foo.click(</SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(){<BR> alert(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">Foo!</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">);<BR>});<BR>foo.detach(); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 从 DOM中移除事件</SPAN><SPAN style="COLOR: #008000"><BR>//</SPAN><SPAN style="COLOR: #008000"> … 其他操作</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">foo.appendTo(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">body</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 将元素重新加入到 DOM</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">foo.click(); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 弹出 "Foo!"</SPAN></DIV>
jQuery 1.4中我们有两种使用".index()”方法的方式。首先,你可以仅仅将一个元素自身作为参数,这样你可以得到该元素在当前集合中所处的索引位置值。
如果不设置参数的话,现在会返回该元素的兄弟节点。因此,假设有下面的DOM结构:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">ul</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">苹果</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">香蕉</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">葡萄</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">草莓</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">例子</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">桃子</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">li</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR> </SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">ul</SPAN><SPAN style="COLOR: #0000ff">></SPAN></DIV>
当一个节点被点击的时候,你如果想找到在所有的节点集合中被单击的节点的位置,你可以这样写:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery('li').click(function(){<BR> alert( jQuery(this).index() );<BR>});</SPAN></DIV>
jQuery 1.4还允许你指定一个选择器作为".index()”的第一个参数,这样它会返回该元素在选择器中产生的元素集合中的位置。
最后需要注意的是,如果该元素在集合中存在的话,该函数会返回一个其位置的整数,如果不存在,则返回-1。
大部分对DOM进行操作的函数现在都支持传递函数作为唯一参数了(如果是".css()”和".attr()”的情况的话,它会作为第二参数)。该函数会在集合中的每个元素上都执行一次,从而确定哪些应作为该函数的实际值提供给回调函数调用。
下面给出所有支持该功能的函数列表:
通过回调函数,你可以通过"this”访问数组中的当前元素,还可以通过第一个参数得到它在数组中的位置。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">li</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).html(</SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(i){<BR> </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">该列表中的索引位置: </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> i;<BR>});</SPAN></DIV>
同样,你也可以通过上面的某些方法得到另外一个参数,如果你调用一个setter方法(如".html()”或".attr(‘href')")你就可以直接访问当前的值了。如:
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">a</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).attr(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">href</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(i, currentHref){<BR> </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> currentHref </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">?foo=bar</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">;<BR>}); </SPAN></DIV>
正如上面看到的,在使用"<font face="新宋体">.css()</font>
" 和 "<font face="新宋体">.attr()</font>
" 方法时,因为第一个参数需要被用作指定那些你需要设置或改变的属性名,你需要将回调函数作为第二参数。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">li</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">).css(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">color</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(i, currentCssColor){<BR> </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> i </SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">red</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000"> : </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">blue</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">;<BR>});</SPAN></DIV>
jQuery 1.4添加了两个新的方法(直接放到了jQuery命名空间下面)以方便我们来判断当前处理的对象的类型。
首先是方法"isEmptyObject”,顾名思义,这个函数会返回一个布尔值来表明传递的对象是不是空对象(缺乏属性--无论是对象自身还是继续的对象)。其次是方法"isPlainObject”,它会返回一个布尔值来表明操作的对象是否是一个简单的js对象(指通过"{ }"或者"new Object”创建的对象)。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery.isEmptyObject({}); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> true</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">jQuery.isEmptyObject({foo:</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">}); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> false</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">jQuery.isPlainObject({}); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> true</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">jQuery.isPlainObject(window); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> false </SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">jQuery.isPlainObject(jQuery()); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> false</SPAN></DIV>
jQuery的".closest()”方法现在可以接受一组选择器了。如果开发人员想遍历一个元素的祖先,并在其中查找离其最近的那些符合某些特征的节点的时候,这会很有用。
另外,该函数现在也接受将上下文作为第二参数了。这意味着你现在可以控制你想在DOM中遍历的元素范围了。虽然这两种情况我们一般很少用到,不过它们在jQuery内部被很好的使用。
上文已经提到,为了委托"focus”和"blur”事件你需要这些新的事件,它们是"focusin”和"focusout”。这些新的事件将允许你在一个元素或其子元素获取焦点的时候进行相应操作。
<DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN style="COLOR: #000000">jQuery(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">form</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)<BR> .focusin(</SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(){<BR> jQuery(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">).addClass(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">focused</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">);<BR> });<BR> .focusout(</SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">(){<BR> jQuery(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">).removeClass(</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">focused</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">);<BR> });</SPAN></DIV>
同时应该注意的是这两个事件都不是按冒泡顺序传递,它们将按捕获顺序触发。这意味着最外层的(也即祖先)元素会在实际节点事件触发前被触发。
更多关于 nextUntil8 和 nextUntil9 事件。
大家开始玩转jQuery 1.4吧。它是目前jQuery发布的一个最有前瞻性,最多特性支持和表现最好的一个版本!
好了,本文就到这里了。我已经开始准备跟随这个改变了,我相信这些改变同样会让你印象深刻的!
如果你还没准备好,你可以看看 "parentsUntil0", 一个专门为jQuery 1.4为做的在线站点,对了,另外还是为了庆祝jQeury的四岁生日!
最后不要忘了去阅读 parentsUntil1 !
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号