<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pivot Code &#187; JQuery</title>
	<atom:link href="http://www.pivotcode.com/category/web-development/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pivotcode.com</link>
	<description>欢迎来我的小思想绽放的地方……</description>
	<lastBuildDate>Sun, 30 Oct 2011 13:37:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>jQuery 1.4 的新特性</title>
		<link>http://www.pivotcode.com/jquery-1-4-new-features/</link>
		<comments>http://www.pivotcode.com/jquery-1-4-new-features/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 05:16:43 +0000</pubDate>
		<dc:creator>Vidar Liu</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQurey]]></category>
		<category><![CDATA[新特性]]></category>
		<category><![CDATA[版本]]></category>

		<guid isPermaLink="false">http://mingsenliu.com/?p=193</guid>
		<description><![CDATA[jQuery 1.4 最近发布了。 超乎大家的预期，这次并非简单的修修补补，1.4 包含了很多新特性、功能增强和性能提升！本文即向您介绍这些可能对你十分有用的新特性和优化增强。 你可以立刻下载jQuery 1.4试用： http://code.jquery.com/jquery-1.4.js 1. 传参给 jQuery(…) 之前，jQuery可以通过 attr 方法设置元素的属性，既可传属性的名和值，也可以是包含几组特定 属性名值对 的 对象。在 jQuery 1.4 中，你可以把一个参数对象作为第二个参数传给 jQuery 函数本身，同时创建HTML元素。 比方说你想要创建一个带有几个属性的锚记元素（&#60;a&#62;&#60;/a&#62;）。在1.4中，一切如此简单： jQuery('&#60;a/&#62;', { id: 'foo', href: 'http://google.com', title: 'Become a Googler', rel: 'external', text: 'Go to Google!' }); 你大概也能猜到，这个锚记元素没有的 text 属性会调用 jQuery 的私有方法”.text()” ，把元素里的文字设置为“Go to Google!” 针对这一用法，下面是更有用的实例： jQuery('&#60;div/&#62;', { id: 'foo', css: { [...]]]></description>
			<content:encoded><![CDATA[<div id="article_content">
<p>jQuery 1.4 <a href="http://jquery14.com/day-01/jquery-14" target="_blank">最近发布了</a>。 超乎大家的预期，这次并非简单的修修补补，1.4 包含了<strong>很多</strong>新特性、功能增强和性能提升！本文即向您介绍这些可能对你十分有用的新特性和优化增强。</p>
<p>你可以立刻下载jQuery 1.4试用： <a href="http://code.jquery.com/jquery-1.4.js" target="_blank">http://code.jquery.com/jquery-1.4.js</a></p>
<h3>1. 传参给 jQuery(…)</h3>
<p>之前，jQuery可以通过 <a href="http://api.jquery.com/attr/" target="_blank"><code>attr</code></a> 方法设置元素的属性，既可传属性的名和值，也可以是包含几组特定 属性名值对 的 对象。在 jQuery 1.4 中，你可以把一个参数对象作为第二个参数传给 jQuery 函数本身，同时创建HTML元素。</p>
<p>比方说你想要创建一个带有几个属性的锚记元素（&lt;a&gt;&lt;/a&gt;）。在1.4中，一切如此简单：</p>
<pre>jQuery('&lt;a/&gt;', {
 id: 'foo',
 href: 'http://google.com',
 title: 'Become a Googler',
 rel: 'external',
 text: 'Go to Google!'
 });</pre>
<p>你大概也能猜到，这个锚记元素没有的 text 属性会调用 jQuery 的私有方法”<code>.text()</code>” ，把元素里的文字设置为“Go to Google!”</p>
<p>针对这一用法，下面是更有用的实例：</p>
<pre>jQuery('&lt;div/&gt;', {
 id: 'foo',
 css: {
 fontWeight: 700,
 color: 'green'
 },
 click: function(){
 alert('Foo has been clicked!');
 }
 });</pre>
<p>id 为一般属性，被直接加上了，而 css 和 click 则激发了相应的 jQuery 方法。在1.4以前，上面的代码需写成这个样子：</p>
<pre>jQuery('&lt;div/&gt;')
 .attr('id', 'foo')
 .css({
 fontWeight: 700,
 color: 'green'
 })
 .click(function(){
 alert('Foo has been clicked!');
 });</pre>
<p><a href="http://api.jquery.com/jQuery/" target="_blank">详细了解 jQuery(…) </a></p>
<h3>2. 直到遇见你&#8230;</h3>
<p>1.4的DOM遍历工具包里又增加了3个新方法：<code>nextUntil，</code> <code>prevUntil</code> 和 <code>parentsUntil</code>。这些方法会按照特定的方向遍历DOM，直到遇到满足指定选择符的元素为止。举例来说，现在我们有一张水果名的清单：</p>
<pre>&lt;ul&gt;
 &lt;li&gt;Apple&lt;/li&gt;

 &lt;li&gt;Banana&lt;/li&gt;
 &lt;li&gt;Grape&lt;/li&gt;

 &lt;li&gt;Strawberry&lt;/li&gt;
 &lt;li&gt;Pear&lt;/li&gt;

 &lt;li&gt;Peach&lt;/li&gt;
 &lt;/ul&gt;</pre>
<p>我们想挑选出所有在 Apple 后，Pear 前的所有条目。代码十分简单：</p>
<pre>jQuery('ul li:contains(Apple)').nextUntil(':contains(Pear)');
 // 选出的是 Banana, Grape, Strawberry</pre>
<p>详细了解： <a href="http://api.jquery.com/prevUntil/">prevUntil</a>, <a href="http://api.jquery.com/nextUntil/">nextUntil</a>, <a href="http://api.jquery.com/parentsUntil/">parentsUntil</a></p>
<h3>3. 绑定多个事件处理器</h3>
<p>不再需要把各个事件绑定方法“链”在一起，现在你可以把它们捆成一堆，如下：</p>
<pre>jQuery('#foo).bind({
 click: function() {
 // do something
 },
 mouseover: function() {
 // do something
 },
 mouseout: function() {
 // do something
 }
 })</pre>
<p>这一用法也适用于 “<code>.one()</code>“.</p>
<p><a href="http://api.jquery.com/bind/">详细了解 .bind(…)</a></p>
<h3>4. 依属性指定缓动效果</h3>
<p>以前只能为一个动画指定一种缓动效果（easing，即动画过程中的速度变化规律。jQuery 原生支持两种缓动效果，swing（默认）和linear 。要使用其他效果，你需要自己<a href="http://gsgd.co.uk/sandbox/jquery/easing/" target="_blank">另行下载</a>。），现在你可以为动画的各个属性参数指定不同的缓动效果：</p>
<pre>jQuery('#foo').animate({
 left: 500,
 top: [500, 'easeOutBounce']
 }, 2000);</pre>
<p><a href="http://james.padolsey.com/demos/jquery/easing/easing-jq14.html">点此查看实际效果</a></p>
<p>你也可以在一个可选的动画选项对象中为 secialEasing 设置一系列名值对来完全上面的工作：</p>
<pre>jQuery('#foo').animate({
 left: 500,
 top: 500
 }, {
 duration: 2000,
 specialEasing: {
 top: 'easeOutBounce'
 }
 });</pre>
<p><strong>编辑注：我们的作者 James Padolsey 谦虚了，这一功能点是他想出来的哦！</strong></p>
<p><a href="http://api.jquery.com/animate/#per-property-easing" target="_blank">详细了解有关 per-property-easing 的内容</a></p>
<h3>5. 更新的 Live 事件！</h3>
<p>jQuery 1.4 添加了对指派 <strong>submit </strong>， <strong>change </strong>， <strong>focus</strong> 和 <strong>blur</strong> 事件的支持。在jQuery中，我们利用”<code>.live()</code>” 方法指派事件。当你想要为多个元素注册事件处理器时，这会非常有用。而且就算满足选择符的元素是新出现的，这些事件也会继续有效（使用 <code>.live() </code>比不断重复绑定要省力省心得多）。</p>
<p><strong>不过，当心了！</strong>注册 focus 和 blur 事件时你需要用 <strong>focusin</strong> 和 <strong>focusout</strong> 作为事件名。</p>
<pre>jQuery('input').live('focusin', function(){
 // do something with this
 });</pre>
<h3>6. 控制函数上下文</h3>
<p>jQuery 1.4 提供了一个全新的 <code>proxy</code> 函数，位于 jQuery 命名空间下。这一函数接受两个参数，一个是“作用域”（scope）或者一个方法名，另一个是某函数或者目标作用域（the intended scope）。</p>
<p>众所周知， JavaScript的 this 关键字是一个很难把握的东西。有时候你并不想它代表一个元素，而想让它代表你前面创建的某个对象。</p>
<p>例如，在这里我们创建了一个 <code>app</code> 对象，它拥有两个属性，一个是 <code>clickHandler</code> 方法，一个是负责参数配置的对象。</p>
<pre>var app = {
 config: {
 clickMessage: 'Hi!'
 },
 clickHandler: function() {
 alert(this.config.clickMessage);
 }
 };</pre>
<p>这个 <code>clickHandler</code> 方法，当像 <code>app.clickHandler()</code> 这样调用时，<code>app</code> 就是其上下文，也就是说 <code>this</code> 关键字指向的是 <code>app</code> 。如果只需简单地调用，这样的写法没什么问题：</p>
<pre>app.clickHandler(); // "Hi!" is alerted</pre>
<p>不过如果把它当作一个事件处理器：</p>
<pre>jQuery('a').bind('click', app.clickHandler);</pre>
<p>当我们点击这个锚记时，并没有达到预想的效果（没东西 alert 出来）。这是因为 jQuery （以及大部分理智的事件模型），默认地，都会把处理器的上下文指定为目标元素本身。也就是说，<code>this 所代表正是被点击的这个链接。而我们想的是，<code>this</code> 应该继续代表 <code>app</code> 。在jQuery 1.4中，实现这一目的十分简单：</code></p>
<pre>jQuery('a').bind(
 'click',
 jQuery.proxy(app, 'clickHandler')
 );</pre>
<p>现在点击所有锚记都会弹出“Hi!”了。</p>
<p>代理函数把你的函数包裹一圈，同时把函数内的 <code>this</code> 设定为你想要东西。在其他上下文应用场景，如把回调函数传给其他 jQuery 方法或插件，代理函数也能派上用场。</p>
<p><a href="http://api.jquery.com/jQuery.proxy">了解更多 <code>jQuery.proxy</code></a></p>
<h3>7.  动画队列延迟</h3>
<p>现在，可以给动画队列加一个延迟了。虽然这个功能可以在任何队列里实现，但最常用的可能还是延迟“fx 队列”（”fx” queue，jQuery默认的动画队列）。它能让你在两个动画行为之间暂停一下，而不用牵扯到回调函数和 <code>setTimeout</code> 之类的东西。<code>.delay()</code> 的第一个参数即你需要设定的延迟毫秒数：</p>
<pre>jQuery('#foo')
 .slideDown() // Slide down
 .delay(200) // Do nothing for 200 ms
 .fadeIn(); // Fade in</pre>
<p>如果你想延迟一个除 fx 以外的队列，把队列名作为第二个参数传给 <code>.delay() 就可以了</code>。</p>
<p><a href="http://api.jquery.com/delay">详细了解 <code>.delay(…)</code></a></p>
<h3>8. 检测元素是否含有特定内容</h3>
<p>jQuery 1.4 让检测一个元素（或集合）是否含有（<code>.has()</code>）某项东西更为容易。其背后的机理和选择过滤器 <code>:has()</code>是一样的。这个方法会从当前集合中选出满足任意指定条件之一的元素。</p>
<pre>jQuery('div').has('ul');</pre>
<p>这条语句在所有DIV元素中挑出那些包含UL元素的。这种情况可能用选择过滤器 <code>:has()</code> 就好了，但当你要更程式化地过滤选择集时 <code>.has() </code>方法就十分有用了。</p>
<p>jQuery 1.4 还在 jQuery 命名空间下新增了一个 <code>contains</code> 函数。这是一个比较底层的函数，接受两个 DOM 节点为参数。返回一个布尔值，指示第二个元素是否包含在第一个元素中。例如：</p>
<pre>jQuery.contains(document.documentElement, document.body);
 // 返回true - &lt;body&gt; 确实包含在 &lt;html&gt; 中</pre>
<p>消息了解： <a href="http://api.jquery.com/has/"><code>.has(…)</code></a>, <a href="http://api.jquery.com/jQuery.contains/"><code>jQuery.contains(…)</code></a></p>
<h3>9. 给元素剥皮！</h3>
<p>很早以前，jQuery 就可以用 <code>.wrap()</code> 给元素裹一层皮。现在， jQuery 1.4 添加了 <code>.unwrap() </code>方法，用以剥皮。看下面的DOM结构：</p>
<pre>&lt;div&gt;
 &lt;p&gt;Foo&lt;/p&gt;
 &lt;/div&gt;</pre>
<p>来把 p 元素外面的一层皮（div）剥掉：</p>
<pre>jQuery('p').unwrap();</pre>
<p>DOM 变成了：</p>
<pre>&lt;p&gt;Foo&lt;/p&gt;</pre>
<p>总而言之，这个方法可以把任意元素的父元素移除。.</p>
<p><a href="http://api.jquery.com/unwrap/">详细了解 <code>.unwrap(…)</code></a></p>
<h3>10. 移除元素，而不删除数据</h3>
<p>以前有一个<code>.remove()</code>方法，是把元素剥离后抛弃。全新的 <code><strong>.detach() </strong></code>方法可以让你把一个元素从DOM中剥离，而不丢失数据。囊括该元素的 jQuery 对象会在操作完成后还保留这个元素。数据可以通过 <code>.data()</code> 或者 jQuery 事件系统中的任意事件处理器传入 jQuery 对象。</p>
<p>当你需要把某个元素从DOM中移除，然后在将来某个场景中重新引入时，这个函数会很有用。元素的事件句柄和其他所有数据都会被保留。</p>
<pre>var foo = jQuery('#foo');

 // 绑定一个重要的事件处理器
 foo.click(function(){
 alert('Foo!');
 });

 foo.detach(); // 从DOM中移除
 // … do stuff

 foo.appendTo('body'); // 重新加入到DOM

 foo.click(); // 弹出alert信息： "Foo!"</pre>
<p><a href="http://api.jquery.com/detach">详细了解 <code>.detach(…)</code></a></p>
<h3>11. index(…) 的功能增强</h3>
<p>jQuery 1.4 为您使用 <code>.index()</code> 提供了两种新方法。以前，你必须把元素作为参数传给它，然后获得一个返回的数值，代表在当前集合中这个元素的索引。现在，如果不传参数过去，返回的值就代表某元素在其同辈中排行老几。比方说下面的DOM：</p>
<pre>&lt;ul&gt;
 &lt;li&gt;Apple&lt;/li&gt;

 &lt;li&gt;Banana&lt;/li&gt;
 &lt;li&gt;Grape&lt;/li&gt;

 &lt;li&gt;Strawberry&lt;/li&gt;
 &lt;li&gt;Pear&lt;/li&gt;

 &lt;li&gt;Peach&lt;/li&gt;
 &lt;/ul&gt;</pre>
<p>你想要晓得点击一个条目后它是列表中的第几个，实现的代码十分简单：</p>
<pre>jQuery('li').click(function(){
 alert( jQuery(this).index() );
 });</pre>
<p>jQuery 1.4 也允许你将选择符作为 <code>.index()</code> 的第一个参数。这样做会返回当前元素在你指定的选择符所匹配出的元素集合中的索引值。</p>
<p>还得提醒一点，这一方法返回的是数值，如果文档中无法找到指定条件的元素，会返回数值 -1 。</p>
<p><a href="http://api.jquery.com/index">详细了解 <code>.index(…)</code></a></p>
<h3>12. DOM 操纵可接收回调函数</h3>
<p>现在，大部分的DOM操纵方法都支持了将 函数 作为单一参数传入（<code>.css()</code> 和 <code>.attr() </code>传入为第二个参数）。这个函数会为选择集中的每一个元素都跑一遍，从而为相应操纵方法提供更“有个性”的值。</p>
<p>下列方法支持这一功能：</p>
<ul>
<li><a href="http://api.jquery.com/after">after</a></li>
<li><a href="http://api.jquery.com/before">before</a></li>
<li><a href="http://api.jquery.com/append">append</a></li>
<li><a href="http://api.jquery.com/prepend">prepend</a></li>
<li><a href="http://api.jquery.com/addClass">addClass</a></li>
<li><a href="http://api.jquery.com/toggleClass">toggleClass</a></li>
<li><a href="http://api.jquery.com/removeClass">removeClass</a></li>
<li><a href="http://api.jquery.com/wrap">wrap</a></li>
<li><a href="http://api.jquery.com/wrapAll">wrapAll</a></li>
<li><a href="http://api.jquery.com/wrapInner">wrapInner</a></li>
<li><a href="http://api.jquery.com/val">val</a></li>
<li><a href="http://api.jquery.com/text">text</a></li>
<li><a href="http://api.jquery.com/replaceWith">replaceWith</a></li>
<li><a href="http://api.jquery.com/css">css</a></li>
<li><a href="http://api.jquery.com/attr">attr</a></li>
<li><a href="http://api.jquery.com/html">html</a></li>
</ul>
<p>有了回调函数，你就能在选择集中利用 <code>this</code> 关键字来访问当前元素，用回调函数的第一个参数来访问其索引值。</p>
<pre>jQuery('li').html(function(i){
 return 'Index of this list item: ' + i;
 });</pre>
<p>还有还有，上面的某些方法还提供第二个参数供你利用。如果你调用的是一个设定型方法（如<code>.html()</code> 和 <code>.attr('href...)</code> ），你还能取得当前值。例如：</p>
<pre>jQuery('a').attr('href', function(i, currentHref){
 return currentHref + '?foo=bar';
 });</pre>
<p>如您所见，对于<code>.css()</code> 和 <code>.attr() </code>方法来说，之所以要把函数作为第二个参数传递，是因为第一个参数要用来指定我们需要修改的是哪一个属性：</p>
<pre>jQuery('li').css('color', function(i, currentCssColor){
 return i % 2 ? 'red' : 'blue';
 });</pre>
<h3>13. 判定对象类型</h3>
<p>jQuery 1.4 新增了两个全新的辅助函数（都直接位于 jQuery 命名空间下），可以帮助你判别你正在操纵的是何种对象。</p>
<p>第一个函数是 <code>isEmptyObject</code>, ，它返回一个布尔值，判定对象是否为空（）。第二个是 <code>isPlainObject</code> ，它返回的布尔值代表传递过去的参数是否为JavaScript的简单对象（plain object），也就是用 <code>{} 或 </code><code>new Object()</code>创建的对象。</p>
<pre>jQuery.isEmptyObject({}); // true
 jQuery.isEmptyObject({foo:1}); // false

 jQuery.isPlainObject({}); // true
 jQuery.isPlainObject(window); // false
 jQuery.isPlainObject(jQuery()); // false</pre>
<p>了解更多： <a href="http://api.jquery.com/jQuery.isPlainObject"><code>isPlainObject(…)</code></a>, <a href="http://api.jquery.com/jQuery.isEmptyObject"><code>isEmptyObject(…)</code></a></p>
<h3>14. Closest(…) 的功能增强</h3>
<p>jQuery的<code>.closest()</code> 方法现在可以接受一组选择符作为参数。当你需要遍历某一元素的所有上级，找到<strong>一个以上</strong>符合特定特征的最近元素时，此功能就能派上用场。</p>
<p>而且，现在它还能接受上下文环境作为第二个参数，也就是说你可以控制DOM遍历的深度或者说远度。虽然说大家可能很少会用到这两个新特性，但一旦用上效果是惊人的！</p>
<p><a href="http://api.jquery.com/closest">了解更多 <code>.closest(…)</code></a></p>
<h3>15. 新事件！ focusIn 与 focusOut</h3>
<p>如前所述，现在部署 focus 和 blur 事件时，你需要使用 focusin 和 focusout 这两个新事件。这两个事件帮助你在特定元素或者该元素的子元素 获取/失去 焦点时采取行动。</p>
<pre>jQuery('form')
 .focusin(function(){
 jQuery(this).addClass('focused');
 });
 .focusout(function(){
 jQuery(this).removeClass('focused');
 });</pre>
<p>值得说明的是，这两个事件不会传播开来（即所谓的“冒泡”），它们会被捕获住。也就是说，外部元素（父辈）会在作孽的“目标”元素（causal “target” element）之前被触发。</p>
<p>了解更多关于 <a href="http://api.jquery.com/focusin"><code>focusIn</code></a> 和 <a href="http://api.jquery.com/focusout"><code>focusOut</code></a> 事件的内容。</p>
<h3>爱上 jQuery 1.4 吧！史上考虑最周到，功能最丰富，性能最好的 jQuery ！</h3>
<p>就是这样&#8230; 我已经介绍完我觉得对你最有影响的一些新特性了。</p>
<p>如果你还不知道，那就赶快去看看 “<strong><a href="http://jquery14.com/">14 days of jQuery</a></strong>”活动，为庆祝 jQuery 1.4 的发布和 jQuery 四岁生日而举办的超赞的线上活动。秀出你用 jQuery 写的得意之作，赢得 MediaTemple 一年的主机使用权！</p>
<p>另外，别放了看看全新的 <a href="http://api.jquery.com/"><strong>API 文档</strong></a> ！</p>
<blockquote><p>原文作者：<a href="http://james.padolsey.com/">James Padolsey</a></p>
<p>原文地址：<a title="http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/" href="http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/">http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/</a></p>
<p>转载需保留出处。</p></blockquote>
</div>
<h2  class="related_post_title">相关日志</h2><ul class="related_post"><li><a href="http://www.pivotcode.com/sample-javasctipt-regular-expressions/" title="基础Javasctipt正则表达式">基础Javasctipt正则表达式</a></li><li><a href="http://www.pivotcode.com/web-test-tools-share-2/" title="网页测试工具分享">网页测试工具分享</a></li><li><a href="http://www.pivotcode.com/microsoft-expression-web-3-superpreview/" title="体验篇：Microsoft Expression Web 3 SuperPreview全攻略">体验篇：Microsoft Expression Web 3 SuperPreview全攻略</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.pivotcode.com/jquery-1-4-new-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

