<?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>Hiro技术站 &#187; 前端工程</title>
	<atom:link href="http://www.ihiro.org/tag/front-end-engineering/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ihiro.org</link>
	<description></description>
	<lastBuildDate>Fri, 06 Jan 2012 07:58:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>XSLT:通过间隔符获取英文名的First Name和Last Name</title>
		<link>http://www.ihiro.org/xslt-get-the-first-last-name</link>
		<comments>http://www.ihiro.org/xslt-get-the-first-last-name#comments</comments>
		<pubDate>Fri, 06 Jan 2012 07:51:55 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[XML、XSLT]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>
		<category><![CDATA[前端工程]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1727</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2012/01/xml-xslt.png" alt="" title="xml-xslt" width="100" height="100" class="alignleft size-full wp-image-1732" />
<p>本文是工作中关于XSLT和XML一个小实例的分享。</p>
<p><strong>需求</strong>：XML抛出数据字段username,如Hiro xxx Zhang,需要通过XSLT获得First Name和Last Name，并填入到对应Form表单的字段中。</p>
<p>就XSLT来说，没有像JS中indexOf和lastIndexOf方法，所以不能够通过定位“空格”位置的方法去实现。唯一的方法只能通过“空格”反复去截取，知道剩下的字符串中没有空格。</p>
<p><strong>First Name</strong>通过截取第一个“空格”前的所有字符串：substring-before($name, ' ')。</p>
<p><strong>Lsat Name</strong>则通过递归的方法，方法截取“空格”后面的字符串，直到最后只剩下一个不含空格的字符串即可：xsl:template name="lastName"。</p>]]></description>
			<content:encoded><![CDATA[<p>本文是工作中关于XSLT和XML一个小实例的分享。</p>
<p><strong>需求</strong>：XML抛出数据字段username,如Hiro xxx Zhang,需要通过XSLT获得First Name和Last Name，并填入到对应Form表单的字段中。</p>
<p>就XSLT来说，没有像JS中indexOf和lastIndexOf方法，所以不能够通过定位“空格”位置的方法去实现。唯一的方法只能通过“空格”反复去截取，知道剩下的字符串中没有空格。</p>
<h3>1. XML数据：</h3>

<div class="wp_codebox"><table><tr id="p17271"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1727code1"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Hiro ODC Zhang<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>2. XSLT代码：</h3>

<div class="wp_codebox"><table><tr id="p17272"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p1727code2"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;html&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">omit-xml-declaration</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/xsl:variable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;firstName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring-before($name, ' ')&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/xsl:variable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call</span> -template <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with</span> -param <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$name&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/xsl:with<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:variable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        First Name: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$firstName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        Last Name: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$lastName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:when</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;contains($name, ' ')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call</span> -template <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with</span> -param <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring-after($name, ' ')&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/xsl:with<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:when<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value</span> -of <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$name&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>First Name</strong>通过截取第一个“空格”前的所有字符串：substring-before($name, &#8216; &#8216;)。</p>
<p><strong>Lsat Name</strong>则通过递归的方法，方法截取“空格”后面的字符串，直到最后只剩下一个不含空格的字符串即可：xsl:template name=”lastName”。</p>
<p><a href="http://www.ihiro.org/blog/wp-content/uploads/2012/01/TEST.zip" rel="external">下载XSLT代码文件</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li><li>2010年07月4日 -- <a href="http://www.ihiro.org/perforomance-best-pritices-for-web-developers" title="读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》">读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》</a> (10)</li><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/xslt-get-the-first-last-name/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CSS3:Transition属性详解</title>
		<link>http://www.ihiro.org/css3-transition</link>
		<comments>http://www.ihiro.org/css3-transition#comments</comments>
		<pubDate>Wed, 20 Apr 2011 06:40:44 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[HTML5、CSS3]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[学习笔记]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1656</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2011/04/css3-transition.png" alt="" title="css3-transition" width="100" height="100" class="alignleft size-full wp-image-1659" />
<p class="red"><strong>因博客服务器瘫痪，上周的所有数据丢失。该文为恢复后补发。</strong></p>
<p>暂时不做HTML5读书笔记的更新，下一篇再继续。临时性地插入CSS3新属性的详细介绍，以便可以和HTML5的知识结合起来合理应用，这样达到两者配合。</p>
<p>Transition属性主要是用来对某个CSS属性的变化过程进行控制，官方的介绍是”<em>CSS Transitions allow property changes in CSS values to occur smoothly over a specified duration.</em>“。而我个人则简单地理解为”<span class="green">在某个时间段内，平滑地改变某个CSS属性。</span>“。</p>
<p>Transition又包含了四个子属性，分别为property、duration、timing-function、delay。下面来一一介绍，在最后会给出一个简单的实例和使用方法说明。</p>]]></description>
			<content:encoded><![CDATA[<p class="red"><strong>因博客服务器瘫痪，上周的所有数据丢失。该文为恢复后补发。</strong></p>
<p>暂时不做HTML5读书笔记的更新，下一篇再继续。临时性地插入CSS3新属性的详细介绍，以便可以和HTML5的知识结合起来合理应用，这样达到两者配合。</p>
<p>Transition属性主要是用来对某个CSS属性的变化过程进行控制，官方的介绍是”<em>CSS Transitions allow property changes in CSS values to occur smoothly over a specified duration.</em>“。而我个人则简单地理解为”<span class="green">在某个时间段内，平滑地改变某个CSS属性。</span>“。</p>
<p>Transition又包含了四个子属性，分别为property、duration、timing-function、delay。下面来一一介绍，在最后会给出一个简单的实例和使用方法说明。</p>
<h3>1. transition-property:</h3>
<p>property针对了当前选择器的某个css属性进行设置。比如我要过渡一个背景色时，则设置property值为background。</p>
<h3>2. transition-duration:</h3>
<p>duration针对了过渡效果的持续时间。</p>
<h3>3. transition-timing-function:</h3>
<p>timing-function算是Transition属性中最为复杂的一个了。它针对了过渡效果的特效，有多种特效展示。这里得涉及到一个学术性的话题：貝茲曲線。说实话，我也没搞的太明白，但W3C给出了一张曲线图，一看就明白了（后附图）。</p>
<div>介绍下预留的几个特效：<br />
ease: cubic-bezier(0.25, 0.1, 0.25, 1.0)<br />
linear: cubic-bezier(0.0, 0.0, 1.0, 1.0)<br />
ease-in: cubic-bezier(0.42, 0, 1.0, 1.0)<br />
ease-out: cubic-bezier(0, 0, 0.58, 1.0)<br />
ease-in-out: cubic-bezier(0.42, 0, 0.58, 1.0)<br />
cubic-bezier(x1, y1, x2, y2) 为自定义，x1,x2,y1,y2的值范围在[0, 1]<br />
其中的cubic-bezier即为貝茲曲線中的绘制方法。先来看图：<img src="http://www.ihiro.org/blog/wp-content/uploads/2011/04/css3-transition.jpg" alt="" title="css3-transition" width="366" height="347" class="aligncenter size-full wp-image-1657" />图上有四点，P0-3，其中P0、P3是默认的点，对应了[0,0], [1,1]。而剩下的P1、P2两点则是我们通过cubic-bezier()自定义的。</div>
<p><strong>参考阅读：</strong><br />
W3C: <a href="http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag" rel="external">http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag</a><br />
貝茲曲線：<a href="http://zh.wikipedia.org/wiki/%E8%B2%9D%E8%8C%B2%E6%9B%B2%E7%B7%9A" rel="external">http://zh.wikipedia.org/wiki/%E8%B2%9D%E8%8C%B2%E6%9B%B2%E7%B7%9A</a></p>
<h3>4. transition-delay:</h3>
<p>duration针对了过渡效果的延迟执行时间。</p>
<h3>5. 代码演示：</h3>
<dl class="outer_dl">
<dt>1). 过渡单个属性：</dt>
<dd>

<div class="wp_codebox"><table><tr id="p16563"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1656code3"><pre class="css" style="font-family:monospace;">transition-property<span style="color: #00AA00;">:</span>opacity<span style="color: #00AA00;">;</span>
transition-duration<span style="color: #00AA00;">:</span>2s<span style="color: #00AA00;">;</span>
transition-timing-function<span style="color: #3333ff;">:ease-</span>in<span style="color: #00AA00;">;</span>
transition-delay<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span></pre></td></tr></table></div>

</dd>
<dt>2). 过渡多个属性：</dt>
<dd>
<dl class="inner_dl">
<dt>[1]. 上下一一对应型：</dt>
<dd>

<div class="wp_codebox"><table><tr id="p16564"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1656code4"><pre class="css" style="font-family:monospace;">transition-property<span style="color: #3333ff;">:opacity </span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
transition-duration<span style="color: #00AA00;">:</span>2s<span style="color: #00AA00;">,</span> 4s<span style="color: #00AA00;">;</span>
transition-timing-function<span style="color: #3333ff;">:ease-</span>in<span style="color: #00AA00;">;</span>
transition-delay<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<p>此时：opacity过渡时间为2s，left过渡时间为4s。</dd>
<dt>[2]. 循环对应型：</dt>
<dd>

<div class="wp_codebox"><table><tr id="p16565"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1656code5"><pre class="css" style="font-family:monospace;">transition-property<span style="color: #3333ff;">:opacity </span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">width</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">;</span>
transition-duration<span style="color: #00AA00;">:</span>2s<span style="color: #00AA00;">,</span> 4s<span style="color: #00AA00;">;</span>
transition-timing-function<span style="color: #3333ff;">:ease-</span>in<span style="color: #00AA00;">;</span>
transition-delay<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<p>此时：opacity和width过渡时间为2s，left和height过渡时间为4s。</dd>
</dl>
</dd>
<dt>3). transition简写模式：</dt>
<dd>顺序为：transition-property transition-duration transition-timing-function transition-delay</p>

<div class="wp_codebox"><table><tr id="p16566"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1656code6"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*单个属性：*/</span>
-moz-transition<span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">background</span> 0.5s ease-out 0s<span style="color: #00AA00;">;</span>
<span style="color: #808080; font-style: italic;">/*多个属性：*/</span>
-moz-transition<span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">,</span> 0.5s ease-out 0s<span style="color: #00AA00;">,</span> <span style="color: #000000; font-weight: bold;">color</span> <span style="color: #cc66cc;">0.4</span> ease-out 0s<span style="color: #00AA00;">;</span></pre></td></tr></table></div>

</dd>
</dl>
<h3>6. 实例展示：</h3>
<p>请用火狐打开，暂没添加其他浏览器支持。有兴趣的可以到Demo页面，下了源代码自行添加后测试。也可以查看我的头部导航，也用到了Transition的效果。</p>
<p class="demo-down-bar"><a href="http://ihiro.org/html5-css3/CSS3/01.transition.html" class="demo" rel="external">Demo</a></p>
<div>HTML代码：</p>

<div class="wp_codebox"><table><tr id="p16567"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1656code7"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;http://www.ihiro.org/&quot; target=&quot;_blank&quot;&gt;ihiro.org&lt;/a&gt;</pre></td></tr></table></div>

</div>
<div>CSS代码：</p>

<div class="wp_codebox"><table><tr id="p16568"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p1656code8"><pre class="css" style="font-family:monospace;">&lt;a href<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;http://www.ihiro.org/&quot;</span> target<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #00AA00;">&gt;</span>ihiro.org&lt;/a<span style="color: #00AA00;">&gt;</span>
a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">160px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#33589f</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-transform</span><span style="color: #00AA00;">:</span><span style="color: #993333;">uppercase</span><span style="color: #00AA00;">;</span>
    <span style="color: #808080; font-style: italic;">/*只有当鼠标移出后才处理*/</span>
    -moz-transition<span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">background</span> 0.2s linear 0s<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#263c7b</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#f60</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #933;">2px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#f00</span><span style="color: #00AA00;">;</span>
    <span style="color: #808080; font-style: italic;">/* 只有当鼠标移入时才处理
       注：若a:hover中不写transition,则会继承a中的transition */</span>
    <span style="color: #808080; font-style: italic;">/*
    1. 单个属性
        -moz-transition:background 0.5s ease-out 0s;
    2. 多个属性：
       -moz-transition:background, 0.5s ease-out 0s, color 0.4 ease-out 0s;
    */</span>
    -moz-transition<span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">background</span> 0.5s ease-out<span style="color: #00AA00;">,</span> <span style="color: #000000; font-weight: bold;">color</span> 0.4s ease-out<span style="color: #00AA00;">,</span> <span style="color: #000000; font-weight: bold;">text-shadow</span> 0.3s linear<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

</div>
<p>更多的Transition相关请阅读：<a href="http://www.w3.org/TR/css3-transitions/" rel="external">http://www.w3.org/TR/css3-transitions/</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li><li>2010年08月12日 -- <a href="http://www.ihiro.org/sizzle-custom-selector" title="Sizzle:开源JS选择器，如何自定义选择器">Sizzle:开源JS选择器，如何自定义选择器</a> (31)</li><li>2010年07月4日 -- <a href="http://www.ihiro.org/perforomance-best-pritices-for-web-developers" title="读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》">读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》</a> (10)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/css3-transition/feed</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
		<item>
		<title>HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</title>
		<link>http://www.ihiro.org/html5-reading-note-chp4-canvas</link>
		<comments>http://www.ihiro.org/html5-reading-note-chp4-canvas#comments</comments>
		<pubDate>Fri, 01 Apr 2011 06:11:03 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[HTML5、CSS3]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[学习笔记]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1648</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2011/04/canvas-logo.png" alt="" title="canvas-logo" width="100" height="100" class="alignleft size-full wp-image-1650" />
<p>《HTML5》的第四章节主要是对Canvas标签的属性以及相应的API进行了介绍，给出实例，这样学习起来就更加地容易和生动。</p>
<p>今天是愚人节，特意选择今天更新一篇博文。倒不是因为文章的内容和愚人节有什么关系，只是做个纪念。同时也是纪念一下哥哥（张国荣）去世8周年。</p>
<p>给出五个简单的实例，通过这些实例可以对Canvas的API有一个简要的认识和了解。在代码中也附加了注释，这样可以清楚地了解该行代码的含义。</p>]]></description>
			<content:encoded><![CDATA[<p>《HTML5》的第四章节主要是对Canvas标签的属性以及相应的API进行了介绍，给出实例，这样学习起来就更加地容易和生动。</p>
<p>今天是愚人节，特意选择今天更新一篇博文。倒不是因为文章的内容和愚人节有什么关系，只是做个纪念。同时也是纪念一下哥哥（张国荣）去世8周年。</p>
<p>给出五个简单的实例，通过这些实例可以对Canvas的API有一个简要的认识和了解。在代码中也附加了注释，这样可以清楚地了解该行代码的含义。</p>
<h3>一、绘制矩形：</h3>
<p>查看效果：<a href="http://ihiro.org/html5-css3/reading/CHP4.Canvas/1.drawRect.html" rel="external">1.drawRect.html</a></p>

<div class="wp_codebox"><table><tr id="p16489"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p1648code9"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> rect <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rect'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//画一个80x80的正方形</span>
rect.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#eee'</span><span style="color: #339933;">;</span>
rect.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//在80x80的正方形中间截去一个50x50的正方形</span>
rect.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//画一个空心的80x80的正方形</span>
rect.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#ccc'</span><span style="color: #339933;">;</span>
rect.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100.5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100.5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">180</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">180</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">//.5 for line width(two side), 2x0.5=1px</span></pre></td></tr></table></div>

</p>
<h3>二、绘制线条：</h3>
<p>查看效果：<a href="http://ihiro.org/html5-css3/reading/CHP4.Canvas/2.drawLine.html" rel="external">2.drawLine.html</a></p>

<div class="wp_codebox"><table><tr id="p164810"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1648code10"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'line'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//画一条垂直线</span>
line.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
line.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//画一条斜角线</span>
line.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//再一条斜角线闭合三角形</span>
line.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//给三角形边线着色</span>
line.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span>
line.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>三、绘制文本：</h3>
<p>查看效果：<a href="http://ihiro.org/html5-css3/reading/CHP4.Canvas/3.drawText.html" rel="external">3.drawText.html</a></p>

<div class="wp_codebox"><table><tr id="p164811"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p1648code11"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">240</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">//预留20px的空白</span>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">260</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//画右上的箭头</span>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">495</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">35</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">495</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">153</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">//预留20px的空白</span>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">173</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//画左下的箭头</span>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">65</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">55</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//着色</span>
context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#000&quot;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
context.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bold 12px sans-serif&quot;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//标注X坐标文字</span>
context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;x&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">248</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">43</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//标注Y坐标文字</span>
context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;y&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">58</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">165</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
context.<span style="color: #660066;">textBaseline</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;top&quot;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//标注起始坐标文字</span>
context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;(0, 0)&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//画左上角的标识点</span>
context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
context.<span style="color: #660066;">textAlign</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;right&quot;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">textBaseline</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bottom&quot;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//标注结束坐标文字</span>
context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;(500, 375)&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">492</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//画右下角的标识点</span>
context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">494</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">372</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>四、绘制渐变：</h3>
<p>查看效果：<a href="http://ihiro.org/html5-css3/reading/CHP4.Canvas/4.drawGradients.html" rel="external">4.drawGradients.html</a></p>

<div class="wp_codebox"><table><tr id="p164812"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1648code12"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gradients'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//创建一个宽300px的线性渐变， 决定了渐变方向，仅x1为横向， 仅x2为纵向， 两者都有为对角线渐变</span>
<span style="color: #003366; font-weight: bold;">var</span> my_gradients <span style="color: #339933;">=</span> context.<span style="color: #660066;">createLinearGradient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">225</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//起始色：黑色</span>
my_gradients.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'black'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//终止色： 白色</span>
my_gradients.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'white'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//设置填充色</span>
context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> my_gradients<span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//画300x225尺寸的线性渐变矩形</span>
context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">225</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>五、绘制图片：</h3>
<p>查看效果：<a href="http://ihiro.org/html5-css3/reading/CHP4.Canvas/5.drawImage.html" rel="external">5.drawImage.html</a></p>

<div class="wp_codebox"><table><tr id="p164813"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1648code13"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> cat <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//坐标5, 5处绘入 - 源图</span>
    context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>cat<span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//坐标100, 100处绘入 - 源图缩放成80x60尺寸</span>
    context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>cat<span style="color: #339933;">,</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//坐标200, 200处绘入 - 源图从10,10处取160x100尺寸，缩放到80x50尺寸</span>
    context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>cat<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">160</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p>下载本篇文章的代码压缩包：<a href="http://ihiro.org/html5-css3/reading/CHP4.Canvas/CHP4.Canvas.zip" rel="external">CHP4.Canvas.zip</a></p>
<h3>附加信息：</h3>
<p><strong class="green">开源代码，兼容主流浏览器，IE下都是通过VML实现兼容：</strong><br />
Google Code Canvas库：<a href="http://code.google.com/p/explorercanvas/" rel="external">http://code.google.com/p/explorercanvas/</a><br />
jQuery Maphilight 插件：<a href="http://davidlynch.org/js/maphilight/docs/" rel="external">http://davidlynch.org/js/maphilight/docs/</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年09月10日 -- <a href="http://www.ihiro.org/javascript-oop" title="Javascript:面向对象之静态、公有、私有、特权对象">Javascript:面向对象之静态、公有、私有、特权对象</a> (10)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年02月24日 -- <a href="http://www.ihiro.org/scroll-images" title="Javascript:图片无限循环滚动">Javascript:图片无限循环滚动</a> (27)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/html5-reading-note-chp4-canvas/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</title>
		<link>http://www.ihiro.org/html5-reading-note-chp3-elements</link>
		<comments>http://www.ihiro.org/html5-reading-note-chp3-elements#comments</comments>
		<pubDate>Mon, 21 Mar 2011 06:17:59 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[HTML5、CSS3]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[学习笔记]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1634</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2011/03/html5-elements.gif" alt="" title="html5-elements" width="100" height="100" class="alignleft size-full wp-image-1637" />
<p>即上篇的<a href="http://www.ihiro.org/html5-reading-note-chp2-features" rel="external">特性章节</a>之后，继续更新我的HTML5读书笔记。本篇主要记录一些标签方面的更新注意事项。一些普通的header、nav、article等标签的用法网上是一大堆，我只是把我觉得这方面重要的内容或者不容易记住的内容做了一个备份。</p>
<p>这些新的HTML5标签在IE8及以下版本中都不能支持，这就需要在页面加载之前使用JS动态地创建HTML5新标签，以实现页面的正确的渲染。当然这样处理并不是万能的，在处理一些AJAX交互时，就会出现返回的HTML5标签无法正确解析的问题。此时就需要<a href="http://jdbartlett.github.com/innershiv/" rel="internal">HTML5 innerShiv</a>来解决了。</p>]]></description>
			<content:encoded><![CDATA[<p>即上篇的<a href="http://www.ihiro.org/html5-reading-note-chp2-features" rel="external">特性章节</a>之后，继续更新我的HTML5读书笔记。本篇主要记录一些标签方面的更新注意事项。一些普通的header、nav、article等标签的用法网上是一大堆，我只是把我觉得这方面重要的内容或者不容易记住的内容做了一个备份。</p>
<p>这些新的HTML5标签在IE8及以下版本中都不能支持，这就需要在页面加载之前使用JS动态地创建HTML5新标签，以实现页面的正确的渲染。当然这样处理并不是万能的，在处理一些AJAX交互时，就会出现返回的HTML5标签无法正确解析的问题。此时就需要<a href="http://jdbartlett.github.com/innershiv/" rel="internal">HTML5 innerShiv</a>来解决了。</p>
<h3>1. Doctype:</h3>
<p>
<div class="wp_codebox"><table><tr id="p163414"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1634code14"><pre class="html" style="font-family:monospace;">&lt; !DOCTYPE html&gt;</pre></td></tr></table></div>

<p>无需再去添加XHTML的框架型、过渡性或者严谨型的声明。</p>
<h3>2. HTML</h3>
<p>
<div class="wp_codebox"><table><tr id="p163415"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1634code15"><pre class="html" style="font-family:monospace;">&lt;html lang=&quot;en&quot;&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>无需再添加xmlns属性来约束标签的命名空间了。</p>
<h3>3. HEAD:</h3>
<dl class="outer_dl">
<dt>1).meta:</dt>
<dd>

<div class="wp_codebox"><table><tr id="p163416"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1634code16"><pre class="html" style="font-family:monospace;">&lt;meta charset=&quot;utf-8&quot; /&gt;</pre></td></tr></table></div>

</dd>
<dt>2).link:</dt>
<dd>
<dl class="inner_dl">
<dt>2.1).rel=”stylesheet”</dt>
<dd>

<div class="wp_codebox"><table><tr id="p163417"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1634code17"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;style-original.css&quot; /&gt;</pre></td></tr></table></div>

</dd>
<dt>2.2).rel=”alertnate”     //用在订阅</dt>
<dd>

<div class="wp_codebox"><table><tr id="p163418"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1634code18"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;alternate&quot; type=&quot;application/atom+xml&quot; title=&quot;My Weblog feed&quot; href=&quot;/feed/&quot; /&gt;</pre></td></tr></table></div>

</dd>
<dt>2.3).rel=”icon”</dt>
<dd>

<div class="wp_codebox"><table><tr id="p163419"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1634code19"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;shortcut icon&quot; href=&quot;/favicon.ico&quot;&gt;&lt;/link&gt;</pre></td></tr></table></div>

</dd>
</dl>
</dd>
</dl>
<h3>4. How to style unknown elements in IE:</h3>
<p>
<div class="wp_codebox"><table><tr id="p163420"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1634code20"><pre class="html" style="font-family:monospace;">     &lt;!--[if lt IE 9]&gt;
          &lt;script type=&quot;text/javascript&quot;&gt;
               var e = (&quot;abbr,article,aside,audio,canvas,datalist,details,&quot; +
                         &quot;figure,footer,header,hgroup,mark,menu,meter,nav,output,&quot; +
                         &quot;progress,section,time,video&quot;).split(',');
               for (var i = 0; i &lt; e.length; i++) {
                    document.createElement(e[i]);
               }
          &lt;/script&gt;
     &lt; ![endif]--&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>OR:</p>

<div class="wp_codebox"><table><tr id="p163421"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1634code21"><pre class="html" style="font-family:monospace;">     &lt;!--[if lt IE 9]&gt;
          &lt;script type=&quot;text/javascript&quot; src=&quot;http://html5shiv.googlecode.com/svn/trunk/html5.js&quot;&gt;&lt;/script&gt;
     &lt; ![endif]--&gt;</pre></td></tr></table></div>

</p>
<h3>5. The HTML5 algorithm says that an &lt;article&gt; element creates a new section, that is, a new node in the document outline. And in HTML5, each section can have its own &lt;h1&gt; element.</h3>
<h3>6. Time and Date:</h3>
<p>
<div class="wp_codebox"><table><tr id="p163422"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1634code22"><pre class="html" style="font-family:monospace;">&lt;time datetime=&quot;2009-10-22&quot; pubdate=&quot;pubdate&quot;&gt;October 22, 2009&lt;/time&gt;</pre></td></tr></table></div>
</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年09月10日 -- <a href="http://www.ihiro.org/javascript-oop" title="Javascript:面向对象之静态、公有、私有、特权对象">Javascript:面向对象之静态、公有、私有、特权对象</a> (10)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年02月24日 -- <a href="http://www.ihiro.org/scroll-images" title="Javascript:图片无限循环滚动">Javascript:图片无限循环滚动</a> (27)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/html5-reading-note-chp3-elements/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</title>
		<link>http://www.ihiro.org/html5-reading-note-chp2-features</link>
		<comments>http://www.ihiro.org/html5-reading-note-chp2-features#comments</comments>
		<pubDate>Tue, 15 Mar 2011 02:55:31 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[HTML5、CSS3]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[学习笔记]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1626</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2011/03/html5_logo.gif" alt="" title="html5_logo" width="100" height="100" class="alignleft size-full wp-image-1627" />
<p>HTML5最近确实很火，逼的我不得不继续自学下去。去年年底的时候看了一些《Pro HTML5》的章节。因为过年，后来就断掉了。除了一些练习的代码外，没留下什么笔记。</p>
<p>年后没多久我就开始看起了《HTML5. Up and Running》这本书，中文版《HTML5揭秘》已经上市。我到今天也看到了第六章。摘取了一些扼要的东西，做了一些简单的笔记，以便以后的翻查。Everbox这个时候就发挥到了作用，让我随时随地想翻的时候都可以翻一翻。</p>
<p class="green">这篇文章只是开篇，后面会陆续有些我自己的笔记分享，很简单的文字。</p>]]></description>
			<content:encoded><![CDATA[<p>HTML5最近确实很火，逼的我不得不继续自学下去。去年年底的时候看了一些《Pro HTML5》的章节。因为过年，后来就断掉了。除了一些练习的代码外，没留下什么笔记。</p>
<p>年后没多久我就开始看起了《HTML5. Up and Running》这本书，中文版《HTML5揭秘》已经上市。我到今天也看到了第六章。摘取了一些扼要的东西，做了一些简单的笔记，以便以后的翻查。Everbox这个时候就发挥到了作用，让我随时随地想翻的时候都可以翻一翻。</p>
<p class="green">这篇文章只是开篇，后面会陆续有些我自己的笔记分享，很简单的文字。</p>
<p>本文介绍的是HTML5特性判断：</p>
<h3>1. Canvas:</h3>
<p>
<div class="wp_codebox"><table><tr id="p162623"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code23"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">!!</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>2. Video:</h3>
<p>
<div class="wp_codebox"><table><tr id="p162624"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code24"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">!!</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'video'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">canPlayType</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Video Formats:</p>

<div class="wp_codebox"><table><tr id="p162625"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1626code25"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;video&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">return</span> v.<span style="color: #660066;">canPlayType</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>3. Local Storage: </h3>
<p>
<div class="wp_codebox"><table><tr id="p162626"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code26"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'localStorage'</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> window<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'localStorage'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>4. Web Workers:</h3>
<p>
<div class="wp_codebox"><table><tr id="p162627"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code27"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">!!</span>window.<span style="color: #660066;">Worker</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>5. Offline Web Applications:</h3>
<p>
<div class="wp_codebox"><table><tr id="p162628"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code28"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">!!</span>window.<span style="color: #660066;">applicationCache</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>6. Geolocation:</h3>
<p>
<div class="wp_codebox"><table><tr id="p162629"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code29"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">!!</span>navigator.<span style="color: #660066;">geolocation</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>7. From:</h3>
<p>
Input:</p>

<div class="wp_codebox"><table><tr id="p162630"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1626code30"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
i.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">return</span> i.<span style="color: #660066;">type</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;text&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Placeholder Text:</p>

<div class="wp_codebox"><table><tr id="p162631"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1626code31"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'placeholder'</span> <span style="color: #000066; font-weight: bold;">in</span> i<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Form Autofocus:</p>

<div class="wp_codebox"><table><tr id="p162632"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1626code32"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'autofocus'</span> <span style="color: #000066; font-weight: bold;">in</span> i<span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>8. Microdata:</h3>
<p>
<div class="wp_codebox"><table><tr id="p162633"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1626code33"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!!</span>document.<span style="color: #660066;">getItems</span><span style="color: #339933;">;</span></pre></td></tr></table></div>
</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年09月10日 -- <a href="http://www.ihiro.org/javascript-oop" title="Javascript:面向对象之静态、公有、私有、特权对象">Javascript:面向对象之静态、公有、私有、特权对象</a> (10)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年02月24日 -- <a href="http://www.ihiro.org/scroll-images" title="Javascript:图片无限循环滚动">Javascript:图片无限循环滚动</a> (27)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/html5-reading-note-chp2-features/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Javascript:根据指定日期获得星期数</title>
		<link>http://www.ihiro.org/javascript-getweek-by-date</link>
		<comments>http://www.ihiro.org/javascript-getweek-by-date#comments</comments>
		<pubDate>Thu, 30 Dec 2010 08:06:25 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[Pure Javascript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1583</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/12/calendar.jpg" alt="" title="calendar" width="100" height="100" class="alignleft size-full wp-image-1584" />
<p>今天在写一个简单的日历程序，写着写着发现没办法把日期和头部的星期数对应起来，便通过Google搜索到了这个叫做“蔡勒（Zeller）公式”。根据这个公式写了个方法，发现还是蛮管用的。</p>
<p>不过就这个格式描述（见内页）来说少讲了一点，就是计算出来的结果若为负数的话，就需要再加上7，这样得出来的结果才是正确的。</p>
<p>当然这个公式不仅仅适合与JS,其他语言也可以写出来。原理一致...</p>]]></description>
			<content:encoded><![CDATA[<p>今天在写一个简单的日历程序，写着写着发现没办法把日期和头部的星期数对应起来，便通过Google搜索到了这个叫做“蔡勒（Zeller）公式”。根据这个公式写了个方法，发现还是蛮管用的。</p>
<blockquote class="quote"><p><span></p>
<p><strong>蔡勒（Zeller）公式：w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1</strong></p>
<p>公式中的符号含义如下，w：星期；c：世纪-1；y：年（两位数）；m：月（m 大于等于3，小于等于14，即在蔡勒公式中，某年的1、2月要看作上一年的13、14月来计算，比如2003年1月1日要看作2002年的13月1日来计算）；d：日；[ ]代表取整，即只要整数部分。(C是世纪数减一，y是年份后两位，M是月份，d是日数。1月和2月要按上一年的13月和 14月来算，这时C和y均按上一年取值。)</p>
<p>算出来的W除以7，余数是几就是星期几。如果余数是0，则为星期日。</p>
<p>以2049年10月1日（100周年国庆）为例，用蔡勒（Zeller）公式进行计算，过程如下：<br />蔡勒（Zeller）公式：w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1<br />=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1<br />=49+[12.25]+5-40+[28.6]<br />=49+12+5-40+28<br />=54 (除以7余5)<br />即2049年10月1日（100周年国庆）是星期5。</p>
<p>摘自：<a href="http://www.cnblogs.com/chingho/archive/2010/03/26/1697282.html">http://www.cnblogs.com/chingho/archive/2010/03/26/1697282.html</a></p>
<p></span></p></blockquote>
<p>不过就这个格式描述来说少讲了一点，就是计算出来的结果若为负数的话，就需要再加上7，这样得出来的结果才是正确的。</p>
<p>当然这个公式不仅仅适合与JS,其他语言也可以写出来。原理一致&#8230;</p>
<p class="demo-down-bar"><a target="_blank" href="http://www.ihiro.org/cases/getWeek/getWeek.zip" rel="external" class="download">download</a> <a target="_blank" href="http://www.ihiro.org/cases/getWeek/" rel="external" class="demo">demo</a></p>
<h3>公式计算代码：</h3>

<div class="wp_codebox"><table><tr id="p158334"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p1583code34"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
 * 	获得指定日期的星期数，1-6为星期一到星期六，0为星期天
 *	@y 年份
 *	@m 月份
 *	@d 日
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> getWeek<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span> m<span style="color: #339933;">,</span> d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> _int <span style="color: #339933;">=</span> parseInt<span style="color: #339933;">,</span>
		c <span style="color: #339933;">=</span> _int<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">/</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	y <span style="color: #339933;">=</span> y.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	y <span style="color: #339933;">=</span> _int<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m <span style="color: #339933;">===</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		m <span style="color: #339933;">=</span> <span style="color: #CC0000;">13</span><span style="color: #339933;">;</span>
		y<span style="color: #339933;">--;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m <span style="color: #339933;">===</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		m <span style="color: #339933;">=</span> <span style="color: #CC0000;">14</span><span style="color: #339933;">;</span>
		y<span style="color: #339933;">--;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> y <span style="color: #339933;">+</span> _int<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">/</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> _int<span style="color: #009900;">&#40;</span>c<span style="color: #339933;">/</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>c <span style="color: #339933;">+</span> _int<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">26</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>m<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> d <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
	w <span style="color: #339933;">=</span> w<span style="color: #339933;">%</span>7<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> w <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">?</span> w <span style="color: #339933;">:</span> w<span style="color: #339933;">+</span><span style="color: #CC0000;">7</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年02月24日 -- <a href="http://www.ihiro.org/scroll-images" title="Javascript:图片无限循环滚动">Javascript:图片无限循环滚动</a> (27)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/javascript-getweek-by-date/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>CSS:position属性定位总结</title>
		<link>http://www.ihiro.org/css-position-hack</link>
		<comments>http://www.ihiro.org/css-position-hack#comments</comments>
		<pubDate>Thu, 25 Nov 2010 03:37:42 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[CSS、XHTML]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[总结笔记]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1568</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/11/position-logo.jpg" alt="" title="position-logo" width="100" height="100" class="alignleft size-full wp-image-1571" />
<p>在写这篇文章之前，我一直以为css中position定位时一直是读取最后出现两个方位属性的值。昨天给一同事演示jQuery的animate特效时，我试图通过js来控制已经设置过top和left的DIV的right属性为0，以达到滑动到最右边的效果。结果没有效果，很是郁闷。</p>
<p>早上写了一些关于postion的样式做测试，发现不论给某个DIV设置了多少个方位（当然最多到4个），也不管存放的顺序是如何颠倒，它的读取顺序依旧是top-bottom-left-right.很符合日常所说的“上下左右”规则。这与我之前理解的不相符合。</p>]]></description>
			<content:encoded><![CDATA[<p>在写这篇文章之前，我一直以为css中position定位时一直是读取最后出现两个方位属性的值。昨天给一同事演示jQuery的animate特效时，我试图通过js来控制已经设置过top和left的DIV的right属性为0，以达到滑动到最右边的效果。结果没有效果，很是郁闷。</p>
<p>早上写了一些关于postion的样式做测试，发现不论给某个DIV设置了多少个方位（当然最多到4个），也不管存放的顺序是如何颠倒，它的读取顺序依旧是top-bottom-left-right.很符合日常所说的“上下左右”规则。这与我之前理解的不相符合。</p>
<p>另外我们也知道给一个标签设置magin、padding、border等时，它的顺序是“上右下左”，这个顺序和方位读取顺序也没有类似的关系。为此我总结了一份Demo页面，供大家对比方位读取顺序，也算是一个总结笔记吧。</p>
<p class="demo-down-bar"><a target="_blank" href="http://www.ihiro.org/cases/position/position.zip" rel="external" class="download">download</a> <a target="_blank" href="http://www.ihiro.org/cases/position/" rel="external" class="demo">demo</a></p>
<p class="green"><strong>附文中jQuery的animate示例：</strong></p>
<h3>Html代码：</h3>

<div class="wp_codebox"><table><tr id="p156835"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1568code35"><pre class="html" style="font-family:monospace;">&lt;button id=&quot;trigger&quot;&gt;Click To Animate&lt;/button&gt;
&lt;div id=&quot;box&quot;&gt;
	&lt;div class=&quot;box&quot; id=&quot;box1&quot;&gt;&lt;/div&gt;
	&lt;div class=&quot;box&quot; id=&quot;box2&quot;&gt;&lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<h3>CSS代码：</h3>

<div class="wp_codebox"><table><tr id="p156836"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1568code36"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#box</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">800px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">dashed</span> <span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">120px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">40px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#f60</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#390</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#box1</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#box2</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">62px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>jQuery代码：</h3>

<div class="wp_codebox"><table><tr id="p156837"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1568code37"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#trigger'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.box'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'right'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>结果执行JS后，Firebug中实时Html代码为：<a href="http://www.ihiro.org/blog/wp-content/uploads/2010/11/firebug-code.jpg"><img src="http://www.ihiro.org/blog/wp-content/uploads/2010/11/firebug-code.jpg" alt="" title="firebug-code" width="494" height="59" class="aligncenter size-full wp-image-1570" /></a>而通过Cssviewer观察到的代码不变：<a href="http://www.ihiro.org/blog/wp-content/uploads/2010/11/cssviewer-code.jpg"><img src="http://www.ihiro.org/blog/wp-content/uploads/2010/11/cssviewer-code.jpg" alt="" title="cssviewer-code" width="284" height="147" class="aligncenter size-full wp-image-1569" /></a></p>
<p class="red">所以，不要试图用JS来改变已设定top和left的DIV的right和bottom值，当然改变top和left值还是会有效果出来的。</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2011年03月2日 -- <a href="http://www.ihiro.org/css-specific-for-internet-explorer" title="CSS:区分IE版本的三个方法">CSS:区分IE版本的三个方法</a> (27)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/css-position-hack/feed</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Range:文本框选区学习笔记</title>
		<link>http://www.ihiro.org/text-range</link>
		<comments>http://www.ihiro.org/text-range#comments</comments>
		<pubDate>Mon, 22 Nov 2010 05:38:30 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[Pure Javascript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Range]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[学习笔记]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1556</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/11/text-range.jpg" alt="" title="text-range" width="100" height="100" class="alignleft size-full wp-image-1557" />
<p>有一段时间没有看《Javascript高级程序设计》了，最近重新接着看起，发现新的两个勘误，已经提交给<a href="http://www.cn-cuckoo.com/" rel="external">为之漫笔</a>了。</p>
<p>昨晚看到了文本框选区跨浏览器处理的部分。之前参加的交流会上，Taobao的前端分享过他们的Editor，主要的技术也是Range的应用。Range主要还是用在即见即所得Editor中，本文纯粹是做笔记之用，为避免以后自己遇到这样的需求而到处搜索，不如在自己的Blog上做个标记。</p>
<p>也做了个Demo页面，有兴趣的可以点击了查看效果。</p>]]></description>
			<content:encoded><![CDATA[<p>有一段时间没有看《Javascript高级程序设计》了，最近重新接着看起，发现新的两个勘误，已经提交给<a href="http://www.cn-cuckoo.com/" rel="external">为之漫笔</a>了。</p>
<p>昨晚看到了文本框选区跨浏览器处理的部分。之前参加的交流会上，Taobao的前端分享过他们的Editor，主要的技术也是Range的应用。Range主要还是用在即见即所得Editor中，本文纯粹是做笔记之用，为避免以后自己遇到这样的需求而到处搜索，不如在自己的Blog上做个标记。</p>
<p>也做了个Demo页面，有兴趣的可以点击了查看效果。</p>
<p class="demo-down-bar"><a href="http://www.ihiro.org/cases/textRange/textRange.zip" rel="external" class="download">download</a> <a href="http://www.ihiro.org/cases/textRange/" rel="external" class="demo">demo</a></p>
<h3>一、Html代码：</h3>
<p>
<div class="wp_codebox"><table><tr id="p155638"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1556code38"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
	&lt;li&gt;&lt;input type=&quot;text&quot; value=&quot;Hello World!&quot; id=&quot;textbox&quot; name=&quot;textbox&quot; size=&quot;50&quot; /&gt;&lt;/li&gt;
&nbsp;
    &lt;li&gt;&lt;input type=&quot;button&quot; id=&quot;getSelectedBtn&quot; value=&quot;getSelectedText&quot; /&gt;&lt;/li&gt;
&nbsp;
    &lt;li&gt;&lt;label for=&quot;startIndex&quot;&gt;开始位置：&lt;/label&gt;&lt;input type=&quot;text&quot; size=&quot;5&quot; id=&quot;startIndex&quot; name=&quot;startIndex&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;label for=&quot;stopIndex&quot;&gt;结束位置：&lt;/label&gt;&lt;input type=&quot;text&quot; size=&quot;5&quot; id=&quot;stopIndex&quot; name=&quot;endIndex&quot; /&gt;&lt;/li&gt;
&nbsp;
	&lt;li&gt;&lt;input type=&quot;button&quot; id=&quot;selectBtn&quot; value=&quot;selectText&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></td></tr></table></div>

</p>
<h3>二、Javascript代码：</h3>
<p>
<div class="wp_codebox"><table><tr id="p155639"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p1556code39"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> textbox <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'textbox'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	getSelectedBtn <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'getSelectedBtn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	selectBtn <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selectBtn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getSelectedText<span style="color: #009900;">&#40;</span>textbox<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">selection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">selection</span>.<span style="color: #660066;">createRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//IE</span>
		<span style="color: #000066; font-weight: bold;">return</span> textbox.<span style="color: #660066;">value</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>textbox.<span style="color: #660066;">selectionStart</span><span style="color: #339933;">,</span> textbox.<span style="color: #660066;">selectionEnd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> selectText<span style="color: #009900;">&#40;</span>textbox<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> startIndex <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'startIndex'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> 
		stopIndex <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stopIndex'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>textbox.<span style="color: #660066;">setSelectionRange</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		textbox.<span style="color: #660066;">setSelectionRange</span><span style="color: #009900;">&#40;</span>startIndex<span style="color: #339933;">,</span> stopIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>textbox.<span style="color: #660066;">createTextRange</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//IE</span>
		<span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> textbox.<span style="color: #660066;">createTextRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//创建范围</span>
		range.<span style="color: #660066;">collapse</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				<span style="color: #006600; font-style: italic;">//折叠预选取范围到开始位置</span>
		range.<span style="color: #660066;">moveStart</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'character'</span><span style="color: #339933;">,</span> startIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//将预选取范围的起点和终点移动到相同的位置</span>
		range.<span style="color: #660066;">moveEnd</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'character'</span><span style="color: #339933;">,</span> stopIndex <span style="color: #339933;">-</span> startIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//选取字符长度</span>
		range.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			<span style="color: #006600; font-style: italic;">//选取范围</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>stopIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	textbox.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
getSelectedBtn.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>getSelectedText<span style="color: #009900;">&#40;</span>textbox<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
selectBtn.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	selectText<span style="color: #009900;">&#40;</span>textbox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>
</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年09月10日 -- <a href="http://www.ihiro.org/javascript-oop" title="Javascript:面向对象之静态、公有、私有、特权对象">Javascript:面向对象之静态、公有、私有、特权对象</a> (10)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年02月24日 -- <a href="http://www.ihiro.org/scroll-images" title="Javascript:图片无限循环滚动">Javascript:图片无限循环滚动</a> (27)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/text-range/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>HTML5:《Pro HTML5 Programming》英文电子书推荐</title>
		<link>http://www.ihiro.org/html5-pro-ebook-recommendations</link>
		<comments>http://www.ihiro.org/html5-pro-ebook-recommendations#comments</comments>
		<pubDate>Wed, 03 Nov 2010 08:52:55 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[书籍文档]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[读书]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1552</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/11/Pro_Html5_logo.jpg" alt="" title="Pro_Html5_logo" width="100" height="100" class="alignleft size-full wp-image-1553" />
<p>介于上一篇文章：<a href="http://www.ihiro.org/jquery-plugin-recommended-maphighlight" rel="external">jQuery:热区高亮maphighlight插件推荐</a>，特向大家推荐一本HTML5的学习书籍。</p>
<p>现在国内关于HTML5的书籍几乎没有，即使有也还在翻译或编写中，没出版。这段时间在新公司或许英文文档看的多了，现在阅读器英文的书籍也相对轻松了许多。上上周的时候看完了Canvas API的相关，上周忙于工作，到现在也没在继续往下看。等时间空了继续看下去。</p>
<p>并不指望在实际的项目开发中用到，纯粹是兴趣爱好，或者是以后可能个人做些手机终端的开发。毕竟手机终端对HTML5的支持还是蛮多的。</p>]]></description>
			<content:encoded><![CDATA[<p>介于上一篇文章：<a href="http://www.ihiro.org/jquery-plugin-recommended-maphighlight" rel="external">jQuery:热区高亮maphighlight插件推荐</a>，特向大家推荐一本HTML5的学习书籍。</p>
<p>现在国内关于HTML5的书籍几乎没有，即使有也还在翻译或编写中，没出版。这段时间在新公司或许英文文档看的多了，现在阅读器英文的书籍也相对轻松了许多。上上周的时候看完了Canvas API的相关，上周忙于工作，到现在也没在继续往下看。等时间空了继续看下去。</p>
<p>并不指望在实际的项目开发中用到，纯粹是兴趣爱好，或者是以后可能个人做些手机终端的开发。毕竟手机终端对HTML5的支持还是蛮多的。</p>
<p>上篇的插件推荐其实漏掉了一部分内容，虽然该插件使用了HTML5的Canvas标签实现了绘图功能，但其实在IE下是不支持的。那么在IE下它是如何实现兼容的呢？原来它是使用了IE下特有的VML API，更多的参考可以访问：<a href="http://www.itlearner.com/code/vml/index.html" rel="external">VML教程 (Thinking in VML)</a>，我这就不做什么解释了，因为我也没去研究。(略图：)<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/11/VML_code_thum.jpg" alt="" title="VML_code_thum" width="600" height="284" class="aligncenter size-full wp-image-1554" /></p>
<p>最后附上《Pro HTML5 Programming》的下载地址：<a href="http://www.fileserve.com/file/vy9367V/Pro.HTML5.Programming.pdf" rel="external">http://www.fileserve.com/file/vy9367V/Pro.HTML5.Programming.pdf</a></p></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年07月4日 -- <a href="http://www.ihiro.org/perforomance-best-pritices-for-web-developers" title="读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》">读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》</a> (10)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年12月7日 -- <a href="http://www.ihiro.org/beginning-python" title="Python:Beginning Python['paiθən]">Python:Beginning Python['paiθən]</a> (27)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/html5-pro-ebook-recommendations/feed</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>读书:《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》</title>
		<link>http://www.ihiro.org/perforomance-best-pritices-for-web-developers</link>
		<comments>http://www.ihiro.org/perforomance-best-pritices-for-web-developers#comments</comments>
		<pubDate>Sun, 04 Jul 2010 14:47:42 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[书籍文档]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[读书]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1477</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/07/web_book.jpg" alt="" title="web_book" width="100" height="100" class="alignleft size-full wp-image-1478" />
<p>相信关注互联网或者做前端的朋友一定阅读过《高性能网站建设指南》这本前端惊世之作，我也读过，读得不够详细，但从中也学到了一些前端性能优化的技术，并在实际的项目应用于其中。</p>
<p>今天推荐的这本书是《高性能网站建设指南》进阶版，原作者从更加详细的细节方面全面地介绍Web性能优化的知识。该书是今年4月的时候才出版，我也是近两天无意间发现有新书出版，便急切的在亚马逊购买了阅读起来。看了一些，发觉很是不错，便写篇文章推荐给大家。</p>
<p>购买链接：<a href="http://www.amazon.cn/gp/product/product-description/B003FO0MEC/ref=dp_proddesc_0?ie=UTF8&#038;s=books" rel="external">http://www.amazon.cn/gp/product/product-description/B003FO0MEC/ref=dp_proddesc_0?ie=UTF8&#038;s=books</a></p>]]></description>
			<content:encoded><![CDATA[<p>相信关注互联网或者做前端的朋友一定阅读过《高性能网站建设指南》这本前端惊世之作，我也读过，读得不够详细，但从中也学到了一些前端性能优化的技术，并在实际的项目应用于其中。</p>
<p>今天推荐的这本书是《高性能网站建设指南》进阶版，原作者从更加详细的细节方面全面地介绍Web性能优化的知识。该书是今年4月的时候才出版，我也是近两天无意间发现有新书出版，便急切的在亚马逊购买了阅读起来。看了一些，发觉很是不错，便写篇文章推荐给大家。</p>
<p class="red">下面部分介绍来自卓越亚马逊。</p>
<h3>内容简介</h3>
<p>《高性能网站建设进阶指南:Web开发者性能优化最佳实践》是《高性能网站建设指南》姊妹篇。作者Steve Souders是Google Web性能布道者和Yahoo！前首席性能工程师。在《高性能网站建设进阶指南：Web开发者性能优化最佳实践》中，Souders与8位专家分享了提升网站性能的最佳实践和实用建议，主要包括：理解Ajax性能，编写高效的JavaScript，创建快速响应的应用程序、无阻塞加载脚本，跨域共享资源，无损压缩图片大小，使用块编码加快网页渲染；避免或取代iframe的方法，简化CSS选择符，以及其他技术。</p>
<h3>章节介绍</h3>
<p>第1章：理解Ajax性能<br />
第2章：创建快速响应的Web应用<br />
第 3章：拆分初始化负载<br />
第4章：无阻塞加载脚本<br />
第5章：整合异步脚本<br />
第6章：布置行内脚本<br />
第7章：编写高效的JavaScript<br />
第8章：可伸缩的Comet<br />
第9章：超越Gzip压缩<br />
第10章：图像优化<br />
第11章：划分主域<br />
第12章：尽早刷新文档的输出<br />
第 13章：少用iframe<br />
第14章：简化CSS选择符</p>
<p>《高性能网站建设进阶指南:Web开发者性能优化最佳实践 》购买链接：<a href="http://www.amazon.cn/gp/product/product-description/B003FO0MEC/ref=dp_proddesc_0?ie=UTF8&#038;s=books" rel="external">http://www.amazon.cn/gp/product/product-description/B003FO0MEC/ref=dp_proddesc_0?ie=UTF8&#038;s=books</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年11月3日 -- <a href="http://www.ihiro.org/html5-pro-ebook-recommendations" title="HTML5:《Pro HTML5 Programming》英文电子书推荐">HTML5:《Pro HTML5 Programming》英文电子书推荐</a> (21)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年12月7日 -- <a href="http://www.ihiro.org/beginning-python" title="Python:Beginning Python['paiθən]">Python:Beginning Python['paiθən]</a> (27)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/perforomance-best-pritices-for-web-developers/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery:揭露jQuery的隐藏功能</title>
		<link>http://www.ihiro.org/uncovering-jquerys-hidden-features</link>
		<comments>http://www.ihiro.org/uncovering-jquerys-hidden-features#comments</comments>
		<pubDate>Mon, 15 Mar 2010 05:43:30 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[前端工程译文]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[译文]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1158</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/jQuery-hidden-features.png" alt="" title="jQuery-hidden-features" width="100" height="100" class="alignleft size-full wp-image-1162" />
<p>jQuery并不像他表面显示的那样，只有那些额定的方法供我们使用。其他在他库的代码内有着很多很酷很便捷的潜在方法，等待被我们发现。在本文中，我将带领你去领略一些我在jQuery库中发现的那些不怎么常见的方法。</p>
<p>jQuery中的隐藏功能远远不止文章提到的这些，在以后的文章中会再总结一些我自己发现的功能与大家分享。</p>
<p>本文翻译自：<a href="http://net.tutsplus.com/tutorials/javascript-ajax/uncovering-jquerys-hidden-features/" rel="external">Uncovering jQuery’s Hidden Features</a></p>]]></description>
			<content:encoded><![CDATA[<p>jQuery并不像他表面显示的那样，只有那些额定的方法供我们使用。其他在他库的代码内有着很多很酷很便捷的潜在方法，等待被我们发现。在本文中，我将带领你去领略一些我在jQuery库中发现的那些不怎么常见的方法。</p>
<h3>1.理解jQuery方法：</h3>
<p>当你调用jQuery方法时会发生什么呢？看看jQuery的方法体，非常地简单。</p>

<div class="wp_codebox"><table><tr id="p115840"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1158code40"><pre class="javascript" style="font-family:monospace;">jQuery <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>selector<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// The jQuery object is actually just the init constructor 'enhanced'</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span>selector<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>在表层的代码之下，jQuery方法（通常称之为“包装”方法）将返回一个实例化后的jQuery对象，即&#8217;jQuery.fn.init&#8217;构造函数的一个实例。</p>
<p>理解了这一点非常有用，当我们每次调用jQuery方法时，都会返回一个带有一组属性的完全独立的对象。而jQuery的聪明之处在于返回的这个对象是以数组的方式返回的。该对象中的所有元素（通称”集合“）都是由一个数字索引所指向，就像数组中的下标一样。而且jQuery还返回了一个对象的length属性，就像数组中的长度一样。这种思想给jQuery方法提供了无限的想象空间。比如，我们可以从&#8217;Array.prototype&#8217;中借用一些功能性方法。jQuery中的slice方法就是一个典型的例子：</p>

<div class="wp_codebox"><table><tr id="p115841"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1158code41"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* ... jQuery.fn.extend({ ... */</span>
slice<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pushStack</span><span style="color: #009900;">&#40;</span>
        Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;slice&quot;</span><span style="color: #339933;">,</span>
        Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>wbr<span style="color: #339933;">&gt;</span>arguments<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009966; font-style: italic;">/* ... */</span>
<span style="color: #339933;">&lt;/</span>wbr<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>在该slice方法不管this是否是一个真实的数组，它和其他的元素会和睦地相处，因为有着”length“这么一个属性。
</p>
<p>jQuery中还有一些其他有趣的属性，比如”selector”&#8212;选择器，”context”&#8212;-上下文等。大部分的时候，只需给jQuery方法传递对象参数即可：</p>

<div class="wp_codebox"><table><tr id="p115842"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1158code42"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> jqObject <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jqObject.<span style="color: #660066;">selector</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; &quot;a&quot;</span></pre></td></tr></table></div>

<p>一个重要的提示是，jQuery有时会返回一个全新的对象供你使用。比如你试图通过执行一段代码来改变一个集合，如”parent()”方法，但是此时jQuery将不会修改当前的对象，它会返回给你一个全新的对象。</p>

<div class="wp_codebox"><table><tr id="p115843"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1158code43"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> originalObject <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> anotherObject <span style="color: #339933;">=</span> originalObject.<span style="color: #660066;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
originalObject <span style="color: #339933;">===</span> anotherObject<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; false</span></pre></td></tr></table></div>

</p>
<p>所有试图改变集合的方法都会返回一个新的jQuery对象，你可以通过”.end()”方法或”.prevObject”属性来访问之前的那个对象。</p>
<h3>2.面包、奶油元素的建立：</h3>
<p>jQuery创建DOM的能力，是其核心功能之一。jQuery1.4新版本带来了一个全新的便捷地创建DOM对象的方法，如：</p>

<div class="wp_codebox"><table><tr id="p115844"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p1158code44"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myDiv <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'my-new-element'</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'foo'</span><span style="color: #339933;">,</span>
    css<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span>
        backgrondColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#FFF'</span><span style="color: #339933;">,</span>
        border<span style="color: #339933;">:</span> <span style="color: #3366CC;">'1px solid #CCC'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Clicked!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    html<span style="color: #339933;">:</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        href<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#'</span><span style="color: #339933;">,</span>
        click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// do something</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>在1.4版本中，当你创建一个元素对象时，可以再传入第二参数，该第二参宿可以作为该元素对象的属性设置，也可以理解会调用了.attr()方法。当然，jQuery会自行匹配传入的参数们，如&#8217;click&#8217;对应了jQuery库中的.click方法，以及&#8217;css&#8217;、&#8217;html&#8217;分别对应了jQuery库中的.css()、.html()方法，等等&#8230;</p>
<h3>3.序列化表单中的input元素：</h3>
<p>jQuery提供了一个方法可以将1个或多个表单中的所有input元素进行序列化，这在Ajax提交数据时非常有用。其实该方法在jQuery库中已经存在很长的时间了，但一直很少谈及，所以很多的程序员都忽略了它。通过jQuery中的Ajax方法提交整个表单将变得非常简单。</p>

<div class="wp_codebox"><table><tr id="p115845"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1158code45"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myForm <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#my-form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submit.php'</span><span style="color: #339933;">,</span> myForm.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Data has been sent!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>jQuery同样也通过了一个&#8217;serializeArray&#8217;方法，他通常用在多个form表单存在，需要自行收集数据的情况。在jQuery的命名空间下有一个&#8217;param&#8217;辅助方法可以将一个数组对象处理后返回成一个查询字符串。</p>

<div class="wp_codebox"><table><tr id="p115846"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1158code46"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Joe'</span><span style="color: #339933;">,</span>
    age<span style="color: #339933;">:</span> <span style="color: #CC0000;">44</span><span style="color: #339933;">,</span>
    profession<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Web Developer'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
jQuery.<span style="color: #660066;">param</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; &quot;name=Joe&amp;age=44&amp;profession=&lt;wbr&gt;Web+Developer&quot;</span>
<span style="color: #339933;">&lt;/</span>wbr<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<h3>4.任何元素都可以动画方式展现：</h3>
<p>jQuery中的&#8217;animate&#8217;方法也许是最灵活的jQuery的方法。它可以用几乎任何东西来制作动画，而不仅仅是CSS属性，或者是DOM元素。通常会这样使用&#8217;动画&#8217;：</p>

<div class="wp_codebox"><table><tr id="p115847"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1158code47"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#box'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    left<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>当您给.animate()方法指定一个属性时（例如&#8217;top&#8217;），jQuery会检查你是否是想通过某个css属性来控制动画风格。首先jQuery会检查css样式中是否存在该top属性，若不存在，则jQuery会自行地给对应元素加上该属性。下面是一个例子：</p>

<div class="wp_codebox"><table><tr id="p115848"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1158code48"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#box'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    top<span style="color: #339933;">:</span> <span style="color: #CC0000;">123</span><span style="color: #339933;">,</span>
    foo<span style="color: #339933;">:</span> <span style="color: #CC0000;">456</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>上面的代码中&#8217;top&#8217;是一个可用的css属性，则jQuery就会改变&#8217;element.style.top&#8217;,但是&#8217;foo&#8217;不是一个可用的css属性，所以jQuery将只更新&#8217;element.foo&#8217;。</p>
<p>我们可以利用这个优点来实现一些有趣的效果。比方说，例如，您要在一个画布上使一个正方形动画起来。首先让我们定义一个简单的构造和一个在每一步都会调用的&#8217;drwa&#8217;的方法：</p>

<div class="wp_codebox"><table><tr id="p115849"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p1158code49"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Square<span style="color: #009900;">&#40;</span>cnvs<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> color<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> width<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> height<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> color<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cHeight</span> <span style="color: #339933;">=</span> cnvs.<span style="color: #660066;">height</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cWidth</span> <span style="color: #339933;">=</span> cnvs.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cntxt</span> <span style="color: #339933;">=</span> cnvs.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
Square.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">draw</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cntxt</span>.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cWidth</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cHeight</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cntxt</span>.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">color</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cntxt</span>.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>通过jQuery方法创建一个canvas画布元素（需要html5支持），再实例化一个正方形对象，通过调用.animate()方法在动画的每一步过程中调用.draw()方法，这样就可以轻易地使该正方形动画起来：</p>

<div class="wp_codebox"><table><tr id="p115850"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p1158code50"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Create a &lt;canvas /&gt; element</span>
<span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;canvas /&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #339933;">&lt;</span>wbr<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
canvas.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">;</span>
canvas.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">600</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Instantiate Square</span>
<span style="color: #003366; font-weight: bold;">var</span> square <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Square<span style="color: #009900;">&#40;</span>canvas<span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'rgb(255,0,0)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
jQuery<span style="color: #009900;">&#40;</span>square<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    x<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
    y<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 'draw' should be called on every step</span>
    <span style="color: #006600; font-style: italic;">// of the animation:</span>
    step<span style="color: #339933;">:</span> jQuery.<span style="color: #660066;">proxy</span><span style="color: #009900;">&#40;</span>square<span style="color: #339933;">,</span> <span style="color: #3366CC;">'draw'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>wbr<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p>这是一个非常简单的效果，但它清楚地表明了jQuery的无限可能性。你可以通过下面的链接看到具体的效果（需要浏览器支持html5）：<a href="http://jsbin.com/ocida" rel="extrnal">Demo</a></p>
<h3>5.jQuery.ajax方法返回XHR对象：</h3>
<p>jQuery的 Ajax方法是一个多功能的方法，它包含了&#8217;jQuery.ajax&#8217;, &#8216;jQuery.get&#8217;, &#8216;jQuery.post&#8217;等多个方法，这些方法在调用后都会返回一个&#8217;XMLHttpRequest&#8217; 对象，从而来实现请求完成后的后续操作。如：</p>

<div class="wp_codebox"><table><tr id="p115851"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p1158code51"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> curRequest<span style="color: #339933;">;</span>
&nbsp;
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.makeRequest'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    curRequest <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo.php'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Data: '</span> <span style="color: #339933;">+</span> response.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.cancelRequest'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>curRequest<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        curRequest.<span style="color: #660066;">abort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// abort() is a method of XMLHttpRequest</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>上面的代码中当我们点击&#8217;makeRequest&#8217;按钮时，Ajax请求将会被触发。而当我们点击&#8217;cancelRequest&#8217;按钮时，该请求则会被取消。</p>
<p>另一个潜在的用途是同步请求：</p>

<div class="wp_codebox"><table><tr id="p115852"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p1158code52"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myRequest <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'foo.txt'</span><span style="color: #339933;">,</span>
    async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>myRequest.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>请多的资料可以阅读<a href="https://developer.mozilla.org/en/XMLHttpRequest" rel="external">&#8216;XMLHttpRequest&#8217;</a>，也可以阅读Ajax的API文档<a href="http://api.jquery.com/category/ajax/" rel="external">jQuery&#8217;s Ajax utilities</a></p>
<h3>6.自定义队列：</h3>
<p>jQuery的有一个内置的队列机制，一般都是有.animate等动画方法使用的（如：.fadeOut()、.fadeIn()等，实质都是有.animate()方法实现）。这种队列机制可以用一个简单的动画就可以说明：</p>

<div class="wp_codebox"><table><tr id="p115853"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1158code53"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>paddingLeft<span style="color: #339933;">:</span><span style="color: #3366CC;">'+=15px'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>paddingLeft<span style="color: #339933;">:</span><span style="color: #3366CC;">'-=15px'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>快速地在一个链接标记上鼠标移入、移出，当再次移入时，会导致动画的效果反复执行。不信你可以到这里试试：<a href="http://jsbin.com/aqaku" rel="external">Demo</a></p>
<p>&#8216;queue&#8217;方法和大家熟知的&#8217;each&#8217;方法比较相似，你通过给方法传递一个函数参数，则该函数将被匹配集合中的每个元素所调用：</p>

<div class="wp_codebox"><table><tr id="p115854"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1158code54"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'all-done'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>给&#8217;queue&#8217;方法只传入一个函数参数的话会导致该函数被添加到默认的&#8217;fx&#8217;队列中。若刚好该队列会被jQuery中的所有动画对象所调用，那么只有等前面的这些对象都调用后，才会轮到自己。<br />
鉴于该点，在上面的代码中我给对象添加了一个名为&#8217;all-done&#8217;的class，根据协议，这个class只有在当前所有的动画都执行完成后才会添加。在代码中我们也调用了.dequeue()方法。<strongstyle ="color:#f00">这一点很重要，它将使jQuery的继续进行队列。jQuery1.4版本提供了另一个可以继续队列的方法nextItemInQueue()，它可以用来取代调用.dequeue()方法。它的调用只需将第一个参数传递给方法即可：</p>

<div class="wp_codebox"><table><tr id="p115855"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1158code55"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>wbr<span style="color: #339933;">&gt;</span>nextItemInQueue<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Continue queue:</span>
    nextItemInQueue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>wbr<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>当然两个方法也不完全相同，nextItemInQueue()可以在方法体内部的任何地方都可以调用，即使在一团糟的匿名函数内（典型情况是&#8217;this&#8217;对象被破坏）。当然在1.4版本前，你可以尽可能地保存一个对&#8217;this&#8217;对象的引用，但这样使用起来有点累。</strongstyle></p>
<p>要想向自定义队列中添加方法，只需将你自定义的队列名称作为第一参数，而将方法体作为第二参数传入即可：</p>

<div class="wp_codebox"><table><tr id="p115856"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1158code56"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'customQueueName'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do stuff</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'customQueueName'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>需要认识的一点，为了不将该队列添加到默认的&#8217;fx&#8217;队列中，我们需要将自定义的队列名传入.dequeue()方法中，这样在队列结束后，可以保证jQuery队列可以继续执行下去。</p>
<p>更多的资料可以阅读<a href="http://api.jquery.com/queue" rel="external" class="p_5">&#8216;queue&#8217;</a>,<a href="http://api.jquery.com/dequeue/" rel="external" class="p_5">&#8216;dequeue&#8217;</a>,<a href="http://api.jquery.com/jQuery.queue/" rel="external" class="p_5">&#8216;jQuery.queue&#8217;</a></p>
<h3>7.事件命名空间：</h3>
<p>jQuery提供了一个命名时间名称的方法，这在编写插件或第三方组件时非常有用。当需要时，您的插件用户可以通过取消绑定这些你注册事件名称即可。<br />
<br />向命名空间中添加注册一个事件处理名称，只需在事件名称后添加自定义的后缀名（如：.fooPlugin):</p>

<div class="wp_codebox"><table><tr id="p115857"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p1158code57"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">foo</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.fooPlugin'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// do stuff</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover.fooPlugin'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// do stuff</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Use the plugin:</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Destroy its event handlers:</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.fooPlugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>取消绑定是只需调用.unbind()方法，并传入自定义事件名称即可实现。</p>
<h3>总结：</h3>
<p>jQuery中的隐藏功能远远不止这些，在以后的文章中会再总结一些我自己发现的功能与大家分享。</p>
<p>本文翻译自：<a href="http://net.tutsplus.com/tutorials/javascript-ajax/uncovering-jquerys-hidden-features/" rel="external">Uncovering jQuery’s Hidden Features</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li><li>2009年11月30日 -- <a href="http://www.ihiro.org/coding-clean-and-rich-semantic-html" title="HTML:编写干净的且富有语义的html代码">HTML:编写干净的且富有语义的html代码</a> (13)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2009年08月6日 -- <a href="http://www.ihiro.org/drop-down-menu" title="jQuery:两个样式下拉菜单(click hover)">jQuery:两个样式下拉菜单(click hover)</a> (8)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/uncovering-jquerys-hidden-features/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>CSS:CSS3基础（附图）</title>
		<link>http://www.ihiro.org/css3-basic</link>
		<comments>http://www.ihiro.org/css3-basic#comments</comments>
		<pubDate>Mon, 08 Mar 2010 02:41:14 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[CSS、XHTML]]></category>
		<category><![CDATA[前端工程译文]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[译文]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/css-css3-based-with-photos</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-basics.gif" alt="" title="css3-basics" width="100" height="100" class="alignleft size-full wp-image-1138" />
<p>今日在<a href="http://www.webdesignerwall.com/" rel="external">Web Designer Wall</a>博客上看到这篇介绍css3基础的博文，文章其实很普通，关键喜欢他配上了截图，看起来就更加明了。便花了点时间整理后发表出来与大家分享一下！</p>
<p>文中主要介绍了四个css3高级属性，也是最最常用的四个css3属性。</p>
<p>最后，css3高级属性远远不值文中的这些，文中讲到的是一些基础的，也是使用最多的，为了给用户带去更多的美好体验，努力学透css吧！</p>
<p>本文翻译自：<a href="http://www.webdesignerwall.com/tutorials/the-basics-of-css3/" rel="external">Web Designer Wall -- The Basics of CSS3</a></p>]]></description>
			<content:encoded><![CDATA[<p>今日在<a href="http://www.webdesignerwall.com/" rel="external">Web Designer Wall</a>博客上看到这篇介绍css3基础的博文，文章其实很普通，关键喜欢他配上了截图，看起来就更加明了。便花了点时间整理后发表出来与大家分享一下！</p>
<p>文中主要介绍了四个css3高级属性，也是最最常用的四个css3属性。</p>
<h3>1.RGBA</h3>
<p>RBGA是用于css的background属性，前三个参数是RBG颜色值，最后一个参数是透明度设置。<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-rgba.gif" alt="" title="css3-rgba" width="470" height="276" class="aligncenter size-full wp-image-1143" />RGBA也可以使用在css中与颜色相关的所有属性中，如color,border-color,background-color,shadow-color等。<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-rgba2.gif" alt="" title="css3-rgba2" width="470" height="303" class="aligncenter size-full wp-image-1144" /></p>
<h3>2.Text Shadow</h3>
<p>text-shadow的设置值是由x-offset, y-offset, blur, color四个参数组成。<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-text-shadow.gif" alt="" title="css3-text-shadow" width="470" height="276" class="aligncenter size-full wp-image-1145" />若给x-offset设置一个负值，则阴影偏向左边；若个y-offset设置一个负值，则阴影偏向顶部；当然你也可以使用RGBA来设置第四个参数color。<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-text-shadow-example2.gif" alt="" title="css3-text-shadow-example2" width="470" height="114" class="aligncenter size-full wp-image-1146" />你也可以给text-shadow设置一系列的值（用逗号隔开）。下面的图片展示了两个值来设置一个单词的印刷效果（1px向上，1px向下）：<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-text-shadow-example3.gif" alt="" title="css3-text-shadow-example3" width="470" height="114" class="aligncenter size-full wp-image-1147" /></p>
<h3>3.Border Radius</h3>
<p>对border radius的记忆方法就和padding、margin一样(如： border-radius: 20px)。为了达到多浏览器兼容，需要在该属性前加上“-moz-“用于火狐浏览器、加上”-webkit-“用于谷歌和苹果浏览器。（IE浏览器不支持此CSS3高级属性）<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-border-radius.gif" alt="" title="css3-border-radius" width="470" height="340" class="aligncenter size-full wp-image-1139" />你也可以分别设置四个角的圆角直径，火狐浏览器与苹果（谷歌)浏览器的设置属性有所差别，看图：<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-border-radius-corners.gif" alt="" title="css3-border-radius-corners" width="470" height="460" class="aligncenter size-full wp-image-1140" /></p>
<h3>4.Box Shadow</h3>
<p>box shadow属性设置值的构成与text-shadow是一样的，都是有四个属性：x-offset, y-offset, blur, color组成。<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-box-shadow.gif" alt="" title="css3-box-shadow" width="470" height="353" class="aligncenter size-full wp-image-1141" />当然，box-shadow也可以用一系列的值来设置属性，如：</p>

<div class="wp_codebox"><table><tr id="p114858"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1148code58"><pre class="css" style="font-family:monospace;">-moz-box-shadow<span style="color: #00AA00;">:</span>
		<span style="color: #933;">-2px</span> <span style="color: #933;">-2px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">,</span>
		<span style="color: #933;">2px</span> <span style="color: #933;">2px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc00cc;">#bb9595</span><span style="color: #00AA00;">,</span>
		<span style="color: #933;">2px</span> <span style="color: #933;">4px</span> <span style="color: #933;">15px</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> .3<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<p>效果如下图：<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/03/css3-box-shadow2.gif" alt="" title="css3-box-shadow2" width="470" height="278" class="aligncenter size-full wp-image-1142" /></p>
<p>最后，css3高级属性远远不值文中的这些，文中讲到的是一些基础的，也是使用最多的，为了给用户带去更多的美好体验，努力学透css吧！</p>
<p>本文翻译自:<a href="http://www.webdesignerwall.com/tutorials/the-basics-of-css3/" rel="external">Web Designer Wall &#8212; The Basics of CSS3</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年03月2日 -- <a href="http://www.ihiro.org/css-specific-for-internet-explorer" title="CSS:区分IE版本的三个方法">CSS:区分IE版本的三个方法</a> (27)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年11月30日 -- <a href="http://www.ihiro.org/coding-clean-and-rich-semantic-html" title="HTML:编写干净的且富有语义的html代码">HTML:编写干净的且富有语义的html代码</a> (13)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/css3-basic/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>jQuery:1.4新版本中你应该知道的15个新特性(二)</title>
		<link>http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2</link>
		<comments>http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:04:47 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[前端工程译文]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[译文]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1054</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/01/jquery-1.4-logo-2.gif" alt="" title="jquery-1.4-logo-2" width="100" height="100" class="alignleft size-full wp-image-1044" />
<p>继续发布jQuery1.4版本剩余的新特性：第9点至第15点。</p>
<h3>九、去除标签元素的父标签；</h3>
<h3>十、不用删除数据，即可删除掉DOM中的标签元素；</h3>
<h3>十一、对.index()方法进行了扩展；</h3>
<h3>十二、DOM控制方法支持回调函数...</h3>]]></description>
			<content:encoded><![CDATA[<p>继续发布jQuery1.4版本剩余的新特性：第9点至第15点。</p>
<h3>九、去除标签元素的父标签：</h3>
<p>在之前的版本中已经有了.wrap()方法来个标签添加新的父标签，而在1.4新版本中又添加了相对的.unwrap()方法来去除标签的父标签。实例Html代码如下：</p>

<div class="wp_codebox"><table><tr id="p105459"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1054code59"><pre class="html" style="font-family:monospace;">&lt;div&gt;
    &lt;p&gt;Foo&lt;/p&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105460"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1054code60"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unwrap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>执行后的话题Html代码如下:</p>

<div class="wp_codebox"><table><tr id="p105461"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1054code61"><pre class="html" style="font-family:monospace;">&lt;p&gt;Foo&lt;/p&gt;</pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/unwrap/" class="p_5" rel="external">.unwrap()</a>方法的资料&#8230;</p>
<h3>十、不用删除数据，即可删除掉DOM中的标签元素：</h3>
<p>新版本中的.detach()方法允许你删除DOM中的任何标签元素，就像.remove()方法一样。但它与.remove()方法的不同之处是：.remove()是将标签直接删除，而.detach()方法则是将标签暂时移除，并通过方法内部调用.data()方法将标签缓存后赋予变量，再后面的代码中若还需要找回该标签，变量则会自动读取缓存的数据；另外若被.detach()方法移除的标签在移除前绑定了事件机制的话，在后面恢复后，该事件机制依然存在，无需再次绑定。<br />
<small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105462"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1054code62"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Bind an important event handler</span>
foo.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Foo!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
foo.<span style="color: #660066;">detach</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Remove it from the DOM</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// … do stuff</span>
&nbsp;
foo.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Add it back to the DOM</span>
&nbsp;
foo.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts &quot;Foo!&quot;</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/detach" class="p_5" rel="external">.detach()</a>方法的资料&#8230;</p>
<h3>十一、对.index()方法进行了扩展：</h3>
<p>1.4新版本中新增了两个.index()的调用方法。而之前，我们将一个标签元素作为.index()方法的参数，并希望执行后返回一个标识当前集合中匹配元素序列号的数字结果。<br />
<br /><small class="red">若有一段XHtml代码如下：</small></p>

<div class="wp_codebox"><table><tr id="p105463"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1054code63"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Apple<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Banana<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Grape<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Strawberry<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Pear<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Peach<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>点击li标签获得该li标签的序号<br />
<br /><small class="red">jQuery1.32版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105464"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1054code64"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105465"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1054code65"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//或者</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				   
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/index" class="p_5" rel="external">.index()</a>方法的资料&#8230;</p>
<h3>十二、DOM控制方法支持回调函数：</h3>
<p>在1.4版本中绝大部分的DOM控制方法只支持回调函数作为唯一的参数，除了极个别的（比如：.attr()、.css()）方法中可以作为第二参数。这些回调函数将被匹配集合中所有元素所执行，<br />
以确定哪些应作为该方法的实际值。<br />
<br />下面的这些DOM控制方法都支持回调函数：</p>
<ul class="external" style="padding-left:30px; list-style:inside disc;">
<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>在回调方法中，若你想访问该匹配集合的话，你需要使用”this”，或者序号index作为该方法的第一个参数。<br />
<small class="red">jQuery1.4版本中实现代码：</small><br />
</p>

<div class="wp_codebox"><table><tr id="p105466"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1054code66"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'Index of this list item: '</span> <span style="color: #339933;">+</span> i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>当然你可以给该回调函数传递第二个参数，比如是在使用设置类的DOM控制方法（如：.html()、.attr()等），你若想得到当前的值。<br />
<br /><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105467"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1054code67"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> currentHref<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> currentHref <span style="color: #339933;">+</span> <span style="color: #3366CC;">'?foo=bar'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>正如你看到的一样，在使用.attr()和.css()方法是，当该方法的第一个是属性名称时，你可以将回调函数作为方法的第二参数。<br />
<br /><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105468"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1054code68"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> currentCssColor<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> i <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'red'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'blue'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>十三、检测对象类型方法：</h3>
<p>jQuery1.4新版本在命名空间下新增了两个帮助型的方法，用来帮助你检测对象类型。</p>
<p>第一个方法：.isEmptyObject()用来判断一个对象是否是空对象，返回一个布尔类型的结果；<br />
第二个方法：.isPlainObject()用来判断一个对象是否是最原始的Javascript对象(该对象是否是通过{}或者new Object()创建)，同样也返回一个布尔类型的结果。<br />
<br /><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105469"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p1054code69"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">isEmptyObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
jQuery.<span style="color: #660066;">isEmptyObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>foo<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
&nbsp;
jQuery.<span style="color: #660066;">isPlainObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
jQuery.<span style="color: #660066;">isPlainObject</span><span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
jQuery.<span style="color: #660066;">isPlainObject</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/jQuery.isEmptyObject" class="p_l_5" rel="external">.isEmptyObject()</a><a href="http://api.jquery.com/jQuery.isPlainObject" class="p_5" rel="external">.isPlainObject()</a>方法的资料&#8230;</p>
<h3>十四、.Closest()方法扩展：</h3>
<p>扩展后的.Closest()支持数组选择器作为参数，这在遍历一个元素的祖先元素时非常有用。</p>

<div class="wp_codebox"><table><tr id="p105470"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1054code70"><pre class="html" style="font-family:monospace;">&lt; !DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;&lt;/style&gt;
  &lt;script src=&quot;/scripts/jquery-1.4.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;ul&gt;&lt;li&gt;&lt;/li&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;script&gt;var close = $(&quot;li:first&quot;).closest([&quot;ul&quot;, &quot;body&quot;]);
$.each(close, function(i){
  $(&quot;li&quot;).eq(i).html( this.selector + &quot;: &quot; + this.elem.nodeName );
});&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>执行后的结果是：</p>

<div class="wp_codebox"><table><tr id="p105471"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1054code71"><pre class="html" style="font-family:monospace;">    * ul: UL
    * body: BODY</pre></td></tr></table></div>

</p>
<p>另外该方法还支持使用上下文对象作为它的第二参数，也就是说你可以遍历任意上下文对象直到的元素的祖先元素。这个扩展使用的列子非常少，但在内部使用时非常的有效率。</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/closest" class="p_5" rel="external">.closest()</a>方法的资料&#8230;</p>
<h3>十四、新的事件机制（focusIn 、focusOut）：</h3>
<p>新版本中若要给事件对象委派”focus”和”blur”事件，也可以使用.focusIn()和.focusOut()方法（它们是一一对应的）。更加重要的是focus和blur事件不支持.live()方法委派，但focusIn和focusOut事件是支持.live()方法委派。<br />
<br /><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p105472"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p1054code72"><pre class="javascript" style="font-family:monospace;">   jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">focusin</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focused'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    .<span style="color: #660066;">focusout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focused'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p105473"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p1054code73"><pre class="javascript" style="font-family:monospace;">   jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focusin'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focused'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    .<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focusout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focused'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/focusin" class="p_l_5" rel="external">.focusin()</a><a href="http://api.jquery.com/focusout" class="p_5" rel="external">.focusin()</a>方法的资料&#8230;</p>
<p>文章总结翻译来自：<a href="http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/" rel="external">http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/</a></p>
<p>更多的API文档资料请阅读：<a href="http://api.jquery.com/" rel="external">http://api.jquery.com/</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li><li>2009年11月30日 -- <a href="http://www.ihiro.org/coding-clean-and-rich-semantic-html" title="HTML:编写干净的且富有语义的html代码">HTML:编写干净的且富有语义的html代码</a> (13)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2009年08月6日 -- <a href="http://www.ihiro.org/drop-down-menu" title="jQuery:两个样式下拉菜单(click hover)">jQuery:两个样式下拉菜单(click hover)</a> (8)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>jQuery:1.4新版本中你应该知道的15个新特性(一)</title>
		<link>http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1</link>
		<comments>http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1#comments</comments>
		<pubDate>Wed, 20 Jan 2010 05:53:13 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[前端工程译文]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[译文]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=1028</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2010/01/jquery-1.4-logo-1.gif" alt="" title="jquery-1.4-logo-1" width="100" height="100" class="alignleft size-full wp-image-1043" />
<p>题外话：前两天回了一趟泰州，星期二上班后看到了两条比较重大的新闻：1.谷歌耍了我们这些谷歌迷们一把，决定不离开中国了；2.jQuery1.4新版本也正式上线了。</p>
<p>从<a href="http://net.tutsplus.com/" rel="external">Nettuts+</a>博客上看到了这票jQuery1.4新特性的总结性文章，该博总结了15条新的特性，便给出相应的实现代码。觉得不错，便整理后翻译过来供大家参考，并加入了1.32版本的实现方法进行比较性地学习。</p>
<p class="red">文章较长，分两篇文章发布，请关心者耐心阅读与等待！上篇包含特性1-8点，下篇9-15点隔一日再发布！</p>]]></description>
			<content:encoded><![CDATA[<p>题外话：前两天回了一趟泰州，星期二上班后看到了两条比较重大的新闻：1.谷歌耍了我们这些谷歌迷们一把，决定不离开中国了；2.jQuery1.4新版本也正式上线了。</p>
<p>从<a href="http://net.tutsplus.com/" rel="external">Nettuts+</a>博客上看到了这票jQuery1.4新特性的总结性文章，该博总结了15条新的特性，便给出相应的实现代码。觉得不错，便整理后翻译过来供大家参考，并加入了1.32版本的实现方法进行比较性地学习。</p>
<p class="red">文章较长，分两篇文章发布，请关心者耐心阅读与等待！上篇包含特性1-8点，下篇9-15点隔一日再发布！</p>
<h3>一、给jQuery(…)方法传递新的参数：</h3>
<p>jQuery()方法是jQuery库的核心方法之一，在1.32版本及早期的版本中，该方法是用来获取节点或者创建DOM对象的，它只支持一个参数。在使用来创建新的DOM对象时，我们若要给该DOM对象添加属性的话，只能通过attr()方法在后面追加，很是不方便，阅读起来也比较累。在jQuery1.4中，给jQuery方法新增了一个对象型的参数，用来创建新的DOM对象设置该DOM对象相应的属性。<br />
<br /><small class="red">jQuery1.32版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102874"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p1028code74"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a&gt;&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'foo'</span><span style="color: #339933;">,</span>
    href<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://google.com'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Become a Googler'</span><span style="color: #339933;">,</span>
    rel<span style="color: #339933;">:</span> <span style="color: #3366CC;">'external'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Go to Google!'</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
       fontWeight<span style="color: #339933;">:</span> <span style="color: #CC0000;">700</span><span style="color: #339933;">,</span>
       color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'green'</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Foo has been clicked!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102875"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1028code75"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'foo'</span><span style="color: #339933;">,</span>
    href<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://google.com'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Become a Googler'</span><span style="color: #339933;">,</span>
    rel<span style="color: #339933;">:</span> <span style="color: #3366CC;">'external'</span><span style="color: #339933;">,</span>
    text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Go to Google!'</span><span style="color: #339933;">,</span>
    css<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        fontWeight<span style="color: #339933;">:</span> <span style="color: #CC0000;">700</span><span style="color: #339933;">,</span>
        color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'green'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Foo has been clicked!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/jQuery/" class="p_5" rel="external">jQuery(…)</a>方法的资料&#8230;</p>
<h3>二、新增”until”系列方法：</h3>
<p>新增”until”系列方法中有三个方法：”nextUntil”, “prevUntil” 和”parentsUntil”，这些方法是用来获取符合条件内的DOM对象。中文你可以理解为“直到”的意思。<br />
<br /><small class="red">若有一段XHtml代码如下：</small></p>

<div class="wp_codebox"><table><tr id="p102876"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1028code76"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Apple<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Banana<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Grape<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Strawberry<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Pear<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Peach<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>现在我想选取从含有apple的li到含有pear的li,则代码实现：<br />
<br /><small class="red">jQuery1.32版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102877"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1028code77"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102878"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1028code78"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul li:contains(Apple)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">nextUntil</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">':contains(Pear)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>虽然上面两者的选取效果是一样的，但若现在我在含有apple的li到含有pear的li中间再添加内容的话，1.32版本的代码就需要修改了，而1.4版本的则可继续使用，也就说1.4版本的代码可用性和语义性增强了。</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/prevUntil/" class="p_l_5" rel="external">prevUntil</a>, <a href="http://api.jquery.com/nextUntil/" class="p_l_5" rel="external">nextUntil</a>, <a href="http://api.jquery.com/parentsUntil/" class="p_5" rel="external">parentsUntil</a>方法的资料&#8230;</p>
<h3>三、给事件对象一次绑定多个事件：</h3>
<p>1.4不再像1.32版本中那样链式地给一个时间对象绑定时间，现在你可以将多个时间归纳后绑定到一个方法中。<br />
<br /><small class="red">jQuery1.32版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102879"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p1028code79"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo).bind('</span>click<span style="color: #3366CC;">', function() {
        // do something
}).bind('</span>mouseover<span style="color: #3366CC;">', function() {
        // do something
}).bind('</span>mouseout‘， <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102880"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p1028code80"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo).bind({
    click: function() {
        // do something
    },
    mouseover: function() {
        // do something
    },
    mouseout: function() {
        // do something
    }
});</span></pre></td></tr></table></div>

<p>当然对.one()一次点击事件也是适用的哦！</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/bind/" class="p_5" rel="external">.bind(…)</a>方法的资料&#8230;</p>
<h3>四、Per-Property Easing动画效果</h3>
<p>在1.32版本中，我们使用animate()方法给对象添加动画效果时，特效很单一，只能再引入jquery.easing.js插件来丰富动画特效。在新版本1.4中将该插件集成到了jQuery库中，这样在使用过的过程中就更加地方便了。<br />
<br /><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102881"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1028code81"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    left<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'easeOutBounce'</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>当然也可以给附加的属性参数内添加动画效果，如：</p>

<div class="wp_codebox"><table><tr id="p102882"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1028code82"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    left<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">,</span>
    specialEasing<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        top<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutBounce'</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/animate/#per-property-easing" class="p_5" rel="external">per-property easing</a>的资料&#8230;</p>
<h3>五、新增live事件：</h3>
<p>在1.32版本中，live()方法只对click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup这些时间提供了支持，而不支持也比较常用的focus和blur时间。在1.4版本中新增了submit, change, focus, blur四种时间的支持。其中focus对应的是focusin事件，而blur对应了focusout事件。<br />
<br /><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102883"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1028code83"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focusin'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something with this</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focusout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something with this</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h3>六、控制方法的上下文对象：</h3>
<p>1.4版本在jQuery命名空间下新增了一个名叫”proxy“的方法，该方法有两个参数：一个是作用域，一个是准备执行的处理方法。在Javascript中，this关键字很巧妙地自动执行父亲对象或某个DOM元素对象。当有时候我们希望this指向的不是一个DOM元素对象，而是希望它指向一个之前创建的对象。</p>
<p>举个列子，就很容易明白！比如，现在我们有一个app对象，该对象内部有一个”clickHandler“方法和一个命名为”config“的对象。代码如下：</p>

<div class="wp_codebox"><table><tr id="p102884"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1028code84"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> app <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    config<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        clickMessage<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Hi!'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    clickHandler<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">config</span>.<span style="color: #660066;">clickMessage</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>当我们调用app.clickHandler()方法时，app对象就有了自己的上下文对象：clickHandler方法中的this指向的是app对象。执行后会弹出内容为”Hi!“的提示框。若现在我希望将该app对象下的clickHandler方法绑定在一个超链接上，如下：</p>

<div class="wp_codebox"><table><tr id="p102885"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1028code85"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> app.<span style="color: #660066;">clickHandler</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>而此时，当我们点击该超链接时，app对象下的clickHandler方法并不会执行，用Firebug测试则会提示”app.clickHandler is not a function!”。原因在于jQuery（以及绝大部分的事件对象）默认地将事件对象作为了该clickHandler方法的上下文对象，也就是此时this指向了a超链接标签元素。而我们的实际愿望是希望this执行app对象，那么1.4中我们就可以使用新增的proxy()方法来处理这一问题。代码如下：</p>

<div class="wp_codebox"><table><tr id="p102886"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1028code86"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>
    <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span>
    jQuery.<span style="color: #660066;">proxy</span><span style="color: #009900;">&#40;</span>app<span style="color: #339933;">,</span> <span style="color: #3366CC;">'clickHandler'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>这样执行后提示框就会正常地弹出了。proxy()方法返回的是一个包装后的方法，它将执行你所期望的对象（通过第一个参数传递）。这在其他上下文对象中也很实用，比如给其他方法或插件传递回调函数。</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/jQuery.proxy" class="p_5" rel="external">jQuery.proxy()</a>方法的资料&#8230;</p>
<h3>七、延迟动画队列：</h3>
<p>在平时编写代码时，你或许发现有一个动画效果还没完成呢，下面的代码就已经执行完了。而我们希望的是等动画效果执行完成后或执行一段时间后再执行后面的代码。1.4版本中新增了delay()方法来延迟代码的执行。<br />
<br /><small class="red">jQuery1.32版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102887"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1028code87"><pre class="javascript" style="font-family:monospace;">setTimeout<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><small class="red">jQuery1.4版本中实现代码：</small></p>

<div class="wp_codebox"><table><tr id="p102888"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1028code88"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// Slide down</span>
    .<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// Do nothing for 200 ms</span>
    .<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Fade in</span></pre></td></tr></table></div>

<p>若你希望延迟一个动画队列比默认的“fx”(queue()函数的默认参数)还要慢的话，你只需要将这个queue的变量名当作delay()方法的第二个参数传递即可。</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/delay" class="p_5" rel="external">.delay(&#8230;)</a>方法的资料&#8230;</p>
<h3>八、判断一个DOM父对象中是否含有某个匹配的子对象：</h3>
<p>1.4中新增了一个过滤型的方法has()来判断某个对象是否在某个标签或集合内出现过。该方法将返回在标签中出现的所有匹配子对象的集合，至少会包含一个子对象。若没有找到匹配子对象，则返回的是一个空的集合。</p>

<div class="wp_codebox"><table><tr id="p102889"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1028code89"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">has</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>当然jQuery1.32版本中的.contains()方法其实也可以达到同样的效果，1.4版本又对.contains()进行了完善。在1.4版本中通过给.contains()方法传递两个DOM对象参数，从而判断第二个参数对象是否在第一个参数对象中。代码如下：</p>

<div class="wp_codebox"><table><tr id="p102890"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1028code90"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">documentElement</span><span style="color: #339933;">,</span> document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Returns true - &lt;body&gt; is within &lt;html&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;&lt;/</span>body<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p>&gt;&gt;&gt;&gt;阅读更多关于<a href="http://api.jquery.com/has/" class="p_l_5" rel="external">.has(…)</a>，<a href="http://api.jquery.com/jQuery.contains/" class="p_5" rel="external">jQuery.contains(…)</a>方法的资料&#8230;</p>
<p style="color:#090;">剩余的7点新特性隔一日发布！敬请期待吧，呵呵。</p>
<p>文章总结翻译来自：<a href="http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/" rel="external">http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li><li>2009年11月30日 -- <a href="http://www.ihiro.org/coding-clean-and-rich-semantic-html" title="HTML:编写干净的且富有语义的html代码">HTML:编写干净的且富有语义的html代码</a> (13)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2009年08月6日 -- <a href="http://www.ihiro.org/drop-down-menu" title="jQuery:两个样式下拉菜单(click hover)">jQuery:两个样式下拉菜单(click hover)</a> (8)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Javascript:10个Javscript编程的技巧</title>
		<link>http://www.ihiro.org/10-javascript-tips</link>
		<comments>http://www.ihiro.org/10-javascript-tips#comments</comments>
		<pubDate>Fri, 18 Dec 2009 09:45:50 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[Pure Javascript]]></category>
		<category><![CDATA[前端工程译文]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[译文]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=941</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/12/javascript-tips.png" alt="javascript-tips" title="javascript-tips" width="100" height="100" class="alignleft size-full wp-image-951" />
<p>今天在<a href="http://www.impressivewebs.com/" rel="external">Blog：Impressive Webs</a>上看到这篇总结了10个Javascript技巧的文章。觉得比较受用，便简约地理解后翻译了给需要的朋友学习学习。</p>
<p>文章中的10条小技巧或许对大家来说已经是了然于胸了，但有时候编程晕头晕脑的，不经意间就会忘记。所以在平时的编程过程中养成一个良好的编程习惯是一个程序员最基础的素质要求。只要多练、多看、多学习，我们的编程技术就会越来越高明的！</p>
<p>该文整理来自：<a href="http://www.impressivewebs.com/10-javascript-quick-tips-and-best-practices/" rel="external">Impressive Webs:10 JavaScript Quick Tips and Best Practices</a></p>]]></description>
			<content:encoded><![CDATA[<p>今天在<a href="http://www.impressivewebs.com/" rel="external">Blog：Impressive Webs</a>上看到这篇总结了10个Javascript技巧的文章。觉得比较受用，便简约地理解后翻译了给需要的朋友学习学习。</p>
<h3>1.外部引入js文件时，使用defer属性供IE浏览器解析</h3>
<p>这个属性可加可不加，不管添加defer属性是不是一个好的编程技巧，但该属性在适当的时候会派上用场的。它的使用方法如下：</p>

<div class="wp_codebox"><table><tr id="p94191"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p941code91"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> defer<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;defer&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>添加defer属性的目的是在引入外部js文件时告诉Ie浏览器，只有在等页面加载完成后才执行引入的js文件中的js代码。当然这也可以通过一些比较好的代码实现，这些好的代码可以避免页面在没有加载完DOM就执行外部引入的js代码的问题。</p>
<p>注：defer属性只有IE浏览器才会支持。</p>
<h3>2.使用一个CDATA区域来避免XHTML Strict文件验证的错误</h3>
<p>XHTML Strict文件对页面中的标签和代码要求甚高，而js代码中常常出现运算符，而W3C验证XHTML Strict文件时，会认为页面js代码中的”< "、">“等运算符为一个标签的开始和结束，这样就会出现验证错误。</p>
<p>所以比较好的XHTML Strict文件中js编程习惯时给js代码外围加上一个CDATA区域，以达到该段js可以通过W3C验证。效果如下：</p>

<div class="wp_codebox"><table><tr id="p94192"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p941code92"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #006600; font-style: italic;">//&lt; ![CDATA[</span>
<span style="color: #003366; font-weight: bold;">var</span> my_variable <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>my_variable <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do something here...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//]]&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<h3>3.避免使用Javascript的关键字来自定义变量</h3>
<p>每一种编程语言都有默认的一些关键字，在日常的代码编写过程中都因避免使用关键字作为自定义变量名。Javascript中的关键字有:</p>

<div class="wp_codebox"><table><tr id="p94193"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p941code93"><pre class="html" style="font-family:monospace;">break
case
catch
continue
default
delete
do
else
finally
for
function
if
in
instanceof
new
return
switch
this
throw
try
typeof
var
void
while
with</pre></td></tr></table></div>

</p>
<h3>4.避免使用Javascript的保留字来自定义变量</h3>
<p>同样每一种编程语言都有一定量的保留字，在自定义变量时也因避免使用这些保留字作为自定义变量名。Javascript的保留字有:</p>

<div class="wp_codebox"><table><tr id="p94194"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p941code94"><pre class="html" style="font-family:monospace;">abstract
boolean
byte
char
class
const
debugger
double
enum
export
extends
final
float
goto
implements
import
int
interface
long
native
package
private
protected
public
short
static
super
synchronized
throws
transient
volatile</pre></td></tr></table></div>

</p>
<h3>5.在自定义一个变量并赋予相应的类型后，在之后的代码中避免改变该变量的类型</h3>
<p>在Javascript编程中，如下的写法是不太理想的：</p>

<div class="wp_codebox"><table><tr id="p94195"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p941code95"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> my_variable <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;This is a String&quot;</span><span style="color: #339933;">;</span>
my_variable <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>在第一行给my_variable变量赋予了一个String的类型，而在第二行却改变为了Integer类型，这样写是一个不好的习惯，应该避免这种情况的发生。
</p>
<h3>6.不要使用全局变量</h3>
<p>
为了防止可能发生的冲突，99％的情况下，使用var关键字最初声明变量和赋予该变量值。这将确保您的变量是局部的，而不是声明它们的功能函数之外。这样的话，在两个不同的函数体内使用相同的变量名就不会发生变量的冲突。而这里在函数体内定义的变量在函数执行完成后就会被取消，从而节省了命名空间。
</p>
<h3>7.Javascript对大小写敏感</h3>
<p>看下面的两个自定义变量，它们代表了两个不同的变量：</p>

<div class="wp_codebox"><table><tr id="p94196"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p941code96"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myVariable <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;data&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> myvariable <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;more data&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>所以，在日常的编写代码时，养成一个好的编程习惯，不要无意间因大小写的问题而定义了两个变量，其实你需要的只是一个变量。
</p>
<h3>8.使用switch声明来处理多种条件判断</h3>
<p>在多个条件判断代码的编写时，不要如下编写代码：</p>

<div class="wp_codebox"><table><tr id="p94197"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p941code97"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>example_variable <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;cyan&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do something here...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>example_variable <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;magenta&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do something here...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>example_variable <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;yellow&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do something here...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>example_variable <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do something here...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do something here...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>而是应该这样使用：</p>

<div class="wp_codebox"><table><tr id="p94198"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p941code98"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>example_variable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;cyan&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #006600; font-style: italic;">// do something here...</span>
		<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;magenta&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #006600; font-style: italic;">// do something here...</span>
		<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;yellow&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #006600; font-style: italic;">// do something here...</span>
		<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #006600; font-style: italic;">// do something here...</span>
		<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
		<span style="color: #006600; font-style: italic;">// do something here...</span>
		<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>上面的两端代码执行的结果是一样的，但第二种使用swicth编写的代码便于阅读、管理和修改。
</p>
<h3>9.使用try-catch，以防止向用户显示错误</h3>
<p>将需要执行的代码包含在try-catch中，这样用户就永远看不多Javascript的报错信息。如下：</p>

<div class="wp_codebox"><table><tr id="p94199"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p941code99"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
	nonExistentFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;An error has occured.&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>在上面的代码中我试图去调用一个实际不存在的函数来产生一个错误。而浏览器则不会显示“not an object” 或者 “object expected”错误，而是现实了我在catch体内自定义的错误信息。当然你也可以在catch体内不懈任何代码，这样什么错误信息都不会输出。或者也可以在catch体内调用一个自定义的捕获错误的函数，以便调试其中的错误。
</p>
<p>当然，错误也有可能来自开发人员本身代码的错误，所以在代码中加上适当的注释信息是必不可少的。</p>
<h3>10.使用简约的多行注释对代码进行说明</h3>
<p>在Javascript中，注释有单行注释和多行注释之分。而有时候的注释文字很长时，良好的习惯是将注释文字放在多行中，以便于阅读。</p>

<div class="wp_codebox"><table><tr id="p941100"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p941code100"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
 * 我是多行注释 ...
 * 注释..
 * 注释...
 * 注释...
 * 注释...
 */</span></pre></td></tr></table></div>

</p>
<p><strong>总结：</strong>上面的10条小技巧或许对大家来说已经是了然于胸了，但有时候编程晕头晕脑的，不经意间就会忘记。所以在平时的编程过程中养成一个良好的编程习惯是一个程序员最基础的素质要求。只要多练、多看、多学习，我们的编程技术就会越来越高明的！</p>
<p>该文整理来自：<a href="http://www.impressivewebs.com/10-javascript-quick-tips-and-best-practices/" rel="external">Impressive Webs:10 JavaScript Quick Tips and Best Practices</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li><li>2010年12月30日 -- <a href="http://www.ihiro.org/javascript-getweek-by-date" title="Javascript:根据指定日期获得星期数">Javascript:根据指定日期获得星期数</a> (23)</li><li>2010年11月22日 -- <a href="http://www.ihiro.org/text-range" title="Range:文本框选区学习笔记">Range:文本框选区学习笔记</a> (16)</li><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年11月30日 -- <a href="http://www.ihiro.org/coding-clean-and-rich-semantic-html" title="HTML:编写干净的且富有语义的html代码">HTML:编写干净的且富有语义的html代码</a> (13)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/10-javascript-tips/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</title>
		<link>http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy</link>
		<comments>http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy#comments</comments>
		<pubDate>Wed, 16 Dec 2009 03:42:39 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[前端工程]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=932</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/12/tooltips-logo.png" alt="tooltips-logo" title="tooltips-logo" width="100" height="100" class="alignleft size-full wp-image-938" />
<p>jQuery的最大特色之一就是方法连缀写法，这样的书写方式使得阅读起来更加方便。于是乎，在日常的开发中大量使用连缀写法，而事件方法连缀就是一个特例。若给一个Dom对象使用了绑定了多个事件，便于阅读和书写，习惯了使用连缀写法，但这样的书写方式会造成时间的冗余。</p>
<h3>1.事件冗余：多个事件方法中多次调用相同的代码</h3>
<h3>2.事件委派：一次性绑定多个事件，根据事件类别委派相应的操作</h3>]]></description>
			<content:encoded><![CDATA[<p>jQuery的最大特色之一就是方法连缀写法，这样的书写方式使得阅读起来更加方便。于是乎，在日常的开发中大量使用连缀写法，而事件方法连缀就是一个特例。若给一个Dom对象使用了绑定了多个事件，便于阅读和书写，习惯了使用连缀写法，但这样的书写方式会造成时间的冗余。</p>
<h3>1.事件冗余：多个事件方法中多次调用相同的代码</h3>
<p>下面的代码是一个事件方法连缀的写法：</p>

<div class="wp_codebox"><table><tr id="p932101"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p932code101"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			tipTitle <span style="color: #339933;">=</span> link.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
			link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
				top<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span>
				left<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">12</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
			.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>
			.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title'</span><span style="color: #339933;">,</span> tipTitle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
				top<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span>
				left<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">12</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>其中第5|6行、第18|19行、第24|25行多次地使用了同样的一段代码去判断事件对象是否存在。这不管是从代码效率还是代码文件大小的方面来说都是不合理的方法。
</p>
<h3>2.事件委派：一次性绑定多个事件，根据事件类别委派相应的操作</h3>
<p>为了更好地优化上面的代码，可以通过事件委派来修改代码，修改后的代码如下：</p>

<div class="wp_codebox"><table><tr id="p932102"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p932code102"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #003366; font-weight: bold;">var</span> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	  <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</span> <span style="color: #339933;">||</span> event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mousemove'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$liveTip.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		  top<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span>
		  left<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">12</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		tipTitle <span style="color: #339933;">=</span> link.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
		link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
		$liveTip.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>
			.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseout'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$liveTip.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tipTitle<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> tipTitle<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>该段代码中一次性地将多个事件绑定到一个待处理的DOM对象之上，在代码的内部通过判断事件的类别来委派不同的处理代码。这样可以避免代码的重复定义，以达到避免时间冗余的效果。
</p>
<p>以上两种代码的执行后的效果是完全一样的，相信大家一眼就可以看中哪一种代码的执行效率更加的快速吧！</p>
<p class="demo-down-bar"><a href="http://www.ihiro.org/cases/event_delegation/event_delegation.rar" class="download">download</a><a href="http://www.ihiro.org/cases/event_delegation/" rel="external" class="demo">demo</a></p>
<p><strong style="color:#090;">以上的代码来自：<a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips" rel="external">Learning jQuery:Binding Multiple Events to Reduce Redundancy with Event-Delegation Tooltips</a></strong></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2009年08月6日 -- <a href="http://www.ihiro.org/drop-down-menu" title="jQuery:两个样式下拉菜单(click hover)">jQuery:两个样式下拉菜单(click hover)</a> (8)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>jQuery:如何在页面和插件代码中自定义别名</title>
		<link>http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code</link>
		<comments>http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code#comments</comments>
		<pubDate>Tue, 01 Dec 2009 14:03:47 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[前端工程]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=860</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/12/jquery-plugins.gif" alt="jquery-plugins" title="jquery-plugins" width="100" height="100" class="alignleft size-full wp-image-868" />
<p>众所周知，jQuery是采用$符号作为库的别名，但若是和其他类型的库一直使用是，其他库也有用$符号作为别名的，这样就会引起函数的冲突。所以更多的时间是建议大家使用jQuery来做为别名，或者自定义别名。该片文章重点地讲述了如何在页面代码和插件代码中自定义别名，以便于其他库的兼容，为之后的插件开发之旅铺垫道路！</p>
<h3>1.在页面使用代码中如何自定义别名；</h3>
<h3>2.在插件代码中如何自定义别名。</h3>]]></description>
			<content:encoded><![CDATA[<p>众所周知，jQuery是采用$符号作为库的别名，但若是和其他类型的库一直使用是，其他库也有用$符号作为别名的，这样就会引起函数的冲突。所以更多的时间是建议大家使用jQuery来做为别名，或者自定义别名。该片文章重点地讲述了如何在页面代码和插件代码中自定义别名，以便于其他库的兼容，为之后的插件开发之旅铺垫道路！</p>
<h3>1.在页面使用代码中如何自定义别名</h3>
<p>通常，我们是这样开始jQuery库的使用的：</p>

<div class="wp_codebox"><table><tr id="p860103"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p860code103"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//一些代码</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//或者是简写的方式：</span>
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//一些代码</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>但是，若遇到了使用其他非jQuery库的情况，就需要对上面的代码进行改动了，所以兼容性不够好。比较好的方法是将$符号写全（jQuery）：</p>

<div class="wp_codebox"><table><tr id="p860104"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p860code104"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//一些代码</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>但是大家或许发现了，这样写的话，以后查找dom、或调用jQuery库中的函数，都需要写jQuery(&#8216;p&#8217;)、jQuery.trim()。是否觉得比较麻烦，还是觉得$符号好用呢？
</p>
<p>那么为你来介绍如何在页面代码中自定义别名的方法吧。格式如下：</p>

<div class="wp_codebox"><table><tr id="p860105"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p860code105"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>hiro<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//在内部我就可以这样使用,来弹出我当前使用浏览器的版本号</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>hiro.<span style="color: #660066;">browser</span>.<span style="color: #660066;">version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>只需要在function中传入一个自定义的参数，该参数就是你的自定义别名了。该别名可以随意地自定义，当然可以使用$符号来自定义了，格式如下：</p>

<div class="wp_codebox"><table><tr id="p860106"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p860code106"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//在内部我就可以这样使用,来弹出我当前使用浏览器的版本号</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">browser</span>.<span style="color: #660066;">version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>这样使用的好处是，即使你在引入新的库后，也不会和新库产生冲突。
</p>
<h3>2.在插件代码中如何自定义别名</h3>
<p>插件的实现原理就是将所有的插件代码都写在一个方法体内，然后执行即可。就像这样：</p>

<div class="wp_codebox"><table><tr id="p860107"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p860code107"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// 这里可以写插件的代码</span>
  <span style="color: #003366; font-weight: bold;">var</span> xyz<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// xyz 不是一个全局变量，他只在该方法体内可以获得</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 该方法会立即执行（这里用到了匿名函数的概念）</span></pre></td></tr></table></div>

<p>这里多余的括号是必须的，因为你若不加括号的话，匿名函数就得不到执行。
</p>
<p>那么在插件中自定义别名就很简单了，和在页面中自定义别名类似，格式如下：</p>

<div class="wp_codebox"><table><tr id="p860108"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p860code108"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// 这里可以写插件的代码</span>
  <span style="color: #003366; font-weight: bold;">var</span> xyz<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// xyz 不是一个全局变量，他只在该方法体内可以获得</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>在上面的我给方法传入一个jQuery的参数，是因为该方法是对jQuery库的扩展，那么在该方法体内就可以调用jQuery库中的函数；若不传入，则无法调用jQuery库
</p>
<h3>一个小的插件编写技巧：</h3>
<p>在我阅读国外的开发人员编写的插件时，发现很多的插件都会在代码的开始部分加上一个分号，格式如下：</p>

<div class="wp_codebox"><table><tr id="p860109"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p860code109"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// 这里可以写插件的代码</span>
  <span style="color: #003366; font-weight: bold;">var</span> xyz<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// xyz 不是一个全局变量，他只在该方法体内可以获得</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>我猜测这是因为很多的插件在代码的最前面会加上一些版权信息和使用方法的注释信息，加一个分号是为了防止注释结束遗忘吧，也告示着插件代码的开始！(<strong style="color:#f60;">个人猜测，仅供参考！</strong>)
</p>
<p>具体的代码实现格式和运用可以阅读<a href="http://www.ihiro.org/cases/js/jquery-tooltip/jquery.tooltip.js" rel="external">jQuery.tooltips.js</a>的源代码，他的编写方式比较“正统”！</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2009年08月6日 -- <a href="http://www.ihiro.org/drop-down-menu" title="jQuery:两个样式下拉菜单(click hover)">jQuery:两个样式下拉菜单(click hover)</a> (8)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>HTML:编写干净的且富有语义的html代码</title>
		<link>http://www.ihiro.org/coding-clean-and-rich-semantic-html</link>
		<comments>http://www.ihiro.org/coding-clean-and-rich-semantic-html#comments</comments>
		<pubDate>Mon, 30 Nov 2009 03:44:08 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[CSS、XHTML]]></category>
		<category><![CDATA[前端工程译文]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[译文]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=845</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/cleaner-coding.gif" alt="cleaner-coding" title="cleaner-coding" width="100" height="100" class="alignleft size-full wp-image-855" />
<p>如果你是一个使用div标签做任何事的家伙，那么这篇文章将对你十分有益。该问重点讲述了如何通过使用富有语义的标签和最少的div标记来编写干净的html代码。你是否曾经也试图去修改别人的一些html模板，那么你一定会被代码里那些肮脏的标签所抓狂。编写干净的html代码，不仅对你自己有益，而且对你的团队也有益。他将大大地节约了你的开发与错误调试时间（特别是比较大的一些项目）。</p>
<p>1.尽可能少地使用div标签；<br />
2.使用div标签来控制页面的整体布局，如头部，内容部，侧边栏，底部等；<br />
3.内容必须包含在对应的语义标签内，而不是包含在div内；<br />
4.格式化你的源代码并且在结束标签处给出对应的注释语言。</p>
<p>该文翻译自：<a href="http://www.webdesignerwall.com/tutorials/coding-clean-and-semantic-templates/" rel="external">WebDesignWall: Coding Clean and Semantic Templates</a></p>]]></description>
			<content:encoded><![CDATA[<p>如果你是一个使用div标签做任何事的家伙，那么这篇文章将对你十分有益。该问重点讲述了如何通过使用富有语义的标签和最少的div标记来编写干净的html代码。你是否曾经也试图去修改别人的一些html模板，那么你一定会被代码里那些肮脏的标签所抓狂。编写干净的html代码，不仅对你自己有益，而且对你的团队也有益。他将大大地节约了你的开发与错误调试时间（特别是比较大的一些项目）。</p>
<h3>1.去除不必要的div标签：</h3>
<p>我曾经看到好多的人将form和ul标签都包含在了div标签内。为什么要写一个不必要的标签呢。你可以给适当的选择器赋予css规则，这样也可以达到同样的效果。</p>
<h4>例子一：</h4>
<p>下面的列子将展示给你如何删除多余的div标签，而将css规则声明给内部的form标签。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_01_div-form.gif" alt="clean_html_01_div-form" title="clean_html_01_div-form" width="470" height="259" class="aligncenter size-full wp-image-846" /></p>
<h4>例子二：</h4>
<p>有时侯我们会将一些内容用额外的div标签包含起来，其目的是为了严格地控制布局的空隙。在这个例子的左边使用了一个&lt;div class=”box”&gt;来控制两个box之间的外边距margin。但如果每个box内都有一个标题h4，那么我们可以通过h4标签来控制外间距margin，从而可以删除掉多余的&lt;div class=”box”&gt;标签。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_02_div-sidebox.gif" alt="clean_html_02_div-sidebox" title="clean_html_02_div-sidebox" width="470" height="267" class="aligncenter size-full wp-image-847" /></p>
<h3>2.使用富有语义的标记：</h3>
<p>你必须时刻都使用富有语义的标记来编写html代码（如：h1用作标题，p用作文章段落，ul则用于列表项）。那么，即使你的css文件没有引入或者没有被支持，你的html文档看起来也是富有意义的。</p>
<h4>例子：</h4>
<p>下面的一张图片比较了使用div标记和使用富有语义的标记编写的一段html代码（没有css代码的支持）在浏览器中编译后的效果。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_03_semantic-markups.gif" alt="clean_html_03_semantic-markups" title="clean_html_03_semantic-markups" width="470" height="482" class="aligncenter size-full wp-image-848" /></p>
<h3>3.尽可能地少使用div标签：</h3>
<p>你是否看到过一些到处都是div的html文档，它是否让你非常地抓狂？是否你曾忘记关闭一个&lt;/div&gt;或者无意添加了一个打开的&lt;div&gt;标签呢？我确定绝大多数的开发者都曾有过这样的经验。所以你需要尽可能少地使用div标签。这样以后的调试和改错的过程就会变得更加容易。</p>
<h4>例子一：</h4>
<p>用p标签来替代div标签去包含一个面包屑导航。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_04_div-breadcrumb.gif" alt="clean_html_04_div-breadcrumb" title="clean_html_04_div-breadcrumb" width="470" height="130" class="aligncenter size-full wp-image-849" /></p>
<h4>例子二：</h4>
<p>下面的例子展示了如何通过css规则来将两个div标签替换成一个span标签。这两种写法得到的结果是完全一致的。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_05_div-date.gif" alt="clean_html_05_div-date" title="clean_html_05_div-date" width="470" height="500" class="aligncenter size-full wp-image-850" /></p>
<h3>4.格式化（缩进）你的代码：</h3>
<p>你在开发的过程中始终要格式化你的源代码（如缩进嵌套的标签），这样的可以方便以后的阅读和错误调试。如果你使用Adobe Dreamweaver的话，那么你可以很简单地去格式化源代码。方法：点击“命令”菜单下的“应用源代码”选项即可。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_06_format-code.gif" alt="clean_html_06_format-code" title="clean_html_06_format-code" width="470" height="203" class="aligncenter size-full wp-image-851" /></p>
<h3>5.在&lt;/div&gt;结束处给出相应的注释：</h3>
<p>当你编写一个平台模板(如：Wordpress主题)代码时，这些模板文件会被分割成多个不同的文件：如：index.php, header.php, sidebar.php, 和 footer.php。因此，你必须在&lt;/div&gt;结束处给出相应的注释。比如：当我看到&lt;/div&gt;&lt;!&#8211; /wrapper &#8211;&gt;时，我就知道这里是&lt;div id=”wrapper”&gt;标签的结束处。<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/clean_html_07_comment-code.gif" alt="clean_html_07_comment-code" title="clean_html_07_comment-code" width="470" height="122" class="aligncenter size-full wp-image-852" /></p>
<h3>总结：</h3>
<p>1.尽可能少地使用div标签；<br />
2.使用div标签来控制页面的整体布局，如头部，内容部，侧边栏，底部等；<br />
3.内容必须包含在对应的语义标签内，而不是包含在div内；<br />
4.格式化你的源代码并且在结束标签处给出对应的注释语言。</p>
<p>该文翻译自：<a href="http://www.webdesignerwall.com/tutorials/coding-clean-and-semantic-templates/" rel="external">WebDesignWall: Coding Clean and Semantic Templates</a></p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月18日 -- <a href="http://www.ihiro.org/10-javascript-tips" title="Javascript:10个Javscript编程的技巧">Javascript:10个Javscript编程的技巧</a> (4)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li><li>2011年03月21日 -- <a href="http://www.ihiro.org/html5-reading-note-chp3-elements" title="HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements">HTML5:《HTML5. Up and Running》读书笔记.Chp3.Elements</a> (24)</li><li>2011年03月15日 -- <a href="http://www.ihiro.org/html5-reading-note-chp2-features" title="HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features">HTML5:《HTML5. Up and Running》读书笔记.Chp2.Features</a> (23)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/coding-clean-and-rich-semantic-html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</title>
		<link>http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board</link>
		<comments>http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board#comments</comments>
		<pubDate>Thu, 26 Nov 2009 15:30:43 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[案例分享]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[前端工程]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=835</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/11/noticeBox-thum.gif" alt="noticeBox-thum" title="noticeBox-thum" width="100" height="100" class="alignleft size-full wp-image-837" />
<p>昨天晚上收到<label style="color:#090; font-weight:bold;">随风同学</label>的求助邮件，说因上级的要求需要制作一个走马灯效果的公告栏，希望我可以帮助他解决。因看到邮件的时候都快要睡觉了，所以并没有立即处理。</p>
<p>今天早上到了公司，放开手边的工作，花了一点点时间给他制作了一个主流浏览器都兼容的走马灯公告栏。然后mail给他，但他过会儿回邮件说缺少鼠标悬停时停止的效果。于是乎有花了几分钟添加了相应的代码。到了下午快下班的时候，该同学又发来了邮件，说我的公告栏的效果有点欠缺，现在的效果和marquee标签<label style="color:#f60;">(该标签不符合W3C标准)</label>的效果一样，需要等内容上移完成后才会重复滚动，如果可以连续滚动的话那就完美了。</p>
<p>于是就在刚刚的时间我有花了几分钟的时间给他解决了，mail给他。不知道他是否还会有什么新的要求呢，期待啊！毕竟这对我来说也是一个挑战嘛，同时也帮助到了别人，何乐而不为呢！</p>]]></description>
			<content:encoded><![CDATA[<p>昨天晚上收到<label style="color:#090; font-weight:bold;">随风同学</label>的求助邮件，说因上级的要求需要制作一个走马灯效果的公告栏，希望我可以帮助他解决。因看到邮件的时候都快要睡觉了，所以并没有立即处理。</p>
<p>今天早上到了公司，放开手边的工作，花了一点点时间给他制作了一个主流浏览器都兼容的走马灯公告栏。然后mail给他，但他过会儿回邮件说缺少鼠标悬停时停止的效果。于是乎有花了几分钟添加了相应的代码。到了下午快下班的时候，该同学又发来了邮件，说我的公告栏的效果有点欠缺，现在的效果和marquee标签<label style="color:#f60;">(该标签不符合W3C标准)</label>的效果一样，需要等内容上移完成后才会重复滚动，如果可以连续滚动的话那就完美了。</p>
<p>于是就在刚刚的时间我有花了几分钟的时间给他解决了，mail给他。不知道他是否还会有什么新的要求呢，期待啊！毕竟这对我来说也是一个挑战嘛，同时也帮助到了别人，何乐而不为呢！</p>
<p>因为觉得功能差不多都实现了，所以也共享出来与大家分享，或许会对你有所帮助呢。</p>
<p class="demo-down-bar"><a href="http://www.ihiro.org/cases/noticeBox/noticeBox.rar" class="download">download</a><a href="http://www.ihiro.org/cases/noticeBox/" rel="external" class="demo">demo</a></p>
<h3>1.Html代码：</h3>

<div class="wp_codebox"><table><tr id="p835110"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p835code110"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;container&quot;&gt;
    &lt;ul class=&quot;noticeList&quot;&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/wordpress-the-article-automatically-appear-as-two&quot; title=&quot;Wordpress:将文章自动显示为两列&quot;&gt;Wordpress:将文章自动显示为两列&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/film-2012-really-have-that-day-discovery-channel-expose-for-you&quot; title=&quot;电影:《2012》真的有那一天吗？探索频道为你揭露&quot;&gt;电影:《2012》真的有那一天吗？探索频道为你揭露&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/information-november-2009-programming-language-list&quot; title=&quot;资讯:2009年11月编程语言排行榜&quot;&gt;资讯:2009年11月编程语言排行榜&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/a-busy-week-for-rent-a-house-was-finally-gaoding&quot; title=&quot;租房:忙碌一周，房子终于搞定&quot;&gt;租房:忙碌一周，房子终于搞定&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/my-third-theme-innernews&quot; title=&quot;主题:我的第三个主题innerNews提供下载&quot;&gt;主题:我的第三个主题innerNews提供下载&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/my-first-plugin-scrolltop&quot; title=&quot;插件:我的第一个插件–简约的scrollTop滑动插件&quot;&gt;插件:我的第一个插件–简约的scrollTop滑动插件&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/celebrate-my-blog-pr-catapulted-3&quot; title=&quot;PageRank:庆贺我的博客PR连升3级&quot;&gt;PageRank:庆贺我的博客PR连升3级&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/myeclipse-8-and-registration-number&quot; title=&quot;软件:MyEclipse 8.0发布，附注册码&quot;&gt;软件:MyEclipse 8.0发布，附注册码&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/tv-leng-jian&quot; title=&quot;电视:《冷箭》，让我“失眠”的谍战片&quot;&gt;电视:《冷箭》，让我“失眠”的谍战片&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/hd-is-an-attitude&quot; title=&quot;下载:高清，是一种态度！&quot;&gt;下载:高清，是一种态度！&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<h3>2.CSS代码：</h3>

<div class="wp_codebox"><table><tr id="p835111"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p835code111"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span><span style="color: #933;">12px</span> Verdana<span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#container</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">180px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">250px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
	<span style="color: #6666ff;">.noticeList</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">180px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span><span style="color: #993333;">inside</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">270px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
	<span style="color: #6666ff;">.noticeList</span> li <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
	<span style="color: #6666ff;">.noticeList</span> li a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#606060</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
	<span style="color: #6666ff;">.noticeList</span> li a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#09F</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>3.JS代码：（备了部分注释，不懂的可以留言询问。）</h3>

<div class="wp_codebox"><table><tr id="p835112"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p835code112"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">function</span> ScrollAction<span style="color: #009900;">&#40;</span>listObj<span style="color: #339933;">,</span> listElem<span style="color: #339933;">,</span> speed<span style="color: #339933;">,</span> isSeries<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #006600; font-style: italic;">//listObj为需要滚动的列表，  speed为滚动速度</span>
		<span style="color: #003366; font-weight: bold;">var</span> pos<span style="color: #339933;">,</span> top<span style="color: #339933;">,</span> aniTop<span style="color: #339933;">,</span> height<span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//记录setInterval的标记id</span>
&nbsp;
		pos <span style="color: #339933;">=</span> listObj.<span style="color: #660066;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		top <span style="color: #339933;">=</span> pos.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>			<span style="color: #006600; font-style: italic;">//列表的top</span>
		aniTop <span style="color: #339933;">=</span> top<span style="color: #339933;">;</span>				<span style="color: #006600; font-style: italic;">//记录当前运动时的top</span>
		height <span style="color: #339933;">=</span> listObj.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//列表的高度</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> scrollUp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			aniTop<span style="color: #339933;">--;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isSeries<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #006600; font-style: italic;">//isSeries变量控制是否连续滚动，false不连续，true连续</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>aniTop <span style="color: #339933;">==</span> <span style="color: #339933;">-</span>height<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #006600; font-style: italic;">//不连续，滚动玩重新滚动</span>
					listObj.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span> top<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					aniTop <span style="color: #339933;">=</span> top<span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>aniTop <span style="color: #339933;">==</span> <span style="color: #339933;">-</span>listObj.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #006600; font-style: italic;">//连续滚动</span>
					<span style="color: #003366; font-weight: bold;">var</span> firstItem <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt; '</span> <span style="color: #339933;">+</span> listElem <span style="color: #339933;">+</span><span style="color: #3366CC;">'&gt;'</span> <span style="color: #339933;">+</span> listObj.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt; /'</span> <span style="color: #339933;">+</span> listElem <span style="color: #339933;">+</span><span style="color: #3366CC;">'&gt;'</span><span style="color: #339933;">;</span>
					listObj.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					listObj.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>firstItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					aniTop <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			listObj.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span> aniTop <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> hover <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			listObj.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				clearInterval<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				id <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>scrollUp<span style="color: #339933;">,</span> speed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">start</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			id <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>scrollUp<span style="color: #339933;">,</span> speed<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			hover<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> sa <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ScrollAction<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.noticeList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'li'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	sa.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p style="color:#f00;">使用时别忘了引入jQuery库哦，也希望大家给我提出意见，或功能扩充等要求。</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年09月2日 -- <a href="http://www.ihiro.org/example-selectbox-simulation" title="示例:Selectbox模拟实现效果">示例:Selectbox模拟实现效果</a> (22)</li><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li><li>2009年08月28日 -- <a href="http://www.ihiro.org/jquery-hot-pictures-showing-effects-of-rotation" title="jQuery:热点图片轮换展示效果">jQuery:热点图片轮换展示效果</a> (14)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Ajax:当前页面更改内容，前端实现</title>
		<link>http://www.ihiro.org/ajax-editing-in-placing</link>
		<comments>http://www.ihiro.org/ajax-editing-in-placing#comments</comments>
		<pubDate>Wed, 23 Sep 2009 04:26:22 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[案例分享]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[前端工程]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=677</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/09/Ajax-Editing-150x150.jpg" alt="Ajax-Editing" title="Ajax-Editing" width="100" height="100" class="alignleft wp-image-682" />
<p>相信大家有用过Google Doc或Flickr，或者一些SNS社区的图片添加描述信息的效果，他们是在点击了一段文字后出现一个输入框，在输入框内输入你要填写的内容，点击“确认”或者“发表”即可立即显示出来。</p>
<p>今天做的这个效果呢，就是其中的截取版本。为什么这么说呢，因为我的效果只实现了前端的更改后立即发布的效果，而没有实际的与服务器端交互，也没有将输入的内容添加到数据库中，主要是为大家介绍一下实现原理。</p>
<p class="color-red">注意：源代码中没有jQuery库文件，请下载查看代码的朋友根据源代码路径自行添加。</p>]]></description>
			<content:encoded><![CDATA[<p>相信大家有用过Google Doc或Flickr，或者一些SNS社区的图片添加描述信息的效果，他们是在点击了一段文字后出现一个输入框，在输入框内输入你要填写的内容，点击“确认”或者“发表”即可立即显示出来。</p>
<p>今天做的这个效果呢，就是其中的截取版本。为什么这么说呢，因为我的效果只实现了前端的更改后立即发布的效果，而没有实际的与服务器端交互，也没有将输入的内容添加到数据库中，主要是为大家介绍一下实现原理。</p>
<p class="demo-down-bar"><a href="http://www.ihiro.org/cases/AjaxEditing/AjaxEditing.rar" class="download">download</a><a href="http://www.ihiro.org/cases/AjaxEditing/" class="demo">demo</a></p>
<h3>1.HTML代码：</h3>
<p>
<div class="wp_codebox"><table><tr id="p677113"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p677code113"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;container&quot;&gt;
  &lt;h1&gt;Ajax Editing&lt;/h1&gt;
  &lt;p&gt;鼠标移动到列表上，双击即可以进行编辑！&lt;/p&gt;
  &lt;div class=&quot;block&quot;&gt;
    &lt;h2&gt;一、电影列表&lt;/h2&gt;
    &lt;ul class=&quot;editable&quot;&gt;
      &lt;li&gt;《建国大业》，&lt;span class=&quot;color_red&quot;&gt;更新中...&lt;/span&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/film-eternal-beloved&quot;&gt;《爱有来生》&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/movie-coco-chanel&quot;&gt;《COCO Chanel》&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/one-night-in-supermarket&quot;&gt;《夜店》&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/the-legend-of-1900&quot;&gt;《海上钢琴师–1900的传说》&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.ihiro.org/film-xinsushijian-feedback&quot;&gt;《新宿事件》&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  &lt;div class=&quot;block&quot;&gt;
    &lt;h2&gt;二、书籍列表&lt;/h2&gt;
    &lt;ul class=&quot;editable&quot;&gt;
      &lt;li&gt;《精通Javascript》&lt;/li&gt;
      &lt;li&gt;《jQuery基础教程》&lt;/li&gt;
      &lt;li&gt;《Flex入门到精通》&lt;/li&gt;
      &lt;li&gt;《PHP与MySQL Web开发》&lt;/li&gt;
      &lt;li&gt;《Google，你可以做什么》&lt;/li&gt;
      &lt;li&gt;《不抱怨的世界》&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>

</p>
<h3></h3>
<p>
<div class="wp_codebox"><table><tr id="p677114"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p677code114"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span><span style="color: #933;">12px</span>/<span style="color: #933;">20px</span> Georgia<span style="color: #00AA00;">,</span> <span style="color: #ff0000;">&quot;Times New Roman&quot;</span><span style="color: #00AA00;">,</span> Times<span style="color: #00AA00;">,</span> <span style="color: #993333;">serif</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#404040</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
h2 <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">24px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.color_red</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#F00</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#container</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">400px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">1%</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* 标题样式 */</span>
	<span style="color: #cc00cc;">#container</span> h1 <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span><span style="color: #933;">32px</span> <span style="color: #ff0000;">&quot;Lucida Sans Unicode&quot;</span><span style="color: #00AA00;">,</span> <span style="color: #ff0000;">&quot;Lucida Grande&quot;</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
	.<span style="color: #993333;">block</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">180px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span> <span style="color: #933;">20px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* 列表li的样式 */</span>
	ul<span style="color: #6666ff;">.editable</span> li <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* 高亮当前编辑的列表li */</span>
	<span style="color: #6666ff;">.hover</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#E8F3FF</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* 高亮当前的input输入框 */</span>
	<span style="color: #6666ff;">.editInput</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">160px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#E8F3FF</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* save和cancel按钮的效果，鼠标移动到上面的效果 */</span>
	<span style="color: #6666ff;">.btnSave</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.btnCancel</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#09F</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#36F</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
	<span style="color: #6666ff;">.btnSave</span><span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.btnCancel</span><span style="color: #3333ff;">:hover  </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#36F</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#39F</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

</p>
<h3>3. jQuery代码：</h3>
<p>
<div class="wp_codebox"><table><tr id="p677115"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p677code115"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> oldText<span style="color: #339933;">,</span> newText<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//鼠标移动到列表li上后，添加一个高亮的背景</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.editable li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dblclick'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//获得列表li中原先的html代码</span>
		oldText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&quot;/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//将原先的html代码放在一个input中，并追加save和cancel按钮后插入到列表li中</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;input type=&quot;text&quot; class=&quot;editInput&quot; value=&quot;'</span> <span style="color: #339933;">+</span> oldText <span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; /&gt;&lt;a href=&quot;#&quot; class=&quot;btnSave&quot;&gt;save&lt;/a&gt;&lt;a href=&quot;#&quot; class=&quot;btnCancel&quot;&gt;cancel&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'target'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'_blank'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//链接在新窗口中打开</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//点击save按钮，将新的值插入在列表li中</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.btnSave'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		newText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&quot;/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>newText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//设置超链接a的跳转为否</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//若点击cancel按钮，则将最初的html代码插入在列表li中</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.btnCancel'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>oldText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//设置超链接a的跳转为否</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>双击列表li后，将原先列表li中的html源码保存在oldText变量中，并且在列表li标签内插入一个input输入框和两个链接作为按钮save和cancel之用；<br />
点击save按钮后，将input中的新的html源码保存在newText变量中，重新插入到列表li中，从而覆盖了列表里中的input源码；<br />
点击cancel按钮，浙江原先的html代码oldText重新插入到列表li中，覆盖掉因双击插入的input输入框和两个链接按钮。
</p>
<p class="color-red">注意：源代码中没有jQuery库文件，请下载查看代码的朋友根据源代码路径自行添加。</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2010年11月25日 -- <a href="http://www.ihiro.org/css-position-hack" title="CSS:position属性定位总结">CSS:position属性定位总结</a> (27)</li><li>2010年09月2日 -- <a href="http://www.ihiro.org/example-selectbox-simulation" title="示例:Selectbox模拟实现效果">示例:Selectbox模拟实现效果</a> (22)</li><li>2010年03月25日 -- <a href="http://www.ihiro.org/the-enhanced-ajax-effect-instructions" title="Ajax:近期博客改版的增强Ajax加载说明">Ajax:近期博客改版的增强Ajax加载说明</a> (43)</li><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年03月8日 -- <a href="http://www.ihiro.org/css3-basic" title="CSS:CSS3基础（附图）">CSS:CSS3基础（附图）</a> (12)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/ajax-editing-in-placing/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>jQuery:两个样式下拉菜单(click hover)</title>
		<link>http://www.ihiro.org/drop-down-menu</link>
		<comments>http://www.ihiro.org/drop-down-menu#comments</comments>
		<pubDate>Thu, 06 Aug 2009 06:51:18 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[案例分享]]></category>
		<category><![CDATA[前端工程]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=367</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/08/dropdownmenu.jpg" alt="dropdownmenu" title="dropdownmenu" width="100" height="90" class="alignleft  wp-image-368" />
<p>一直都很喜欢下拉菜单，因为这样可以导航到更多的页面，给访问者一目了然的感觉。</p>
<p>之前在公司的网站上就是用的下拉菜单导航栏的方式进行导航，当时是使用了li:hover的方式通过鼠标移动到li上，显示出li下的ul。而在ie6中则使用javascript修复li:hover。这样做出来是没有动态的效果，下拉菜单一下子就凭空出现了。</p>
<p>运用jQuery库制作下拉菜单的好处就是可以做出各种不同的动态效果，我今天讲解的是运用slideDown()和slideUp()函数实现的卷页效果。</p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ihiro.org/blog/wp-content/uploads/2009/08/dropdownmenu.jpg" alt="dropdownmenu" title="dropdownmenu" width="150" height="133" class="alignright size-full wp-image-368" /></p>
<p>一直都很喜欢下拉菜单，因为这样可以导航到更多的页面，给访问者一目了然的感觉。</p>
<p>之前在公司的网站上就是用的下拉菜单导航栏的方式进行导航，当时是使用了li:hover的方式通过鼠标移动到li上，显示出li下的ul。而在ie6中则使用javascript修复li:hover。这样做出来是没有动态的效果，下拉菜单一下子就凭空出现了。</p>
<p>运用jQuery库制作下拉菜单的好处就是可以做出各种不同的动态效果，我今天讲解的是运用slideDown()和slideUp()函数实现的卷页效果。</p>
<p>说明：第一个导航#nav1是点击后出现下拉菜单，而第二个导航#nav2是鼠标移动到上面后出现导航。(样式只为效果，没有美化，望见谅)</p>
<p class="demo-down-bar"><a href="http://www.ihiro.org/cases/DropDownMenu/DropDownMenu.rar" class="download" rel="external">Download</a><a href="http://www.ihiro.org/cases/DropDownMenu/" class="demo" rel="external">Demo</a></p>
<h3>1.html代码：</h3>

<div class="wp_codebox"><table><tr id="p367116"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code" id="p367code116"><pre class="html" style="font-family:monospace;">&lt;ul class=&quot;nav&quot; id=&quot;nav1&quot;&gt;
    	&lt;li&gt;点击方式下拉菜单:click&lt;/li&gt;
    	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;首页&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;关于&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;技术&lt;/a&gt;
        	&lt;ul class=&quot;subnav&quot;&gt;
            	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;wordpress&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;php&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;jQuery&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;CSS&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;影视&lt;/a&gt;
        	&lt;ul class=&quot;subnav&quot;&gt;
            	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;电视剧&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;电影&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;舞台剧&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;联系&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&nbsp;
    &lt;ul class=&quot;nav&quot; id=&quot;nav2&quot;&gt;
    	&lt;li&gt;移动方式下拉菜单:hover&lt;/li&gt;
    	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;首页&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;关于&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;技术&lt;/a&gt;
        	&lt;ul class=&quot;subnav&quot;&gt;
            	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;wordpress&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;php&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;jQuery&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;CSS&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;影视&lt;/a&gt;
        	&lt;ul class=&quot;subnav&quot;&gt;
            	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;电视剧&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;电影&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;舞台剧&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;联系&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;</pre></td></tr></table></div>

<h3>2. CSS源码：</h3>

<div class="wp_codebox"><table><tr id="p367117"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p367code117"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span><span style="color: #933;">14px</span> <span style="color: #ff0000;">'宋体'</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
li <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">underline</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#nav1</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.nav</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">500px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#F60</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.nav</span> li <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding-right</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.nav</span> li a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.nav</span> li span <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">17px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">subnav_btn.gif</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.nav</span> li span<span style="color: #6666ff;">.subhover</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span><span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
ul<span style="color: #6666ff;">.subnav</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #933;">-20px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
ul<span style="color: #6666ff;">.subnav</span> li <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span><span style="color: #993333;">both</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>3. jQuery代码：</h3>

<div class="wp_codebox"><table><tr id="p367118"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p367code118"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span&gt;&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.subnav'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//鼠标点击向下箭头</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#nav1 li span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'subhover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.subnav'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'subhover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.subnav'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// 鼠标移动到含有下拉菜单的li标签上时</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#nav2 li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'subhover'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
			   .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.subnav'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'subhover'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
			   .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.subnav'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年03月15日 -- <a href="http://www.ihiro.org/uncovering-jquerys-hidden-features" title="jQuery:揭露jQuery的隐藏功能">jQuery:揭露jQuery的隐藏功能</a> (5)</li><li>2010年01月22日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-2" title="jQuery:1.4新版本中你应该知道的15个新特性(二)">jQuery:1.4新版本中你应该知道的15个新特性(二)</a> (7)</li><li>2010年01月20日 -- <a href="http://www.ihiro.org/jquery-1-4-released-the-15-new-features-you-should-know-1" title="jQuery:1.4新版本中你应该知道的15个新特性(一)">jQuery:1.4新版本中你应该知道的15个新特性(一)</a> (6)</li><li>2009年12月16日 -- <a href="http://www.ihiro.org/jquery-once-through-the-event-delegate-to-bind-a-variety-of-time-in-order-to-reduce-the-event-of-redundancy" title="jQuery:通过事件委派一次绑定多种事件，以减少事件冗余">jQuery:通过事件委派一次绑定多种事件，以减少事件冗余</a> (7)</li><li>2009年12月1日 -- <a href="http://www.ihiro.org/how-to-custom-alias-in-plugins-and-page-code" title="jQuery:如何在页面和插件代码中自定义别名">jQuery:如何在页面和插件代码中自定义别名</a> (5)</li><li>2009年11月26日 -- <a href="http://www.ihiro.org/jquery-enhanced-marquee-bulletin-board" title="jQuery:增强型走马灯公告栏，可以抛弃marquee标签了">jQuery:增强型走马灯公告栏，可以抛弃marquee标签了</a> (20)</li><li>2009年09月23日 -- <a href="http://www.ihiro.org/ajax-editing-in-placing" title="Ajax:当前页面更改内容，前端实现">Ajax:当前页面更改内容，前端实现</a> (14)</li><li>2012年01月6日 -- <a href="http://www.ihiro.org/xslt-get-the-first-last-name" title="XSLT:通过间隔符获取英文名的First Name和Last Name">XSLT:通过间隔符获取英文名的First Name和Last Name</a> (3)</li><li>2011年04月20日 -- <a href="http://www.ihiro.org/css3-transition" title="CSS3:Transition属性详解">CSS3:Transition属性详解</a> (82)</li><li>2011年04月1日 -- <a href="http://www.ihiro.org/html5-reading-note-chp4-canvas" title="HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas">HTML5:《HTML5. Up and Running》读书笔记.Chp4.Canvas</a> (22)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/drop-down-menu/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
