<?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>刘钢的博客 - 我是UED &#187; 新特性</title>
	<atom:link href="http://www.iamued.com/tag/%e6%96%b0%e7%89%b9%e6%80%a7/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iamued.com</link>
	<description>http://www.IamUED.com</description>
	<lastBuildDate>Wed, 18 Jan 2012 02:51:35 +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 发布：15个新特性实例精讲</title>
		<link>http://www.iamued.com/qianduan/1182.html</link>
		<comments>http://www.iamued.com/qianduan/1182.html#comments</comments>
		<pubDate>Sun, 17 Jan 2010 09:27:44 +0000</pubDate>
		<dc:creator>RichieLiu</dc:creator>
				<category><![CDATA[JavaScript脚本]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery1.4]]></category>
		<category><![CDATA[新特性]]></category>

		<guid isPermaLink="false">http://www.iamued.com/?p=1182</guid>
		<description><![CDATA[jQuery 1.4 最近发布了。 超乎大家的预期，这次并非简单的修修补补，1.4 包含了很多新特性、功能增强和性能提升！本文即向您介绍这些可能对你十分有用的新特性和优化增强。 你可以立刻下载jQuery 1.4试用： http://code.jquery.com/jquery-1.4.js 下面带你进入Jquery1.4新特性实例精讲 1. 传参给 jQuery(…) 之前，jQuery可以通过 attr 方法设置元素的属性，既可传属性的名和值，也可以是包含几组特定 属性名值对 的 对象。在 jQuery 1.4 中，你可以把一个参数对象作为第二个参数传给 jQuery 函数本身，同时创建HTML元素。 比方说你想要创建一个带有几个属性的锚记元素（&#60;a&#62;&#60;/a&#62;）。在1.4中，一切如此简单： 你大概也能猜到，这个锚记元素没有的 text 属性会调用 jQuery 的私有方法 .text() ，把元素里的文字设置为“Go to Google!” 针对这一用法，下面是更有用的实例： id 为一般属性，被直接加上了，而 css 和 click 则激发了相应的 jQuery 方法。在1.4以前，上面的代码需写成这个样子： 详细了解 jQuery(…) 2. 直到遇见你&#8230; 1.4的DOM遍历工具包里又增加了3个新方法： nextUntil，   prevUntil  和  parentsUntil 。这些方法会按照特定的方向遍历DOM，直到遇到满足指定选择符的元素为止。举例来说，现在我们有一张水果名的清单： 我们想挑选出所有在 Apple 后，Pear 前的所有条目。代码十分简单： 详细了解： [...]]]></description>
			<content:encoded><![CDATA[<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>
<p>下面带你进入Jquery1.4新特性实例精讲</p>
<p><span id="more-1182"></span></p>
<p><strong>1. 传参给 jQuery(…)</strong></p>
<p>之前，jQuery可以通过 <a href="http://api.jquery.com/attr/" target="_blank">attr </a>方法设置元素的属性，既可传属性的名和值，也可以是包含几组特定 属性名值对 的 对象。在 jQuery 1.4 中，你可以把一个参数对象作为第二个参数传给 jQuery 函数本身，同时创建HTML元素。</p>
<p>比方说你想要创建一个带有几个属性的锚记元素（&lt;a&gt;&lt;/a&gt;）。在1.4中，一切如此简单：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('&lt;a/&gt;', {
    id: 'foo',
    href: 'http://google.com',
    title: 'Become a Googler',
    rel: 'external',
    text: 'Go to Google!'
});
</pre>
<p>你大概也能猜到，这个锚记元素没有的 text 属性会调用 jQuery 的私有方法 .text() ，把元素里的文字设置为“Go to Google!”</p>
<p>针对这一用法，下面是更有用的实例：</p>
<pre class="brush: jscript; title: ; notranslate">
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 class="brush: jscript; title: ; notranslate">
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>
<p><strong>2. 直到遇见你&#8230;</strong></p>
<p>1.4的DOM遍历工具包里又增加了3个新方法： nextUntil，   prevUntil  和  parentsUntil 。这些方法会按照特定的方向遍历DOM，直到遇到满足指定选择符的元素为止。举例来说，现在我们有一张水果名的清单：</p>
<pre class="brush: xml; title: ; notranslate">
&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 class="brush: jscript; title: ; notranslate">
jQuery('ul li:contains(Apple)').nextUntil(':contains(Pear)');
// 选出的是 Banana, Grape, Strawberry
</pre>
<p>详细了解： <a href="http://api.jquery.com/prevUntil/" target="_blank">prevUntil</a>, <a href="http://api.jquery.com/nextUntil/" target="_blank">nextUntil</a>, <a href="http://api.jquery.com/parentsUntil/" target="_blank">parentsUntil</a></p>
<p><strong>3. 绑定多个事件处理器</strong></p>
<p>不再需要把各个事件绑定方法“链”在一起，现在你可以把它们捆成一堆，如下：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('#foo).bind({
    click: function() {
        // do something
    },
    mouseover: function() {
        // do something
    },
    mouseout: function() {
        // do something
    }
})
</pre>
<p>这一用法也适用于 ” .one() “.</p>
<p><a href="http://api.jquery.com/bind/" target="_blank">详细了解 .bind(…)</a></p>
<p><strong>4. 依属性指定缓动效果</strong></p>
<p>以前只能为一个动画指定一种缓动效果（easing，即动画过程中的速度变化规律。jQuery 原生支持两种缓动效果，swing（默认）和linear 。要使用其他效果，你需要自己<a href="http://gsgd.co.uk/sandbox/jquery/easing/" target="_blank">另行下载</a>。），现在你可以为动画的各个属性参数指定不同的缓动效果：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('#foo').animate({
    left: 500,
    top: [500, 'easeOutBounce']
}, 2000);
</pre>
<p><a href="http://james.padolsey.com/demos/jquery/easing/easing-jq14.html" target="_blank">点此查看实际效果</a></p>
<p>你也可以在一个可选的动画选项对象中为 secialEasing 设置一系列名值对来完全上面的工作：</p>
<pre class="brush: jscript; title: ; notranslate">
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>
<p><strong>5. 更新的 Live 事件！</strong></p>
<p>jQuery 1.4 添加了对指派 <strong>submit </strong>， <strong>change </strong>， <strong>focus</strong> 和 <strong>blur</strong> 事件的支持。在jQuery中，我们利用” .live() ” 方法指派事件。当你想要为多个元素注册事件处理器时，这会非常有用。而且就算满足选择符的元素是新出现的，这些事件也会继续有效（使用  .live()  比不断重复绑定要省力省心得多）。</p>
<p><strong>不过，当心了！</strong> 注册 focus 和 blur 事件时你需要用 <strong>focusin</strong> 和 <strong>focusout</strong> 作为事件名。</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('input').live('focusin', function(){
    // do something with this
});
</pre>
<p><strong>6. 控制函数上下文</strong></p>
<p>jQuery 1.4 提供了一个全新的  proxy  函数，位于 jQuery 命名空间下。这一函数接受两个参数，一个是“作用域”（scope）或者一个方法名，另一个是某函数或者目标作用域（the intended scope）。</p>
<p>众所周知， JavaScript的 this 关键字是一个很难把握的东西。有时候你并不想它代表一个元素，而想让它代表你前面创建的某个对象。</p>
<p>例如，在这里我们创建了一个  app  对象，它拥有两个属性，一个是  clickHandler  方法，一个是负责参数配置的对象。</p>
<pre class="brush: jscript; title: ; notranslate">
var app = {
    config: {
        clickMessage: 'Hi!'
    },
    clickHandler: function() {
        alert(this.config.clickMessage);
    }
};
</pre>
<p>这个  clickHandler  方法，当像  app.clickHandler()  这样调用时， app  就是其上下文，也就是说  this  关键字指向的是  app  。如果只需简单地调用，这样的写法没什么问题：</p>
<pre class="brush: jscript; title: ; notranslate">
app.clickHandler(); // &quot;Hi!&quot; is alerted
</pre>
<p>不过如果把它当作一个事件处理器：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('a').bind('click', app.clickHandler);
</pre>
<p>当我们点击这个锚记时，并没有达到预想的效果（没东西 alert 出来）。这是因为 jQuery （以及大部分理智的事件模型），默认地，都会把处理器的上下文指定为目标元素本身。也就是说， this 所代表正是被点击的这个链接。而我们想的是， this  应该继续代表  app  。在jQuery 1.4中，实现这一目的十分简单：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('a').bind(
    'click',
    jQuery.proxy(app, 'clickHandler')
);
</pre>
<p>现在点击所有锚记都会弹出“Hi!”了。</p>
<p>代理函数把你的函数包裹一圈，同时把函数内的  this  设定为你想要东西。在其他上下文应用场景，如把回调函数传给其他 jQuery 方法或插件，代理函数也能派上用场。</p>
<p><a href="http://api.jquery.com/jQuery.proxy" target="_blank">了解更多  jQuery.proxy </a></p>
<p><strong>7.  动画队列延迟</strong></p>
<p>现在，可以给动画队列加一个延迟了。虽然这个功能可以在任何队列里实现，但最常用的可能还是延迟“fx 队列”（”fx” queue，jQuery默认的动画队列）。它能让你在两个动画行为之间暂停一下，而不用牵扯到回调函数和  setTimeout  之类的东西。 .delay()  的第一个参数即你需要设定的延迟毫秒数：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('#foo')
    .slideDown() // Slide down
    .delay(200) // Do nothing for 200 ms
    .fadeIn(); // Fade in
</pre>
<p>如果你想延迟一个除 fx 以外的队列，把队列名作为第二个参数传给  .delay() 就可以了 。</p>
<p><a href="http://api.jquery.com/delay" target="_blank">详细了解  .delay(…) </a></p>
<p><strong>8. 检测元素是否含有特定内容</strong></p>
<p>jQuery 1.4 让检测一个元素（或集合）是否含有（ .has() ）某项东西更为容易。其背后的机理和选择过滤器  :has() 是一样的。这个方法会从当前集合中选出满足任意指定条件之一的元素。</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('div').has('ul');
</pre>
<p>这条语句在所有DIV元素中挑出那些包含UL元素的。这种情况可能用选择过滤器  :has()  就好了，但当你要更程式化地过滤选择集时  .has()  方法就十分有用了。</p>
<p>jQuery 1.4 还在 jQuery 命名空间下新增了一个  contains   函数。这是一个比较底层的函数，接受两个 DOM 节点为参数。返回一个布尔值，指示第二个元素是否包含在第一个元素中。例如：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery.contains(document.documentElement, document.body);
// 返回true - &lt;body&gt; 确实包含在 &lt;html&gt; 中
</pre>
<p>详细了解： <a href="http://api.jquery.com/has/" target="_blank">.has(…) </a>, <a href="http://api.jquery.com/jQuery.contains/" target="_blank">jQuery.contains(…) </a></p>
<p><strong>9. 给元素剥皮！</strong></p>
<p>很早以前，jQuery 就可以用  .wrap()  给元素裹一层皮。现在， jQuery 1.4 添加了  .unwrap()  方法，用以剥皮。看下面的DOM结构：</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div&gt;
    &lt;p&gt;Foo&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>来把 p 元素外面的一层皮（div）剥掉：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('p').unwrap();
</pre>
<p>总而言之，这个方法可以把任意元素的父元素移除。.</p>
<p><a href="http://api.jquery.com/unwrap/" target="_blank">详细了解  .unwrap(…) </a></p>
<p><strong>10. 移除元素，而不删除数据</strong></p>
<p>以前有一个 .remove() 方法，是把元素剥离后抛弃。全新的  <strong>.detach() </strong>方法可以让你把一个元素从DOM中剥离，而不丢失数据。囊括该元素的 jQuery 对象会在操作完成后还保留这个元素。数据可以通过  .data()   或者 jQuery 事件系统中的任意事件处理器传入 jQuery 对象。</p>
<p>当你需要把某个元素从DOM中移除，然后在将来某个场景中重新引入时，这个函数会很有用。元素的事件句柄和其他所有数据都会被保留。</p>
<pre class="brush: jscript; title: ; notranslate">
var foo = jQuery('#foo');
// 绑定一个重要的事件处理器
foo.click(function(){
    alert('Foo!');
});
foo.detach(); // 从DOM中移除
// … do stuff
foo.appendTo('body'); // 重新加入到DOM
foo.click(); // 弹出alert信息： &quot;Foo!&quot;
</pre>
<p><a href="http://api.jquery.com/detach" target="_blank">详细了解  .detach(…) </a></p>
<p><strong>11. index(…) 的功能增强</strong></p>
<p>jQuery 1.4 为您使用  .index()  提供了两种新方法。以前，你必须把元素作为参数传给它，然后获得一个返回的数值，代表在当前集合中这个元素的索引。现在，如果不传参数过去，返回的值就代表某元素在其同辈中排行老几。比方说下面的DOM：</p>
<pre class="brush: xml; title: ; notranslate">
&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 class="brush: jscript; title: ; notranslate">
jQuery('li').click(function(){
    alert( jQuery(this).index() );
});
</pre>
<p>jQuery 1.4 也允许你将选择符作为  .index()  的第一个参数。这样做会返回当前元素在你指定的选择符所匹配出的元素集合中的索引值。</p>
<p>还得提醒一点，这一方法返回的是数值，如果文档中无法找到指定条件的元素，会返回数值 -1 。</p>
<p><a href="http://api.jquery.com/index" target="_blank">详细了解  .index(…) </a></p>
<p><strong>12. DOM 操纵可接收回调函数</strong></p>
<p>现在，大部分的DOM操纵方法都支持了将 函数 作为单一参数传入（ .css()  和  .attr()  传入为第二个参数）。这个函数会为选择集中的每一个元素都跑一遍，从而为相应操纵方法提供更“有个性”的值。</p>
<p>下列方法支持这一功能：</p>
<ul>
  </p>
<li><a href="http://api.jquery.com/after" target="_blank">after</a><br />
  </li>
<li><a href="http://api.jquery.com/before" target="_blank">before</a><br />
  </li>
<li><a href="http://api.jquery.com/append" target="_blank">append</a><br />
  </li>
<li><a href="http://api.jquery.com/prepend" target="_blank">prepend</a><br />
  </li>
<li><a href="http://api.jquery.com/addClass" target="_blank">addClass</a><br />
  </li>
<li><a href="http://api.jquery.com/toggleClass" target="_blank">toggleClass</a><br />
  </li>
<li><a href="http://api.jquery.com/removeClass" target="_blank">removeClass</a><br />
  </li>
<li><a href="http://api.jquery.com/wrap" target="_blank">wrap</a><br />
  </li>
<li><a href="http://api.jquery.com/wrapAll" target="_blank">wrapAll</a><br />
  </li>
<li><a href="http://api.jquery.com/wrapInner" target="_blank">wrapInner</a><br />
  </li>
<li><a href="http://api.jquery.com/val" target="_blank">val</a><br />
  </li>
<li><a href="http://api.jquery.com/text" target="_blank">text</a><br />
  </li>
<li><a href="http://api.jquery.com/replaceWith" target="_blank">replaceWith</a><br />
  </li>
<li><a href="http://api.jquery.com/css" target="_blank">css</a><br />
  </li>
<li><a href="http://api.jquery.com/attr" target="_blank">attr</a><br />
  </li>
<li><a href="http://api.jquery.com/html" target="_blank">html</a></li>
</ul>
<p>有了回调函数，你就能在选择集中利用  this  关键字来访问当前元素，用回调函数的第一个参数来访问其索引值。</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('li').html(function(i){
    return 'Index of this list item: ' + i;
});
</pre>
<p>还有还有，上面的某些方法还提供第二个参数供你利用。如果你调用的是一个设定型方法（如 .html()  和  .attr(&#8216;href&#8230;)  ），你还能取得当前值。例如：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('a').attr('href', function(i, currentHref){
    return currentHref + '?foo=bar';
});
</pre>
<p>如您所见，对于 .css()  和  .attr()  方法来说，之所以要把函数作为第二个参数传递，是因为第一个参数要用来指定我们需要修改的是哪一个属性：</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery('li').css('color', function(i, currentCssColor){
    return i % 2 ? 'red' : 'blue';
});
</pre>
<p><strong>13. 判定对象类型</strong></p>
<p>jQuery 1.4 新增了两个全新的辅助函数（都直接位于 jQuery 命名空间下），可以帮助你判别你正在操纵的是何种对象。</p>
<p>第一个函数是  isEmptyObject , ，它返回一个布尔值，判定对象是否为空（）。第二个是  isPlainObject  ，它返回的布尔值代表传递过去的参数是否为JavaScript的简单对象（plain object），也就是用  {} 或   new Object() 创建的对象。</p>
<pre class="brush: jscript; title: ; notranslate">
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" target="_blank">isPlainObject(…) </a>, <a href="http://api.jquery.com/jQuery.isEmptyObject" target="_blank">isEmptyObject(…) </a></p>
<p><strong>14. Closest(…) 的功能增强</strong></p>
<p>jQuery的 .closest()  方法现在可以接受一组选择符作为参数。当你需要遍历某一元素的所有上级，找到<strong>一个以上</strong>符合特定特征的最近元素时，此功能就能派上用场。</p>
<p>而且，现在它还能接受上下文环境作为第二个参数，也就是说你可以控制DOM遍历的深度或者说远度。虽然说大家可能很少会用到这两个新特性，但一旦用上效果是惊人的！</p>
<p><a href="http://api.jquery.com/closest" target="_blank">了解更多  .closest(…) </a></p>
<p><strong>15. 新事件！ focusIn 与 focusOut</strong></p>
<p>如前所述，现在部署 focus 和 blur 事件时，你需要使用 focusin 和 focusout 这两个新事件。这两个事件帮助你在特定元素或者该元素的子元素 获取/失去 焦点时采取行动。</p>
<pre class="brush: jscript; title: ; notranslate">
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" target="_blank">focusIn </a>和 <a href="http://api.jquery.com/focusout" target="_blank">focusOut </a>事件的内容。</p>
<p><a href="http://api.jquery.com/">Jquery1.4 API文档</a></p>
<p>  原文作者：<a href="http://james.padolsey.com/" target="_blank">James Padolsey</a><br />
  原文来自<a href="http://ktmud.com/" target="_blank">亲泥巴</a>：<a href="http://blog.ktmud.com/javascript/jquery-1-4-new-features/" target="_blank">jQuery 1.4 发布：15个新特性实例精讲</a><br />
  英文原文地址：<a href="http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/" target="_blank">http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iamued.com/qianduan/1182.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

