<?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>彬Go &#187; Javascript</title>
	<atom:link href="http://blog.bingo929.com/category/technology/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bingo929.com</link>
	<description>关注前端开发/网页设计/网站可用性/用户体验</description>
	<lastBuildDate>Fri, 16 Dec 2011 17:14:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)</title>
		<link>http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html</link>
		<comments>http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html#comments</comments>
		<pubDate>Sun, 11 Dec 2011 15:30:11 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[我的作品]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[拖拽]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3186</guid>
		<description><![CDATA[　　早在公元2011年6月3日傍晚，人人网推出了一个很装B且完全无视IE浏览器的功能——拖拽上床。哦，Sorry, 是拖拽上传。到现在，这个功能已经上线了整整半年，这篇文章也足足拖延了半年才... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html"><img title="人人网拖拽上传实例详解" src="http://blog.bingo929.com/wp-content/uploads/2011/12/blog-title.jpg" alt="" width="500" height="150" /></a></p>
<p>　　早在公元2011年6月3日傍晚，人人网推出了一个很装B且完全无视IE浏览器的功能——拖拽上床。哦，Sorry, 是<strong>拖拽上传</strong>。到现在，这个功能已经上线了整整半年，这篇文章也足足拖延了半年才分享给大家，实在是对不住了，呵呵，今后彬Go一定要勤奋发Blog！</p>
<p><span id="more-3186"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下HTML5相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html">HTML5 Drag&amp;Drop 拖拽、FileReader实例教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-websockets.html">HTML5 WebSockets 基础使用教程</a>》<br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》</p>
<p>　　关于这个拖拽上传，其实国外有很多网站已经有这样的应用，最早推出拖拽上传应用的是Gmail，它支持标准浏览器下拖拽本地文件到浏览器中作为邮件的附件发送。人人网的这个拖拽上传也是同理，可以让使用标准浏览器的用户通过简单的拖拽行为，将本地文件夹中的照片直接上传到人人网，用户体验能得到提升的同时，也希望借此机会推广一下标准浏览器，淘汰IE。人人网当时也向广大用户推出升级浏览器活动，并喊出口号：&#8221;工欲善其计算机，必先利其浏览器&#8221;。本次拖拽上传的宣传口号：你敢&#8221;脱&#8221;，我就敢上传&#8230;</p>
<p><a href="http://blog.bingo929.com/wp-content/uploads/2011/12/b_large_pbOj_3004000003b45c41.jpg"><img title="人人网 - 拖拽上传" src="http://blog.bingo929.com/wp-content/uploads/2011/12/b_large_pbOj_3004000003b45c41-500x259.jpg" alt="人人网 - 拖拽上传" width="500" height="259" /></a></p>
<p>　　言归正题，首先看看效果，大家如果有<a href="http://www.renren.com" target="_blank">人人网</a>帐号的话可以在首页试一试拖拽上传功能，下面是演示视频：</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="500" height="430" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="align" value="middle" /><param name="src" value="http://player.youku.com/player.php/sid/XMjcyNzU0ODY4/v.swf" /><param name="allowfullscreen" value="true" /><param name="quality" value="high" /><embed type="application/x-shockwave-flash" width="500" height="430" src="http://player.youku.com/player.php/sid/XMjcyNzU0ODY4/v.swf" quality="high" allowfullscreen="true" align="middle"></embed></object></p>
<p><strong>拖拽上传应用主要使用了以下<a href="http://blog.bingo929.com/tag/html5">HTML5</a>技术：</strong></p>
<ul>
<li><a href="http://dev.w3.org/html5/spec/Overview.html#dnd" target="_blank"><strong>Drag&amp;Drop</strong></a> : <a href="http://blog.bingo929.com/tag/html5">HTML5</a>基于拖拽的事件机制.</li>
<li><a href="http://www.w3.org/TR/FileAPI/" target="_blank"><strong>File API</strong></a> :  可以很方便的让Web应用访问文件对象，File API包括<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-filelist">FileList</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-Blob">Blob</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-file">File</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-filereader">FileReader</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#url">URI scheme</a>，本文主要讲解拖拽上传中用到的FileList和FileReader接口。</li>
<li><a href="http://www.w3.org/TR/XMLHttpRequest2/#the-formdata-interface" target="_blank"><strong>FormData</strong></a> : FormData是基于<a style="font-weight: normal;" href="http://www.w3.org/TR/XMLHttpRequest2/" target="_blank">XMLHttpRequest Level 2</a>的新接口，可以方便web应用模拟Form表单数据，最重要的是它支持文件的二进制流数据，这样我们就能够通过它来实现AJAX向后端发送文件数据了。</li>
</ul>
<h3><strong>HTML5 Drag&amp;Drop 拖拽事件</strong></h3>
<p>　　关于Drag&amp;Drop拖拽事件，之前我写过一篇专门介绍的文章<a style="font-weight: bold;" href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html" target="_blank">《给力的 Google HTML5 训练营(HTML5 Drag&amp;Drop 拖拽、FileReader实例教程)》</a>，那篇文章详细讲解了Drag &amp; Drap事件的原理和代码实例，这里的拖拽上传实现原理基本上是一样的，大家有兴趣或不太了解的话可以先看看那篇文章，我在这里就不再过多啰嗦了~下面直接出拖拽上传简要代码实例：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> oDragWrap <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//拖进</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragenter'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//拖离</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragleave'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　dragleaveHandler<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//拖来拖去 , 一定要注意dragover事件一定要清除默认事件</span><br />
<span style="color: #006600; font-style: italic;">//不然会无法触发后面的drop事件</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//扔</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'drop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　dropHandler<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> dropHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #006600; font-style: italic;">//将本地图片拖拽到页面中后要进行的处理都在这</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>获取文件数据 HTML5 File API</h3>
<p>　　在之前<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html" target="_blank">那篇文章</a>中我也有介绍过关于File API中的FileReader接口，作为 <a href="http://www.w3.org/TR/FileAPI/" target="_blank">File API</a> 的一部分，<a href="http://www.w3.org/TR/FileAPI/#dfn-filereader" target="_blank">FileReader</a> 专门用于读取文件，根据 W3C 的定义，FileReader 接口 “提供一些读取文件的方法与一个包含读取结果的事件模型”。关于FileReader的详细介绍和代码实例大家可以先去看看那篇<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html" target="_blank">文章</a>。</p>
<p>　　今天我着重介绍一下File API中的FileList接口,它主要通过两个途径获取本地文件列表，一是<strong>&lt;input type=&#8221;file&#8221;&gt;</strong>的表单形式，另一种则是<strong>e.dataTransfer.files</strong>拖拽事件传递的文件信息。很显然，我们这里会用到后者。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> fileList <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span></div></div>
<p>　　使用<strong>files方法</strong>将会获取到拖拽文件的数组形势的数据，每个文件占用一个数组的索引，如果该索引不存在文件数据，将返回null值。可以通过<strong>length</strong>属性获取文件数量.</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> fileNum <span style="color: #339933;">=</span> fileList.<span style="color: #660066;">length</span><span style="color: #339933;">;</span></div></div>
<p>　　拖拽上传需要注意的是需要判断两个条件，1:拖拽的是文件不是页面中的元素; 2:拖拽的是图片而不是其它文件,可以通过file.type属性获取文件的类型</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//检测是否是拖拽文件到页面的操作</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList.<span style="color: #660066;">length</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">0</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: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">//检测文件是不是图片</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</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></div></div>
<p>　　下面让我们来看看如何结合之前的拖拽事件来实现拖拽图片并在页面中进行预览：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> dropHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//获取文件列表</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> fileList <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测是否是拖拽文件到页面的操作</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</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: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测文件是不是图片</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</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><br />
<br />
　<span style="color: #006600; font-style: italic;">//实例化file reader对象</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　reader.<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>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span><br />
　　oDragWrap.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span><br />
　reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　这里有一个简单的<a href="http://www.bingo929.com/mytest/dragtest.html" target="_blank">拖拽图片预览的Demo</a></p>
<p>　　这时你如果用FireBug等类似调试工具查看DOM的话，会看到&lt;img&gt;标签的src属性是一个超长的文件二进制数据，所以如果DOM有很多这类图片，那就要当心浏览器性能了，因为这些数据极大地扩充的页面的代码量，而每次页面的reflow都会对浏览器形成很大的负担，So,如果这些图片还在DOM中，那就尽量不要做动画或任何重绘操作，如果真的要做就尽量让图片脱离文档流，让其绝对定位比较靠谱。</p>
<p style="background:#FFF7D1;padding:10px;color:#D6006D;">
补充：可以使用<strong><a href="https://developer.mozilla.org/en/DOM/window.URL.createObjectURL" target="_blank">window.URL.createObjectURL(file)</a></strong>来获取文件的URL(Chrome下用<strong>window.webkitURL.createObjectURL(file)</strong>)，这种方式获取的URL要比上面说的<strong>readAsDataURL</strong>简短很多。而且可以省去使用FileReader。下面是使用readAsDataURL与createObjectURL生成的&lt;img&gt;代码对比：
</p>
<p><a href="http://blog.bingo929.com/wp-content/uploads/2011/12/readasdataurl-vs-createobjecturl.png"><img src="http://blog.bingo929.com/wp-content/uploads/2011/12/readasdataurl-vs-createobjecturl-500x241.png" alt="readasdataurl-vs-createobjecturl" title="readAsDataURL与createObjectUrl对比" width="500" height="241" class="alignnone size-medium wp-image-3247" /></a></p>
<p>优化后的代码：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> dropHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #003366; font-weight: bold;">var</span> fileList <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span>　　<span style="color: #006600; font-style: italic;">//获取文件列表</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测是否是拖拽文件到页面的操作</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</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: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测文件是不是图片</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</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><br />
　<br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">URL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　<span style="color: #006600; font-style: italic;">//FF4+</span><br />
　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">URL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<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>window.<span style="color: #660066;">webkitURL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　<span style="color: #006600; font-style: italic;">//Chrome8+</span><br />
　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">webkitURL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
　　<span style="color: #006600; font-style: italic;">//实例化file reader对象</span><br />
　　<span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　　reader.<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>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span><br />
　　　oDragWrap.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　　<span style="color: #009900;">&#125;</span><br />
　　reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　需要注意的是，<strong>window.URL.createObjectURL</strong>是有生命周期的，也就意味着你每用此方法获取URL，其生命周期都会和DOM一样，它会单独占用内存，所以当删除图片或不再需要它是，记得用<strong>window.URL.revokeObjectURL(file)</strong>来释放其内存。当然，如果你没有释放，刷新页面也是可以释放的。</p>
<h3>AJAX上传图片(file.getAsBinary &amp; FormData)</h3>
<p>　　既然已经获取到了拖拽到web页面中图片的数据，下一步就是将其发送到服务器端了。</p>
<p>　　话说HTML5时代之前，AJAX传输文件二进制流数据是不可能完成的事情，而现在我们完全可以通过<strong>file.getAsBinary</strong>获取文件的二进制数据流，进而将其当做XHR的data数据传送到后端，8过由于Chrome不支持file的getAsBinary方法，FF3.6+支持此方法。所以Chrome就要另寻它法了，这时我们发现<a href="http://www.w3.org/TR/XMLHttpRequest2/" target="_blank">XMLHttpRequest Level 2</a>中的FormData接口完美解决了这个问题，它可以很快捷的模拟Form表单数据并通过AJAX发送至后端，FormData的支持情况是FF5及以上支持，Chrome12及以上支持。</p>
<p>　　<strong> file.getAsBinary</strong>获取文件流很简单，但是要想上传数据，就要模拟一下表单的数据格式了，首先看看模拟表单的js代码, <strong>FormData</strong>模拟表单数据时更是简洁，不用麻烦的去拼字符串，而是直接将数据append到formdata对象中即可：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://upload.renren.com/......'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> boundary <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-----------------------'</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> fileName <span style="color: #339933;">=</span> file.<span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
<br />
xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;post&quot;</span><span style="color: #339933;">,</span> url<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><br />
xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'multipart/form-data; boundary='</span> <span style="color: #339933;">+</span> boundary<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">FormData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　<span style="color: #006600; font-style: italic;">//Chrome12+</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> formData <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FormData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　formData.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'file'</span><span style="color: #339933;">,</span> file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　formData.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hostid'</span><span style="color: #339933;">,</span> userId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　formData.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'requestToken'</span><span style="color: #339933;">,</span> t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　data <span style="color: #339933;">=</span> formData<span style="color: #339933;">;</span><br />
<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>file.<span style="color: #660066;">getAsBinary</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　<span style="color: #006600; font-style: italic;">//FireFox 3.6+</span><br />
　data <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Disposition: form-data; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;name=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">'file'</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　unescape<span style="color: #009900;">&#40;</span>encodeURIComponent<span style="color: #009900;">&#40;</span>file.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Type: image/jpeg&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　file.<span style="color: #660066;">getAsBinary</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Disposition: form-data; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;name=<span style="color: #000099; font-weight: bold;">\&quot;</span>hostid<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　userId <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Disposition: form-data; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;name=<span style="color: #000099; font-weight: bold;">\&quot;</span>requestToken<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　t <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">'--'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>首先表单数据headers头信息需要以下两项：</p>
<ul>
<li>Content-Type ： 设置其为multipart/form-data来模拟表单数据</li>
<li>boundary ： 表单数据中的分隔符，用于分隔不同的文件或表单项，这是服务器端设置的格式。</li>
</ul>
<p>发送时的post数据类似这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">-------------------------</span><span style="color: #CC0000;">1323611763556</span><br />
Content<span style="color: #339933;">-</span>Disposition<span style="color: #339933;">:</span> form<span style="color: #339933;">-</span>data<span style="color: #339933;">;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;file&quot;</span><span style="color: #339933;">;</span> filename<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;4.jpg&quot;</span><br />
Content<span style="color: #339933;">-</span>Type<span style="color: #339933;">:</span> image<span style="color: #339933;">/</span>jpeg<br />
<br />
ÿØÿà�JFIF�...这里是文件二进制流...~iúoî­5P<span style="color: #339933;">%-</span>vãîHü 4QHgÿÙ<br />
<span style="color: #339933;">-------------------------</span><span style="color: #CC0000;">1323611763556</span><br />
Content<span style="color: #339933;">-</span>Disposition<span style="color: #339933;">:</span> form<span style="color: #339933;">-</span>data<span style="color: #339933;">;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;hostid&quot;</span><br />
<br />
<span style="color: #CC0000;">229421603</span><br />
<span style="color: #339933;">-----------------------------</span><span style="color: #CC0000;">1323612996486</span><br />
<br />
Content<span style="color: #339933;">-</span>Disposition<span style="color: #339933;">:</span> form<span style="color: #339933;">-</span>data<span style="color: #339933;">;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;requestToken&quot;</span><br />
<br />
<span style="color: #CC0000;">369009193</span><br />
<span style="color: #339933;">-------------------------</span><span style="color: #CC0000;">1323611763556</span><span style="color: #339933;">--</span></div></div>
<p>好了，现在文件上传成功后你就可以按照平常AJAX的操作来进行后续处理了。</p>
<p>最后，再来总结一下拖拽上传的技术要点：</p>
<ol>
<li><strong>监听拖拽</strong>：监听页面元素的拖拽事件，包括：<strong>dragenter、dragover、dragleave和drop</strong>，一定要将dragover的默认事件取消掉，不然无法触发drop事件。如需拖拽页面里的元素，需要给其添加属性draggable=&#8221;true&#8221;；</li>
<li><strong>获取拖拽文件</strong>：在drop事件触发后通过<strong>e.dataTransfer.files</strong>获取拖拽文件列表，<strong>.length</strong>属性获取文件数量，.type属性获取文件类型。</li>
<li><strong>读取图片数据并添加预览图</strong>：实例化<strong>FileReader</strong>对象，通过其<strong>readAsDataURL(file)</strong>方法获取文件二进制流，并监听其<strong>onload事件</strong>，将<strong>e.result</strong>赋值给img的src属性，最后将图片append到DOM中。</li>
<li><strong>发送图片数据</strong>：使用<strong>file.getAsBinary</strong> 和 <strong>FormDat</strong>a分别模拟表单数据AJAX提交文件流。</li>
</ol>
<p>　　OK,拖拽上传就讲到这里，欢迎大家一起探讨。也在这里推荐一下<a href="http://fed.renren.com" target="_blank"><strong>人人网FED的Blog</strong></a>，大家多多捧场，哈~</p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看HTML5最新资源、教程。</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——关注前端开发 &amp; Web UI设计<br />
本文链接:<a title="人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)" href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html">http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html</a><br />
<a title="人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)" href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html"> </a><a title="人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)" href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html"></a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>规范自己的JavaScript书写 &#8211; Dojo Javascript 编程规范</title>
		<link>http://blog.bingo929.com/dojo-javascript-style-guide.html</link>
		<comments>http://blog.bingo929.com/dojo-javascript-style-guide.html#comments</comments>
		<pubDate>Mon, 25 Oct 2010 07:46:49 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3082</guid>
		<description><![CDATA[前言 良好的JavaScript书写习惯的优点不言而喻，今天彬Go向大家推荐Dojo Javascript 编程规范，相当不错的 Javascript 编程风格规范，建议大家可以借鉴一下此规范编写 Javascript。感谢i.feelinglucky的翻... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/dojo-javascript-style-guide.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/10/title3.jpg" alt="Dojo Javascript 编程规范" title="规范自己的JavaScript书写" width="500" height="150" class="alignnone size-full wp-image-3112" /></a></p>
<h3>前言</h3>
<p>良好的JavaScript书写习惯的优点不言而喻，今天彬Go向大家推荐Dojo <a href="http://blog.bingo929.com/category/technology/javascript">Javascript</a> 编程规范，相当不错的 Javascript 编程风格规范，建议大家可以借鉴一下此规范编写 Javascript。感谢i.feelinglucky的翻译。<br />
<span id="more-3082"></span></p>
<h3>序</h3>
<p>Any violation to this guide is allowed if it enhances readability.</p>
<p>所有的代码都要变成可供他人容易阅读的。</p>
<h3>快读参考</h3>
<p>核心 API 请使用下面的风格：</p>
<table style="border:1px solid #ccc">
<tbody>
<tr>
<td><strong>结构</strong></td>
<td><strong>规则</strong></td>
<td><strong>注释</strong></td>
</tr>
<tr>
<td>模块</td>
<td>小写</td>
<td>不要使用多重语义（Never multiple words）</td>
</tr>
<tr>
<td>类</td>
<td>骆驼</td>
<td></td>
</tr>
<tr>
<td>公有方法</td>
<td>混合</td>
<td>其他的外部调用也可以使用 lower_case()，这样的风格</td>
</tr>
<tr>
<td>公有变量</td>
<td>混合</td>
<td></td>
</tr>
<tr>
<td>常量</td>
<td>骆驼 或 大写</td>
<td></td>
</tr>
</tbody>
</table>
<p>下面的虽然不是必要的，但建议使用：</p>
<table style="border:1px solid #ccc">
<tbody>
<tr>
<td><strong>结构</strong></td>
<td><strong>规则</strong></td>
</tr>
<tr>
<td>私有方法</td>
<td>混合，例子：<tt>_mixedCase</tt></td>
</tr>
<tr>
<td>私有变量</td>
<td>混合，例子：<tt>_mixedCase</tt></td>
</tr>
<tr>
<td>方法（method）参数</td>
<td>混合，例子：<tt>_mixedCase, mixedCase</tt></td>
</tr>
<tr>
<td>本地（local）变量</td>
<td>混合，例子：<tt>_mixedCase, mixedCase</tt></p>
<p><tt> </tt></td>
</tr>
</tbody>
</table>
<h3>命名规范</h3>
<p>1.变量名称 必须为 小写字母。<br />
2.类的命名使用骆驼命名规则，例如：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;Account<span style="color: #339933;">,</span> EventHandler</div></div>
<p>3.常量 必须 在对象（类）或者枚举变量的前部声明。枚举变量的命名必须要有实际的意义，并且其成员 必须 使用骆驼命名规则或使用大写：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> NodeTypes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Element <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; DOCUMENT<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>4.简写单词 不能使用 大写名称作为变量名：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getInnerHtml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> getXml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> XmlDocument</div></div>
<p>5.方法的命令 必须 为动词或者是动词短语：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">obj.<span style="color: #660066;">getSomeValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></div>
<p>6.公有类的命名 必须 使用混合名称（mixedCase）命名。<br />
7.CSS 变量的命名 必须 使用其对应的相同的公共类变量。<br />
8.私有类的变量属性成员 必须 使用混合名称（mixedCase）命名，并前面下下划线（_）。例如：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> MyClass <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><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> _buffer<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">doSomething</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><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>9.变量如果设置为私有，则前面 必须 添加下划线。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">this</span>._somePrivateVariable <span style="color: #339933;">=</span> statement<span style="color: #339933;">;</span></div></div>
<p>10.通用的变量 必须 使用与其名字一致的类型名称：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">setTopic<span style="color: #009900;">&#40;</span>topic<span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// 变量 topic 为 Topic 类型的变量</span></div></div>
<p>11.所有的变量名 必须 使用英文名称。<br />
12.变量如有较广的作用域（large scope），必须使用全局变量；此时可以设计成一个类的成员。相对的如作用域较小或为私有变量则使用简洁的单词命名。<br />
13.如果变量有其隐含的返回值，则避免使用其相似的方法：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 避免使用 getEventHandler()</span></div></div>
<p>14.公有变量必须清楚的表达其自身的属性，避免字义含糊不清，例如：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MouseEventHandler</div></div>
<p>，而非 MseEvtHdlr。<br />
　　请再次注意这条规定，这样做得的好处是非常明显的。它能明确的表达表达式所定义的含义。例如：<br />
　　　</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dojo.<span style="color: #660066;">events</span>.<span style="color: #660066;">mouse</span>.<span style="color: #660066;">Handler</span> <span style="color: #006600; font-style: italic;">// 而非 dojo.events.mouse.MouseEventHandler</span></div></div>
<p>15.类/构造函数 可以使用 扩展其基类的名称命名，这样可以正确、迅速的找到其基类的名称：<br />
　EventHandler<br />
　UIEventHandler<br />
　MouseEventHandler<br />
　　基类可以在明确描述其属性的前提下，缩减其命名：<br />
　　　MouseEventHandler as opposed to MouseUIEventHandler.</p>
<h3>特殊命名规范</h3>
<ol>
<li>术语 &#8220;get/set&#8221; 不要和一个字段相连，除非它被定义为私有变量。</li>
<li>前面加 &#8220;is&#8221; 的变量名 应该 为布尔值，同理可以为 &#8220;has&#8221;, &#8220;can&#8221; 或者 &#8220;should&#8221;。</li>
<li>术语 &#8220;compute&#8221; 作为变量名应为已经计算完成的变量。</li>
<li>术语 &#8220;find&#8221; 作为变量名应为已经查找完成的变量。</li>
<li>术语 &#8220;initialize&#8221; 或者 &#8220;init&#8221; 作为变量名应为已经实例化（初始化）完成的类或者其他类型的变量。</li>
<li>UI （用户界面）控制变量应在名称后加控制类型，例如： leftComboBox, TopScrollPane。</li>
<li>复数必须有其公共的名称约定（原文：Plural form MUST be used to name collections）。</li>
<li>带有 &#8220;num&#8221; 或者 &#8220;count&#8221; 开头的变量名约定为数字（对象）。</li>
<li>重复变量建议使用 &#8220;i&#8221;, &#8220;j&#8221;, &#8220;k&#8221; （依次类推）等名称的变量。</li>
<li>补充用语必须使用补充词，例如： get/set, add/remove, create/destroy, start/stop, insert/delete, begin/end, etc.</li>
<li>能缩写的名称尽量使用缩写。</li>
<li>避免产生歧义的布尔变量名称，例如：<br />
　　isNotError, isNotFound 为非法</li>
<li>错误类建议在变量名称后加上 &#8220;Exception&#8221; 或者 &#8220;Error&#8221;。</li>
<li>方法如果返回一个类，则应该在名称上说明返回什么；如果是一个过程，则应该说明做了什么。</li>
</ol>
<h3>文件</h3>
<p>缩进请使用 4 个空白符的制表位。<br />
如果您的编辑器支持 文件标签_（file tags），请加添如下的一行使我们的代码更容易阅读：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// vim:ts=4:noet:tw=0:</span></div></div>
<p>译注：老外用 VIM 编辑器比较多，此条可以选择遵循。</p>
<p>代码折叠必须看起来是完成并且是合乎逻辑的：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> someExpression <span style="color: #339933;">=</span> Expression1<br />
&nbsp; &nbsp; <span style="color: #339933;">+</span> Expression2<br />
&nbsp; &nbsp; <span style="color: #339933;">+</span> Expression3<span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> someObject.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; Expression1<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; Expression2<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; Expression3<br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>注：表达式的缩进与变量声明应为一致的。<br />
注：函数的参数应采用明确的缩进，缩进规则与其他块保持一致。</p>
<h3>变量</h3>
<ol>
<li>变量必须在声明初始化以后才能使用，即便是 NULL 类型。</li>
<li>变量不能产生歧义。</li>
<li>相关的变量集应该放在同一代码块中，非相关的变量集不应该放在同一代码块中。</li>
<li>变量应该尽量保持最小的生存周期。</li>
<li>循环/重复变量的规范：</li>
<ol>
<li>只有循环控制块的话，则必须使用 FOR 循环。</li>
<li>循环变量应该在循环开始前就被初始化；如使用 FOR 循环，则使用 FOR 语句初始化循环变量。</li>
<li>&#8220;do &#8230; while&#8221; 语句是被允许的。</li>
<li>&#8220;break&#8221; 和 &#8220;continue&#8221; 语句仍然允许使用（但请注意）。</li>
</ol>
<li>条件表达式</li>
<ol>
<li>应该尽量避免复杂的条件表达式，如有必要可以使用临时布尔变量。</li>
<li>The nominal case SHOULD be put in the &#8220;if&#8221; part and the exception in the &#8220;else&#8221; part of an &#8220;if&#8221; statement.</li>
<li>应避免在条件表达式中加入块。</li>
</ol>
<li>杂项</li>
<ol>
<li>尽量避免幻数（Magic numbers），他们应该使用常量来代替。</li>
<li>浮点变量必须指明小数点后一位（即使是 0）。</li>
<li>浮点变量必须指明实部，即使它们为零（使用 0. 开头）。</li>
</ol>
</ol>
<h3>布局</h3>
<h4>块</h4>
<ol>
<li>
普通代码段 应该 看起来如下：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isDone<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; isDone <span style="color: #339933;">=</span> moreToDo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
IF 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>someCondition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<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>someOtherCondition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
FOR 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>initialization<span style="color: #339933;">;</span> condition<span style="color: #339933;">;</span> update<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
WHILE 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isDone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; isDone <span style="color: #339933;">=</span> moreToDo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
DO &#8230; WHILE 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</li>
<li>
SWITCH 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">case</span> ABC<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;fallthrough</span><br />
<span style="color: #000066; font-weight: bold;">case</span> DEF<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
TRY &#8230; CATCH 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
单行的 IF &#8211; ELSE，WHILE 或者 FOR 语句也 必须 加入括号，不过他们可以这样写：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> statement<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> statement<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>intialization<span style="color: #339933;">;</span> condition<span style="color: #339933;">;</span> update<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> statement<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></div></div>
</li>
</ol>
<h3>空白</h3>
<ol>
<li>操作符 建议 使用空格隔开（包括三元操作符）。</li>
<li>下面的关键字 避免使用 空白隔开：
<ul>
<li>break</li>
<li>catch</li>
<li>continue</li>
<li>do</li>
<li>else</li>
<li>finally</li>
<li>for</li>
<li>function （如果为匿名函数，例如：var foo = function(){}; ）</li>
<li>if</li>
<li>return</li>
<li>switch</li>
<li>this</li>
<li>try</li>
<li>void</li>
<li>while</li>
<li>with</li>
</ul>
</li>
<li>下面的关键字必须使用空白隔开：
<ul>
<li>case</li>
<li>default</li>
<li>delete</li>
<li>function （如果为申明，例如：function foo(){}; ）</li>
<li>in</li>
<li>instanceof</li>
<li>new</li>
<li>throw</li>
<li>typeof</li>
<li>var</li>
</ul>
</li>
<li>逗号（,） 建议 使用空白隔开。</li>
<li>冒号（:） 建议 使用空白隔开。</li>
<li>点（.） 在后部 建议 使用空白隔开。</li>
<li>点（.） 避免 在前部使用空白。</li>
<li>函数调用和方法 避免 使用空白，例如： doSomething(someParameter); // 而非 doSomething (someParameter)</li>
<li>逻辑块 之间使用空行。</li>
<li>声明 建议 对齐使其更容易阅读。</li>
</ol>
<h3>注释</h3>
<ol>
<li>生涩的代码就 <i>没有必要</i> 添加注释了，首先您需要 <strong>重写</strong> 它们。 </li>
<li>所有的注释请使用英文。 </li>
<li>从已解决的方案到未开发的功能，注释 <i>必须</i> 与代码相关。 </li>
<li>大量的变量申明后 <i>必须</i> 跟随一段注释。 </li>
<li>注释需要说明的是代码段的用处，尤其是接下来的代码段。 </li>
<li>注释 <i>没有必要</i> 每行都添加。 </li>
</ol>
<h3>文档</h3>
<p>下面提供了一些基本的函数或者对象的描述方法：</p>
<ul>
<li><strong>总结（summary）</strong>: 简短的表述此函数或者对象实现的目的 </li>
<li><strong>描述（description）</strong>: 对于此函数或者类的简短的描述 </li>
<li><strong>返回（return）</strong>: 描述此函数返回什么（并不包括返回类型） </li>
</ul>
<h3>基本函数信息</h3>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// summary: Soon we will have enough treasure to rule all of New Jersey.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// description: Or we could just get a new roomate.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Look, you go find him. &nbsp;He don't yell at you.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;All I ever try to do is make him smile and sing around</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;him and dance around him and he just lays into me.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;He told me to get in the freezer 'cause there was a carnival in there.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// returns: &nbsp;Look, a Bananarama tape!</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>对象函数信息</h3>
<p>没有返回值描述</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// summary: Dingle, engage the rainbow machine!</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// description: </span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tell you what, I wish I was--oh my g--that beam,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;coming up like that, the speed, you might wanna adjust that.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;It really did a number on my back, there. I mean, and I don't</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wanna say whiplash, just yet, cause that's a little too far,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;but, you're insured, right?</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>函数的声明</h3>
<p>在有的情况下，对于函数的调用和声明是隐义（invisible）的。在这种情况下，我们没有办法在函数中加入说明等（供程序调用）。如果您遭遇了这种情况，您可以使用一个类来封装函数。</p>
<p>注：此此方法只能在函数没有初始化的参数情况下。如过不是，则它们会被忽略。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dojo.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// summary: Phew, this sure is relaxing, Frylock.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// description: </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Thousands of years ago, before the dawn of</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;man as we knew him, there was Sir Santa of Claus: an</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ape-like creature making crude and pointless toys out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;of dino-bones, hurling them at chimp-like creatures with</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;crinkled hands regardless of how they behaved the</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;previous year.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// returns: Unless Carl pays tribute to the Elfin Elders in space.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>参数<span style="color: #339933;">&lt;/</span>h3<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>ol<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>简单类型<br />
　　　简单的类型的参数可以直接在函数参数定义中注释说明。<br />
<span style="color: #009900;">&#91;</span>cc lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*String*/</span> foo<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*int*/</span> bar<span style="color: #009900;">&#41;</span>...</div></div>
</li>
<li>可变类型参数<br />
　　下面是几个修饰符供参考：
<ul>
<li>? 可选参数</li>
<li>&#8230; 说面参数范围不确定</li>
<li> 数组</li>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*String?*/</span> foo<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*int...*/</span> bar<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*String[]*/</span> baz<span style="color: #009900;">&#41;</span>...</div></div>
<li>全局参数描述<br />
　如果你想增加一个描述，你可以将它们移至初始化块。
</ol>
<p>基本信息格式为： *关键字* 描述字段 （ *key* Descriptive sentence）<br />
参数和变量的格式为： *关键字* ~*类型*~ 描述字段 （ *key* ~*type*~ Descriptive sentence）<br />
注： *关键字* 和 ~*类型*~ 可以使用任何字母和数字表述。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> bar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// foo: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;used for being the first parameter</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// bar: int</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;used for being the second parameter</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>变量</h3>
<p>由于实例变量、原型变量和外部变量的声明是一致的，所以有很多的方法声明、修改变量。具体的如何定义和定位应在变量最先出现的位置指明变量的名称、类型、作用域等信息。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// myString: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// times: int</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;How many times to print myString</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// separator: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;What to print out in between myString*</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myString</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;placeholder text&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">times</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>myString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myString</span> <span style="color: #339933;">=</span> myString<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</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><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>int i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">times</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">separator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
foo.<span style="color: #660066;">separator</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;=====&quot;</span><span style="color: #339933;">;</span></div></div>
<h4>对象中的变量注释</h4>
<p>应使用和对象值和方法一致的标注方式，比如在他们声明的时候：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// key: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A simple value</span><br />
&nbsp; &nbsp; key<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// key2: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Another simple value</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>返回值</h3>
<p>因为函数可以同时返回多个不同（类型）的值，所以应每个返回值之后加入返回类型的注释。注释在行内注释即可，如果所有的返回值为同一类型，则指明返回的类型；如为多个不同的返回值，则标注返回类型为&#8221;mixed&#8221;。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;You passed argument(s)&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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: #006600; font-style: italic;">// Boolean</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>伪代码（有待讨论）</h3>
<p>有时候您需要在函数或者类中添加对于此函数和类的功能性流程描述。如果您打算这样做，您可以使用 /*======== （= 字符最好出现 5 次或者更多），这样做的好处就是可以不用将这些东西加入代码（译注：原作者的意思可能为代码管理系统）。</p>
<p>这样看起来在 /*===== 和 =====*/ 会有非常长的一段注释，等待功能调整完毕以后就可以考虑是否删除。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/*=====<br />
module.pseudo.kwArgs = {<br />
&nbsp; &nbsp; &nbsp; &nbsp; // url: String<br />
&nbsp; &nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;The location of the file<br />
&nbsp; &nbsp; url: &quot;&quot;,<br />
&nbsp; &nbsp; // mimeType: String<br />
&nbsp; &nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text/html, text/xml, etc<br />
&nbsp; &nbsp; mimeType: &quot;&quot;<br />
}<br />
=====*/</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*module.pseudo.kwArgs*/</span> kwArgs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>kwArgs.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>kwArgs.<span style="color: #660066;">mimeType</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>原文链接： http://dojotoolkit.org/developer/StyleGuide<br />
翻译（Translated by）：i.feelinglucky{at}gmail.com from http://www.gracecode.com</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/dojo-javascript-style-guide.html/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>HTML5 WebSockets 基础使用教程</title>
		<link>http://blog.bingo929.com/html5-websockets.html</link>
		<comments>http://blog.bingo929.com/html5-websockets.html#comments</comments>
		<pubDate>Mon, 09 Aug 2010 17:30:28 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[WebSocket]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3003</guid>
		<description><![CDATA[　　HTML5之中一个很酷的新特性就是WebSockets，它可以让我们无需AJAX请求即可与服务器端对话。今天彬Go将让大家通过Php环境的服务器端运行WebSocket，创建客户端并通过WebSockets协议发送和接收服... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/html5-websockets.html"><img class="alignnone size-full wp-image-3004" title="html5-websockets-教程" src="http://blog.bingo929.com/wp-content/uploads/2010/08/html5-websockets-title.jpg" alt="html5-websockets-教程" width="500" height="150" /></a></p>
<p>　　<a href="http://blog.bingo929.com/category/technology/html5">HTML5</a>之中一个很酷的新特性就是WebSockets，它可以让我们无需AJAX请求即可与服务器端对话。今天彬Go将让大家通过Php环境的服务器端运行WebSocket，创建客户端并通过WebSockets协议发送和接收服务器端信息。</p>
<p><span id="more-3003"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下HTML5相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》</p>
<h3>什么是WebSockets？</h3>
<p>　　<strong>WebSockets是在一个(TCP)接口进行双向通信的技术，PUSH技术类型。同时WebSockets仍将基于W3C标准，目前为止，Chrome和Safari的最新版本浏览器已经支持WebSockets了。</strong></p>
<h3>WebSockets将会替代什么？</h3>
<p>　　WebSockets可以替代Long Polling(PHP服务端推送技术)，这是一个有趣的概念。客户端发送一个请求到服务器，现在，服务器端并不会响应还没准备好的数据，它会保持连接的打开状态直到最新的数据准备就绪发送，之后客户端收到数据，然后发送另一个请求。这有它的好处：减少任一连接的延迟，当一个连接已经打开时就不需要创建另一个新的连接。但是Long-Polling并不是什么花俏技术，他仍有可能发生请求暂停，因此会需要建立新的连接。</p>
<p>　　一些AJAX应用使用上述技术-这经常是归因于低资源利用。</p>
<p>　　试想一下，如果服务器在早晨会自启动并发送数据到那些希望接收而不用提前建立一些连接端口的客户端，这是一件多棒的事情啊！欢迎来到PUSH技术的世界！</p>
<h3>第一步：搞定WebSocket服务器</h3>
<p>　　这篇教程会把更多的精力放在客户端的创建而不是服务器端的执行等操作。</p>
<p>　　我使用基于windows 7的<a href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a>来实现本地运行PHP。<a href="http://code.google.com/p/phpwebsockets/" target="_blank">phpwebsockets</a>是PHP WebSocket服务器。(以我的经验这个版本存在一些小问题，我已对它做了些修改并上传源文件共享给大家)下面的这些不同版本也可以实现WebSocket，如果某个不能用，你可以试试其它版本或者继续看下面的教程。</p>
<p>　　<a href="http://jwebsocket.org/" target="_blank">jWebSocket</a> (Java)<br />
　　<a href="http://github.com/gimite/web-socket-ruby" target="_blank">web-socket-ruby</a> (ruby)<br />
　　<a href="http://github.com/LearnBoost/Socket.IO-node" target="_blank">Socket IO-node</a> (node.js)</p>
<h4>启动Apache服务器</h4>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/08/1.jpg" alt="html5-websocket-apache" title="html5-websocket-apache" width="451" height="383" class="alignnone size-full wp-image-3019" /></p>
<h3>第二步：修改URLs和端口</h3>
<p>　　根据你之前的安装修改服务器，下面是setup.class.php中的例子：</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #000088;">$port</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">8000</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> &nbsp;<br />
<span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createSocket</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span><span style="color: #000088;">$port</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　浏览文件并在适当情况下进行更改。</p>
<h3>第三步：开始创建客户端</h3>
<p>　　下面来创建基本模板，这是我的client.php文件：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>WebSockets Client<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>WebSockets Client<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chatLog&quot;</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- #chatLog --&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;examples&quot;</span>&gt;</span>e.g. try 'hi', 'name', 'age', 'today'<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;disconnect&quot;</span>&gt;</span>Disconnect<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- #container --&gt;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span>​</div></div>
<p>　　我们已经创建里基本模板：一个chat log容器，一个input输入框和一个断开连接的按钮。</p>
<h3>第四步：添加一些CSS</h3>
<p>　　没什么花俏代码，只是处理一下标签的样式。</p>
<div class="codecolorer-container css blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">body <span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:Arial</span><span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #cc00cc;">#container</span><span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span> <span style="color: #993333;">solid</span> grey<span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <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> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <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> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #cc00cc;">#chatLog</span><span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <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: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #cc00cc;">#chatLog</span> p <span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <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> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #6666ff;">.event</span> <span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #6666ff;">.warning</span><span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span></div></div>
<h3>第五步：WebSocket事件</h3>
<p>首先让我们尝试并理解WebSocket事件的概念：<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/events-500x250.jpg" alt="WebSocket-事件" title="WebSocket-事件" width="500" height="250" class="alignnone size-medium wp-image-3028" /></p>
<p><strong>WebSocket事件：</strong><br />
我们将使用三个WebSocket事件：<br />
onopen: 当接口打开时<br />
onmessage: 当收到信息时<br />
onclose: 当接口关闭时</p>
<p>我们如何来实现呢？<br />
首先创建WebSocket对象</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>然后向下面这样检测事件</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket.<span style="color: #660066;">onopen</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;<br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Socket has been opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>当我们收到信息时这样做：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Awesome! &nbsp;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>但我们还是尽量避免使用alert，现在我们可以把我们学的东西整合到客户端页面中了。</p>
<h3>第六步：JavaScript</h3>
<p>　　首先我们将代码放到<a href="http://blog.bingo929.com/tag/jquery">jQuery</a> 的 document.ready函数中，然后我们还要检查用户的浏览器是否支持WebSocket。如果不支持，我们就添加一个链向Chrome浏览器页面的链接。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<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> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;WebSocket&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog, input, button, #examples'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fast&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Oh no, you need a browser that supports WebSockets. How about &lt;a href=&quot;http://www.google.com/chrome&quot;&gt;Google Chrome&lt;/a&gt;?&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//The user has WebSockets &nbsp;</span><br />
&nbsp; <br />
&nbsp; &nbsp; connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//the connect function code is below &nbsp;</span><br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>　　如你所见，如果用户浏览器支持WebSocket，我们将执行connect()函数。这里是核心功能，我们将开始创建open、close和receive事件。</p>
<p>我们将在我们的服务器定义URL。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> socket<span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #339933;">;</span></div></div>
<p>　　你可能会发现URL中怎么没有http？恩，是的，这是一个WebSocket URL，使用了不同的协议。下面是URL分解图示：<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/url-500x125.jpg" alt="html5-websocket-url" title="html5-websocket-url" width="500" height="125" class="alignnone size-medium wp-image-3035" /></p>
<p>　　下面让我们继续完成connect()函数，我们将代码放入try/catch块，这样如果代码出现问题，我们能让用户知道。我们创建WebSocket，并将信息传递到message()函数，之后会做讲解。我们创建我们的onopen、onmessage和onclose函数. 需要注意的是我们为用户提供了端口状态，这并不是必需的，但我们把它放进来主要是为了方便调试。</p>
<p>CONNECTING = 0<br />
OPEN = 1<br />
CLOSED = 2</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket<span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span>host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onopen</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;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (open)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;message&quot;&gt;Received: '</span><span style="color: #339933;">+</span>msg.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onclose</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;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (Closed)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Error'</span><span style="color: #339933;">+</span>exception<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　message()函数很简单, 它将我们想展现给用户的文本填入chat log容器内。 我们在socket事件函数中为段落(&lt;p>)标签创建适当的class，我们在message函数中只有一个段落结束标签。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> message<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>目前的成果</h3>
<p>　　如果你已按上面教程按部就班的做的话，很好，我们已经创建了 <a href="http://blog.bingo929.com/tag/divcss">HTML/CSS</a> 模板、创建并建立Websocket连接、通过创建连接保持用户的进展更新。</p>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/08/page.jpg" alt="HTML5-websocket" title="HTML5-websocket" width="369" height="209" class="alignnone size-full wp-image-3042" /></p>
<h3>第七步：发送数据</h3>
<p>　　现在我们已经有了提交按钮，但我们还需要监听用户按下键盘的事件，并运行send函数，下面的&#8217;13&#8242;便是回车键对应的ASCII码。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</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> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'13'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>下面是send()函数：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</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: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt;Please enter a message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Sent: '</span><span style="color: #339933;">+</span>text<span style="color: #009900;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt; Error:'</span> <span style="color: #339933;">+</span> exception<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>下面我们需要：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>　　那些额外的代码做了以下工作：检测用户是否什么都没输入却仍点击返回、清空input输入框、执行message()函数。</p>
<h3>关闭Socket</h3>
<p>　　关闭Socket操作相当简单，添加对断开连接按钮的click事件监听就可以。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#disconnect'</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> &nbsp;<br />
&nbsp; &nbsp; socket.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/08/close.jpg" alt="websocket-close" title="websocket-close" width="324" height="77" class="alignnone size-full wp-image-3053" /></p>
<h3>完整<a href="http://blog.bingo929.com/category/technology/javascript">JavaScript</a>代码</h3>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<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> &nbsp;<br />
&nbsp; <br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;WebSocket&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog, input, button, #examples'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fast&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Oh no, you need a browser that supports WebSockets. How about &lt;a href=&quot;http://www.google.com/chrome&quot;&gt;Google Chrome&lt;/a&gt;?&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//The user has WebSockets &nbsp;</span><br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket<span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span>host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onopen</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;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (open)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;message&quot;&gt;Received: '</span><span style="color: #339933;">+</span>msg.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onclose</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;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (Closed)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Error'</span><span style="color: #339933;">+</span>exception<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</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: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt;Please enter a message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Sent: '</span><span style="color: #339933;">+</span>text<span style="color: #009900;">&#41;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> message<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</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> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'13'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#disconnect'</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> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;socket.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">//End connect &nbsp;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">//End else &nbsp;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>第九步：运行WebSocket服务器</h3>
<p>我们要输入一些命令行，XAMPP提供了比较方便的shell选项。点击XAMPP控制面板的&#8217;shell&#8217;按钮并输入：<br />
php -q path\to\server.php<br />
现在你已经运行了WebSocket服务器！<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/2-500x166.jpg" alt="HTML5-webSocket" title="HTML5-webSocket" width="500" height="166" class="alignnone size-medium wp-image-3056" /></p>
<h3>大功告成！</h3>
<p>　　当页面读取后，将尝试创建一个WebSocket连接，然后用户可以输入信息并从服务器接收信息。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/example.jpg" alt="html5-websocket-教程实例" title="html5-websocket-教程实例" width="484" height="433" class="alignnone size-full wp-image-3057" /></p>
<p>　　感谢大家耐心阅读本教程，希望你能从中学到有用的知识，HTML5 WebSocket的确令人兴奋不已！大家可以通过<a href="http://dev.w3.org/html5/websockets/" target="_blank">The WebSocket API</a>了解HTML5 WebSocket的最新动态。</p>
<p><strong><a href="http://www.bingo929.com/mytest/websocket_source_files.rar">点击这里下载源文件</a></strong></p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看HTML5最新资源、教程。 </p>
<p>英文原文:<a title="Start Using HTML5 WebSockets Today" href="http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/" target="_blank">Start Using HTML5 WebSockets Today</a><br />
翻译原文:<a title="HTML5 WebSockets 基础使用教程" href="http://blog.bingo929.com/html5-websockets.html">HTML5 WebSockets 基础使用教程</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="HTML5 WebSockets 基础使用教程" href="http://blog.bingo929.com/html5-websockets.html">http://blog.bingo929.com/html5-websockets.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/html5-websockets.html/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>关于HTML 5 canvas 的基础教程</title>
		<link>http://blog.bingo929.com/html-5-canvas-the-basics-html5.html</link>
		<comments>http://blog.bingo929.com/html-5-canvas-the-basics-html5.html#comments</comments>
		<pubDate>Tue, 15 Jun 2010 17:31:30 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2947</guid>
		<description><![CDATA[HTML 5 规范引进了很多新特性，其中最令人期待的之一就是 canvas 元素。HTML 5 canvas 提供了通过 JavaScript 绘制图形的方法，此方法使用简单但功能强大。每一个canvas 元素都有一个&#8221;上下文( conte... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/1.jpg" alt="" title="HTML5 Canvas 基础教程" width="500" height="150" class="alignnone size-full wp-image-2969" /></a></p>
<p id="introduction"><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/">HTML 5 规范</a>引进了很多新特性，其中最令人期待的之一就是 <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">canvas 元素</a>。<a href="http://blog.bingo929.com/category/technology/html5">HTML 5</a> canvas 提供了通过 <a href="http://blog.bingo929.com/category/technology/javascript">JavaScript</a> 绘制图形的方法，此方法使用简单但功能强大。每一个canvas 元素都有一个&#8221;上下文( context )&#8221; (想象成绘图板上的一页)，在其中可以绘制任意图形。浏览器支持多个 canvas 上下文，并通过不同的<abbr title="Application Programmable         Interfaces"> API</abbr> 提供图形绘制功能。</p>
<p><span id="more-2947"></span></p>
<p>大部分的浏览器都支持 2D canvas 上下文——包括 <a href="http://opera.com/">Opera</a>, <a href="http://mozilla.com/">Firefox</a>, <a href="http://konqueror.org/">Konqueror</a> 和 <a href="http://apple.com/safari">Safari</a>。而且某些版本的 Opera 还支持 3D canvas ，Firefox 也可以通过插件形式支持 3D canvas ：</p>
<ul>
<li><a href="http://labs.opera.com/news/2008/11/25/">下载支持 3D canvas, HTML video 和 File I/O 的 Opera</a></li>
<li><a href="http://my.opera.com/timjoh/blog/2007/11/13/taking-the-canvas-to-another-dimension">关于 Opera 3D canvas 上下文的文章</a></li>
<li><a href="http://blog.vlad1.com/2007/11/26/canvas-3d-gl-power-web-style/">关于 Firefox 3D canvas<br />
        上下文的文章</a></li>
</ul>
<p>本文介绍 2D canvas<br />
    基础以及如何使用基本 canvas 函数，如线条、形状、图像和文字等。为了理解此文章，你最好了解 JavaScript 基础知识。</p>
<p class="note"><a href="http://dev.opera.com/articles/view/html-5-canvas-the-basics/canvas-primer.zip">可以点击此处批量下载本文实例代码</a></p>
<h3>目录</h3>
<ul>
<li><a href="#introduction">简述</a></li>
<li><a href="#thebasicsofusingcanvas">canvas 基础</a></li>
<li><a href="#the2dcontextapi"> 2D context API</a>
<ul>
<li><a href="#basiclinesandstrokes">基本线条</a></li>
<li><a href="#paths">路径</a></li>
<li><a href="#insertingimages">插入图像</a></li>
<li><a href="#pixelbasedmanipulation">像素级操作</a></li>
<li><a href="#text">文字</a></li>
<li><a href="#shadows">阴影</a></li>
<li><a href="#gradients">颜色渐变</a></li>
</ul>
</li>
<li><a href="#summary">小节</a></li>
</ul>
<h3 id="thebasicsofusingcanvas">canvas 基础</h3>
<p>创建 canvas  的方法很简单，只需要在 HTML 页面中添加 &lt;canvas&gt; 元素：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myCanvas&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;150&quot;</span>&gt;</span><br />
Fallback content, in case the browser does not support Canvas.<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span></div></div>
<p>为了能在 JavaScript 中引用元素，最好给元素设置 ID ；也需要给 canvas 设定高度和宽度。</p>
<p>创建好了画布后，让我们来准备画笔。要在画布中绘制图形需要使用 JavaScript 。首先通过 getElementById 函数找到 canvas<br />
    元素，然后初始化上下文。之后可以使用上下文 API 绘制各种图形。下面的脚本在 canvas 中绘制一个矩形 (<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-using-canvas.html">点击此处查看效果</a>)：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Get a reference to the element.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myCanvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Always check for properties 和 methods, to make sure your code doesn't break </span><br />
<span style="color: #006600; font-style: italic;">// in other browsers.</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> elem.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Get the 2d context.</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Remember: you can only initialize one context per element.</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> elem.<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><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// You are done! Now you can draw your first rectangle.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// You only need to provide the (x,y) coordinates, followed by the width and </span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// height dimensions.</span><br />
&nbsp; &nbsp; 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;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>可以把上面代码放置在文档 head 部分中，或者放在外部文件中。</p>
<h3 id="the2dcontextapi">2D context API</h3>
<p>介绍了如何创建 canvas 后，让我们来看看 2D canvas API，看看能用这些函数做些什么。</p>
<h4 id="basiclinesandstrokes">基本线条</h4>
<p>上面的例子中展示了绘制矩形是多么简单。</p>
<p>通过 <var>fillStyle</var> 和 <var>strokeStyle</var> 属性可以轻松的设置矩形的填充和线条。颜色值使用方法和 <abbr title="Cascading Style Sheet">CSS </abbr>一样：十六进制数、<abbr title="red, green, blue">rgb</abbr>()、<abbr title="red, green, blue, alpha">rgba</abbr>() 和 <abbr title="hue, saturation, light, alpha">hsla</abbr>()（ 若浏览器支持，如<a href="http://dev.opera.com/articles/view/presto-2-2-and-opera-10-a-first-look/"> Opera 10</a> 和 Firefox 3)。</p>
<p>通过 fillRect 可以绘制带填充的矩形。使用 strokeRect 可以绘制只有边框没有填充的矩形。如果想清除部分 canvas 可以使用 clearRect。上述三个方法的参数相同：<var>x</var>, <var>y</var>, <var>width</var>, <var>height</var>。前两个参数设定 (x,y) 坐标，后两个参数设置矩形的高度和宽度。</p>
<p>可以使用 <var>lineWidth</var> 属性改变线条粗细。让我们看看<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-rects.html">使用了fillRect,strokeRect clearRect 和其他的例子</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">context.<span style="color: #660066;">fillStyle</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// blue</span><br />
context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red</span><br />
context.<span style="color: #660066;">lineWidth</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Draw some rectangles.</span><br />
context.<span style="color: #660066;">fillRect</span> &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">clearRect</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>此例子效果图见图1.</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-rects.html"><img alt="Example render of fillRect, strokeRect and clearRect." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example-.jpg" /><br /> <br />
</a></p>
<p class="comment">图 1: fillRect, strokeRect 和<br />
    clearRect效果图</p>
<h3 id="paths">路径</h3>
<p>通过 canvas 路径（path）可以绘制任意形状。可以先绘制轮廓，然后绘制边框和填充。创建自定义形状很简单，使用beginPath()开始绘制，然后使用直线、曲线和其他图形绘制你的图形。绘制完毕后调用 fill 和 stroke 即可添加填充或者设置边框。调用 closePath() 结束自定义图形绘制。</p>
<p>下面是一个绘制<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-triangle.html">三角形的例子</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Set the style properties.</span><br />
context.<span style="color: #660066;">fillStyle</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">lineWidth</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// Start from the top-left point.</span><br />
context.<span style="color: #660066;">moveTo</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: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// give the (x,y) coordinates</span><br />
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">lineTo</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: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Done! Now fill the shape, 和 draw the stroke.</span><br />
<span style="color: #006600; font-style: italic;">// Note: your shape will not be visible until you call any of the two methods.</span><br />
context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>其效果图见图2.</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-triangle.html"><img alt="Triangle" src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example0.jpg" /><br />
</a></p>
<p class="comment">图 2: 三角形</p>
<p>另一个较负责的例子中<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-paths.html">使用了直线、曲线和圆弧</a>。</p>
<h3 id="insertingimages">插入图像</h3>
<p>drawImage 方法允许在 canvas 中插入其他图像<br />
    ( img 和 canvas 元素) 。在 Opera 中可以再 canvas 中绘制 SVG 图形。此方法比较复杂，可以有3个、5个或9个参数：</p>
<ul>
<li>3个参数：最基本的 drawImage 使用方法。一个参数指定图像位置，另两个参数设置图像在 canvas中的位置。</li>
<li>5个参数：中级的 drawImage 使用方法，包括上面所述3个参数，加两个参数指明插入图像宽度和高度 (如果你想改变图像大小)。</li>
<li>9个参数：最复杂drawImage 杂使用方法，包含上述5个参数外，另外4个参数设置源图像中的位置和高度宽度。这些参数允许你在显示图像前动态裁剪源图像。</li>
</ul>
<p>下面是上述<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-drawimage.html">三个使用方法</a>的例子：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Three arguments: the element, destination (x,y) coordinates.</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img_elem<span style="color: #339933;">,</span> dx<span style="color: #339933;">,</span> dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Five arguments: the element, destination (x,y) coordinates, and destination </span><br />
<span style="color: #006600; font-style: italic;">// width and height (if you want to resize the source image).</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img_elem<span style="color: #339933;">,</span> dx<span style="color: #339933;">,</span> dy<span style="color: #339933;">,</span> dw<span style="color: #339933;">,</span> dh<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Nine arguments: the element, source (x,y) coordinates, source width and </span><br />
<span style="color: #006600; font-style: italic;">// height (for cropping), destination (x,y) coordinates, and destination width </span><br />
<span style="color: #006600; font-style: italic;">// and height (resize).</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img_elem<span style="color: #339933;">,</span> sx<span style="color: #339933;">,</span> sy<span style="color: #339933;">,</span> sw<span style="color: #339933;">,</span> sh<span style="color: #339933;">,</span> dx<span style="color: #339933;">,</span> dy<span style="color: #339933;">,</span> dw<span style="color: #339933;">,</span> dh<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>其效果见图3.</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-drawimage.html"><img alt="Example rendering of drawImage." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example1.jpg" /><br />
</a></p>
<p class="comment">图 3: drawImage 效果图。</p>
<h3 id="pixelbasedmanipulation">像素级操作</h3>
<p>2D Context API 提供了三个方法用于像素级操作：createImageData, getImageData, 和<br />
    putImageData。</p>
<p>ImageData对象保存了图像像素值。每个对象有三个属性: <var>width</var>, <var>height</var> 和<br />
    <var>data</var>。<var>data</var> 属性类型为<span class="Type">CanvasPixelArray</span>，用于储存<var>width</var>*<var>height</var>*4个像素值。每一个像素有RGB值和透明度alpha值(其值为 0 至 255，包括alpha在内!)。像素的顺序从左至右，从上到下，按行存储。</p>
<p>为了更好的理解其原理，让我们来看一个例子——<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html">绘制红色矩形</a></p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Create an ImageData object.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> imgd <span style="color: #339933;">=</span> context.<span style="color: #660066;">createImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> pix <span style="color: #339933;">=</span> imgd.<span style="color: #660066;">data</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Loop over each pixel 和 set a transparent red.</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> n <span style="color: #339933;">=</span> pix.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i &nbsp;<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red channel</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">127</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alpha channel</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Draw the ImageData object at the given (x,y) coordinates.</span><br />
context.<span style="color: #660066;">putImageData</span><span style="color: #009900;">&#40;</span>imgd<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>注意: 不是所有浏览器都实现了 createImageData。在支持的浏览器中，需要通过 getImageData 方法获取 ImageData 对象。请参考<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html">示例代码</a>。</p>
<p>通过 ImageData 可以完成很多功能。如可以实现图像滤镜，或可以实现数学可视化 (如分形和其他特效)。下面特效实现了<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata.html">简单的颜色反转滤镜</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Get the CanvasPixelArray from the given coordinates and dimensions.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> imgd <span style="color: #339933;">=</span> context.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>x<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>y<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>width<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>height<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> pix <span style="color: #339933;">=</span> imgd.<span style="color: #660066;">data</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Loop over each pixel and invert the color.</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> pix.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i &nbsp;<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> pix<span style="color: #009900;">&#91;</span>i &nbsp;<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// green</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// blue</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// i+3 is alpha (the fourth element)</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Draw the ImageData at the given (x,y) coordinates.</span><br />
context.<span style="color: #660066;">putImageData</span><span style="color: #009900;">&#40;</span>imgd<span style="color: #339933;">,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>x<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>y<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>图 4 显示了使用此滤镜后的 Opera<br />
    图像 (图 3是原图)。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata.html"><img alt="Example rendering of the invert color filter." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example2.jpg" /><br />
</a></p>
<p class="comment">图 4: 颜色反转滤镜</p>
<h3 id="text">文字</h3>
<p>虽然最近的 WebKit 版本和 Firefox 3.1 nightly build 才开始支持 Text API ，为了保证文章完整性我决定仍在这里介绍文字 API 。</p>
<p>context 对象可以设置以下 text 属性：</p>
<ul>
<li>font：文字字体，同<abbr title="Cascading Style Sheet"> CSS</abbr> font-family 属性</li>
<li>textAlign：文字水平对齐方式。可取属性值: start, end, left,right, center。默认值:start.</li>
<li>textBaseline：文字竖直对齐方式。可取属性值：top, hanging, middle,alphabetic, ideographic, bottom。默认值：alphabetic.</li>
</ul>
<p>有两个方法可以绘制文字：  fillText 和 strokeText。第一个绘制带 <var>fillStyle</var> 填充的文字，后者绘制只有 <var>strokeStyle</var> 边框的文字。两者的参数相同：要绘制的文字和文字的位置(x,y) 坐标。还有一个可选选项——最大宽度。如果需要的话，浏览器会缩减文字以让它适应指定宽度。</p>
<p>文字对齐属性影响文字与设置的<br />
    (x,y) 坐标的相对位置。</p>
<p>下面是一个<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-text.html">在 canvas 中绘制&#8221;hello world&#8221; 文字的例子</a></p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">context.<span style="color: #660066;">fillStyle</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">font</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'italic 30px sans-serif'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">textBaseline</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">fillText</span> &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello world!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">font</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 30px sans-serif'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello world!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>图 5 是其效果图。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-text.html"><img alt="Example text render." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example3.jpg" /><br />
</a></p>
<p class="comment">图 5: 文字效果</p>
<h3 id="shadows">阴影</h3>
<p>目前只有 Konqueror 和 Firefox 3.1 nightly build 支持 Shadows API 。API 的属性为：</p>
<ul>
<li>shadowColor：阴影颜色。其值和 CSS 颜色值一致。</li>
<li>shadowBlur：设置阴影模糊程度。此值越大，阴影越模糊。其效果和 Photoshop 的高斯模糊滤镜相同。</li>
<li>shadowOffsetX 和 shadowOffsetY：阴影的 x 和 y 偏移量，单位是像素。</li>
</ul>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-shadows.html">下面是 canvas 阴影的例子</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">context.<span style="color: #660066;">shadowOffsetX</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">shadowOffsetY</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">shadowBlur</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">shadowColor</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rgba(255, 0, 0, 0.5)'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">fillStyle</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">fillRect</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;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>其效果见图 6。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-shadows.html"><img alt="Example canvas shadow - a blue rectangle with a red shadow." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example4.jpg" /><br /> <br />
</a></p>
<p class="comment">图 6: canvas 阴影效果——蓝色矩形，红色阴影</p>
<h3 id="gradients">颜色渐变</h3>
<p>除了 CSS 颜色， fillStyle 和 strokeStyle 属性可以设置为 CanvasGradient 对象。——通过 CanvasGradient可以为线条和填充使用颜色渐变。</p>
<p>欲创建 CanvasGradient 对象，可以使用两个方法：createLinearGradient 和 createRadialGradient。前者创建线性颜色渐变，后者创建圆形颜色渐变。</p>
<p>创建颜色渐变对象后，可以使用对象的 addColorStop 方法添加颜色中间值。</p>
<p>下面的代码演示了颜色渐变使用方法：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// You need to provide the source 和 destination (x,y) coordinates </span><br />
<span style="color: #006600; font-style: italic;">// for the gradient (from where it starts 和 where it ends).</span><br />
<span style="color: #003366; font-weight: bold;">var</span> gradient1 <span style="color: #339933;">=</span> context.<span style="color: #660066;">createLinearGradient</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Now you can add colors in your gradient.</span><br />
<span style="color: #006600; font-style: italic;">// The first argument tells the position for the color in your gradient. The </span><br />
<span style="color: #006600; font-style: italic;">// accepted value range is from 0 (gradient start) to 1 (gradient end).</span><br />
<span style="color: #006600; font-style: italic;">// The second argument tells the color you want, using the CSS color format.</span><br />
gradient1.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #3366CC;">'#f00'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red</span><br />
gradient1.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#ff0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// yellow</span><br />
gradient1.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #3366CC;">'#00f'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// blue</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// For the radial gradient you also need to provide source</span><br />
<span style="color: #006600; font-style: italic;">// 和 destination circle radius.</span><br />
<span style="color: #006600; font-style: italic;">// The (x,y) coordinates define the circle center points (start 和 </span><br />
<span style="color: #006600; font-style: italic;">// destination).</span><br />
<span style="color: #003366; font-weight: bold;">var</span> gradient2 <span style="color: #339933;">=</span> context.<span style="color: #660066;">createRadialGradient</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sr<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dr<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Adding colors to a radial gradient is the same as adding colors to linear </span><br />
<span style="color: #006600; font-style: italic;">// gradients.</span></div></div>
<p>我也准备了一个更复杂的例子，使用了<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-gradients.html">线性颜色渐变、阴影和文字</a>。其效果见图 7。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-gradients.html"><img alt="Example rendering using a linear gradient." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example5.jpg" /><br /> <br />
</a></p>
<p class="comment">图 7: 使用线性颜色渐变的例子</p>
<h3>canvas 演示</h3>
<p>如果你想知道使用 Canvas可以做些什么，可以参看以下的工程：</p>
<ul>
<li>Opera Widget：
<ul>
<li><a href="http://widgets.opera.com/widget/5040/">SimAquarium</a></li>
<li><a href="http://widgets.opera.com/widget/4647/">Artist&#8217;s Sketchbook</a></li>
<li><a href="http://widgets.opera.com/widget/5218/">Spirograph</a></li>
</ul>
</li>
<li>在线工程和演示
<ul>
<li><a href="http://www.benjoffe.com/code/demos/interpolate/">Newton polynomial</a></li>
<li><a href="http://www.benjoffe.com/code/demos/canvascape/">Canvascape &#8211; &#8220;3D Walker&#8221;</a></li>
<li><a href="http://code.google.com/p/paintweb">Paint.Web &#8211; painting demo, open-source</a></li>
<li><a href="http://arapehlivanian.com/wp-content/uploads/2007/02/canvas.html">Star-field flight</a></li>
<li><a href="http://www.blobsallad.se/">Interactive blob</a></li>
</ul>
</li>
</ul>
<h3 id="summary">小节</h3>
<p>Canvas 是<a href="http://blog.bingo929.com/category/technology/html5"> HTML 5</a>最让人期待的特性之一，目前已获得大部分 Web 浏览器支持。Canvas 可以帮助创建游戏、增强图形用户界面。2D context API 提供大量图形绘制功能——我希望通过本文你了解了 canvas 使用，并且你有兴趣了解更多！</p>
<p>本文转自：<a href="http://kb.operachina.com/node/190" target="_blank">operachina</a></p>
<p>本文翻译自 <a href="http://dev.opera.com/articles/view/html-5-canvas-the-basics/">HTML 5 canvas &#8211; the basics</a><br />
作者 <a href="http://dev.opera.com/author/112520">Mihai Sucan</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/html-5-canvas-the-basics-html5.html/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>用CSS3制作50个超棒动画效果教程</title>
		<link>http://blog.bingo929.com/50-awesome-css3-animations.html</link>
		<comments>http://blog.bingo929.com/50-awesome-css3-animations.html#comments</comments>
		<pubDate>Mon, 15 Mar 2010 06:20:28 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2857</guid>
		<description><![CDATA[　　CSS3为我们带来了令人惊叹的新特性，而最有趣的就是CSS动画。今天彬Go向大家推荐这50个CSS动画集合可以让你通过使用JavaScript函数来让动画更生动。为了能够预览到这些惊人的CSS3技术带来... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/50-awesome-css3-animations.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/css3-animations-title.jpg" alt="50个css3技术教程" title="css3-动画-教程" width="500" height="150" class="alignnone size-full wp-image-2872" /></a></p>
<p>　　CSS3为我们带来了令人惊叹的新特性，而最有趣的就是CSS动画。今天彬Go向大家推荐这50个CSS动画集合可以让你通过使用JavaScript函数来让动画更生动。为了能够预览到这些惊人的CSS3技术带来的动画特效，请大家使用如<strong>Safari</strong>和<strong>Chrome</strong>这类基于WebKit内核的浏览器。(<strong>IE浏览器谢绝观赏</strong>~)<br />
<span id="more-2857"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下CSS3相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/5-css3-design-enhancements-div-css.html">5个CSS3技术实现设计增强</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》<br />
《<a href="http://blog.bingo929.com/div-css-web-design-to-next-level-with-css3.html">提升你设计水平的CSS3新技术</a>》</p>
<h3>1.<a href="http://css-tricks.com/examples/CSS3Clock/" target="_blank">CSS3实现钟表效果(基于jQuery)</a></h3>
<p>　　使用CSS3的基本变形特性:rotate，并结合了jQuery这类javaScript框架制作的CSS3时钟效果。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/clock-jQuery-css3-animations.jpg" alt="表-jQuery-css3" title="表-jQuery-css3" width="500" height="228" class="alignnone size-full wp-image-2869" /></p>
<h3>2.<a href="http://www.fofronline.com/experiments/clock/#clock" target="_blank">模拟时钟</a></h3>
<p>　　模拟时钟基于过渡webkit和CSS3变形，javascript用于将其按当前时间转动。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/analogue-clock-css3-animations.jpg" alt="时钟效果-css3" title="时钟效果-css3" width="500" height="228" class="alignnone size-full wp-image-2863" /></p>
<h3>3.<a href="http://www.fofronline.com/experiments/cube-3d/" target="_blank">可使用箭头键旋转的3D立方体</a></h3>
<p>　　你可以使用上、下、左、右方向键控制这个3D立方体，它是基于webkit-perspective, -webkit-transform 和 -webkit-transition 创建。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/3D-cube-rotates-arrow-keys-css3-animations.jpg" alt="css3-3D立方体" title="css3-教程-3D" width="500" height="228" class="alignnone size-full wp-image-2859" /></p>
<h3>4.<a href="http://www.fofronline.com/experiments/cube/multiCubes.html" target="_blank">多种3D立方体(淡入淡出)</a></h3>
<p>　　多种3D立方体使用CSS3和它的属性‘transform’ 和 ‘transition’ ，我个人觉得效果很震撼，你能看到写在3D立方体上的透视感极强的文字。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/multiple-3d-cubes-slide-in-out-css3-animations.jpg" alt="多种3D立方体(淡入淡出)" title="多种3D立方体(淡入淡出)" width="500"  /></p>
<h3>5.<a href="http://www.fofronline.com/experiments/accordion/#two" target="_blank">CSS3手风琴效果</a></h3>
<p>　　使用CSS3实现的手风琴菜单效果，基于webkit浏览器专有属性。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/accordion-css3-animations.jpg" alt="CSS3手风琴效果" title="CSS3手风琴效果" width="500"  /></p>
<h3>6.<a href="http://www.fofronline.com/experiments/parallax/" target="_blank">自动滚动视差效果</a></h3>
<p>　　自动滚动视差效果使用WebKit的 CSS transition 属性。该效果无需JavaScript。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/auto-scrolling-parallax-css3-animations.jpg" alt="自动滚动视差效果" title="自动滚动视差效果" width="500"  /></p>
<h3>7.<a href="http://www.zachstronaut.com/lab/isocube.html" target="_blank">Isocube</a></h3>
<p> 　　Isocube有些类似于3D立方体，但也有不同之处，它可以让他片贴在立方体面。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/isocube-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>8.<a href="http://devfiles.myopera.com/articles/1041/image-gallery.html" target="_blank">CSS3图片集</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/image-gallery-2-css3-animations.jpg" alt="" title="" width="500"  /></p>
<h3>9.<a href="http://girliemac.com/sandbox/matrix.html" target="_blank">CSS3 Matrix(黑客帝国效果)</a></h3>
<p>　　黑客帝国是最棒的科幻小说改编电影之一，现在使用纯CSS就能实现电影中的矩阵效果。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/matrix-css3-animations.jpg" alt="CSS3 Matrix(黑客帝国效果)" title="CSS3 Matrix(黑客帝国效果)" width="500"  /></p>
<h3>10.<a href="http://webdeveloperjuice.com/demos/css/css3effects.html" target="_blank">7种CSS3结合javascript技术的特效</a></h3>
<p>　　7种CSS3结合javascript技术的特效实例. 所包含的效果有淡入淡出, 震动, 轻移, 扩大, 跳动, 旋转 和 手风琴效果。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/7-javascript-effect-css3-animations.jpg" alt="7种CSS3结合javascript技术的特效" title="7种CSS3结合javascript技术的特效" width="500"  /></p>
<h3>11.<a href="http://dinolatoga.com/demo/webkit-image-hover-effects/" target="_blank">各种鼠标悬停图片特效</a></h3>
<p>　　六个由CSS3替代JavaScript而实现的鼠标悬停图片效果，CSS3实现这种悬停效果依然很炫！<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/image-hover-effects-css3-animations.jpg" alt="各种鼠标悬停图片特效" title="各种鼠标悬停图片特效" width="500"  /></p>
<h3>12.<a href="http://www.romancortes.com/blog/pure-css-coke-can/" target="_blank">旋转的可口可乐易拉罐(使用滚动条控制)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/turning-coke-can-control-with-scrollbar-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>13.<a href="http://www.romancortes.com/blog/css-3d-meninas/" target="_blank">3D 《宫女》</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/3D-meninas-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>14.<a href="http://media.24ways.org/2009/14/5/index.html" target="_blank">宝利来画廊</a></h3>
<p>　　宝利来画廊是由CSS3技术实现的将一些照片堆在一起，有趣的是鼠标悬停照片是，照片会旋转并缓慢放大~<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/polaroid-gallery-2-css3-animations.jpg" alt="CSS3教程" title="CSS3教程" width="500"  /></p>
<h3>15.<a href="http://media.24ways.org/2009/15/space.html" target="_blank">太空</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/space-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>16.<a href="http://www.zurb.com/playground/osx-dock" target="_blank">Mac Dock</a></h3>
<p>　　CSS3模拟的mac操作系统菜单&#8230;<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/mac-dock-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>17.<a href="http://www.zurb.com/playground/drop-in-modals" target="_blank">Drop-In Modals</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/drop-in-modals-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>18.<a href="http://www.zurb.com/playground/sliding-vinyl" target="_blank">滑动的唱片</a></h3>
<p>　　该效果使用了CSS3过渡和些许HTML，在它上面放一个唱片专辑会看上去更生动。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/sliding-vinyl-css3-animations.jpg" alt="CSS3实例" title=""CSS3实例 width="500"  /></p>
<h3>19.<a href="http://www.zurb.com/playground/css3-polaroids" target="_blank">CSS3 Zooming Polaroids</a></h3>
<p>　　该效果会将一个照片列表按顺序排列，并把它们按不同的角度旋转，使用了CSS2和CSS3技术，无JavaScript。它从图片的alt属性中读取出照片描述并放到了照片的下方。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/zooming-polaroids-css3-animations.jpg" alt="CSS3 Zooming Polaroids" title="CSS3 Zooming Polaroids" width="500"  /></p>
<h3>20.<a href="http://www.the-art-of-web.com/css/css-animation/" target="_blank">CSS3 火箭动画</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/animated-rocket-css3-animations.jpg" alt="CSS3 火箭动画" title="CSS3 火箭动画" width="500"  /></p>
<h3>21.<a href="http://webkit.org/blog-files/3d-transforms/poster-circle.html" target="_blank">Poster Circle</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/poster-circle-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>22.<a href="http://webkit.org/blog-files/3d-transforms/morphing-cubes.html" target="_blank">变形的立方体</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/morphing-cubes-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>23.<a href="http://www.3site.eu/examples/gallery/" target="_blank">动画版宝丽来画廊</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/polaroid-gallery-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>24.<a href="http://www.zachstronaut.com/lab/text-shadow-box/text-shadow-box.html" target="_blank">聚光灯下的投影效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/spotlight-cast-shadow-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>25.<a href="http://demo.tutorialzine.com/2009/12/colorful-clock-jquery-css/demo.html" target="_blank">色彩鲜艳的时钟</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/colorful-clock-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>26.<a href="http://demo.tutorialzine.com/2009/11/hovering-gallery-css3-jquery/demo.php" target="_blank">Lightbox Gallery</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/lightbox-gallery-draggable-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>27.<a href="http://buildinternet.com/live/elasticthumbs/" target="_blank">弹性缩略图菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/elastic-thumbnail-menu-css3-animations.jpg" alt="弹性缩略图菜单" title="弹性缩略图菜单" width="500"  /></p>
<h3>28.<a href="http://paulbakaus.com/lab/js/coverflow/" target="_blank">Coverflow</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/coverflow-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>29.<a href="http://demo.marcofolio.net/jquery_dj/" target="_blank">jQuery DJ Hero</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/jQuery-dj-hero-css3-animations.jpg" alt="jQuery DJ Hero" title="jQuery DJ Hero" width="500"  /></p>
<h3>30.<a href="http://designlovr.com/examples/dynamic_stack_of_index_cards/" target="_blank">Dynamic Stacking Cards</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/dynamic-stacking-cards-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>31.<a href="http://www.alexandtheweb.com/demos/transitions-test.html" target="_blank">另一个CSS3图片画廊效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/image-gallery-css3-animations.jpg" alt="" title="" width="500"  /></p>
<h3>32.<a href="http://www.satine.org/research/webkit/snowleopard/snowstack.html" target="_blank">雪堆(使用方向键控制)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/snow-stack-control-with-arrow-keys-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>33.<a href="http://stuffandnonsense.co.uk/content/demo/cannybill/21-10-2009/pricing.html" target="_blank">CSS3动画版价格栏</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/pricing-column-css3-animations.jpg" alt="CSS3动画版价格栏" title="CSS3动画版价格栏" width="500"  /></p>
<h3>34.<a href="http://demo.marcofolio.net/slick_jquery_css3_menu/default.html" target="_blank">平滑jQuery+CSS3菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/slick-jQuery-menu-css3-animations.jpg" alt="平滑jQuery+CSS3菜单" title="平滑jQuery+CSS3菜单" width="500"  /></p>
<h3>35.<a href="http://www.kamikazemusic.com/demo/csstabs.html" target="_blank">CSS标签(无JavaScript)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/tabs-without-javascript-css3-animations.jpg" alt="CSS标签(无JavaScript)" title="CSS标签(无JavaScript)" width="500"  /></p>
<h3>36.<a href="http://development.tobypitman.com/css/menu.html" target="_blank">CSS标签菜单(无JavaScript)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/tab-menus-without-javascript-css3-animations.jpg" alt="CSS标签菜单(无JavaScript)" title="CSS标签菜单(无JavaScript)" width="500"  /></p>
<h3>37.<a href="http://o.sitepen.com/labs/code/cssDock/" target="_blank">SVG+CSS3鱼眼菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/svg-fisheye-menu-css3-animations.jpg" alt="VG+CSS3鱼眼菜单" title="VG+CSS3鱼眼菜单" width="500"  /></p>
<h3>38.<a href="http://webkit.org/blog-files/leaves/index.html" target="_blank">CSS3落叶效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/falling-leaves-css3-animations.jpg" alt="CSS3落叶效果" title="CSS3落叶效果" width="500"  /></p>
<h3>39.<a href="http://samples.webmatze.de/rotating_image_gallery.htm" target="_blank">CSS3旋转图集效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/rotating-gallery-css3-animations.jpg" alt="CSS3旋转图集效果" title="CSS3旋转图集效果" width="500"  /></p>
<h3>40.<a href="http://www.newmediacampaigns.com/files/http://www.webdesignerwall.com/wp-content/uploads/2010/02/nicer-navigation-with-css-transitions/demo.html" target="_blank">CSS3下拉菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/dropdown-menu-css3-animations.jpg" alt="CSS3下拉菜单" title="CSS3下拉菜单" width="500"  /></p>
<h3>41.<a href="http://blog.gesteves.com/post/261593774/im-done-star-wars-opening-crawl-using-only-html" target="_blank">星球大战风格爬行文字(纯HTML+CSS)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/star-wars-crawl-css3-animations.jpg" alt="星球大战风格爬行文字(纯HTML+CSS)" title="星球大战风格爬行文字(纯HTML+CSS)" width="500"  /></p>
<h3>42.<a href="http://webkit.org/demos/sticky-notes/" target="_blank">CSS3即时贴</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/sticky-notes-css3-animation.jpg" alt="CSS3即时贴" title="CSS3即时贴" width="500"  /></p>
<h3>43.<a href="http://natbat.net/code/clientside/css/snowflakes/" target="_blank">css3雪花</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/Snowflakes-css3-animations.jpg" alt="css3雪花" title="css3雪花" width="500"  /></p>
<h3>44.<a href="http://www.niquelao.net/wp-content/uploads/2009/12/example1.html" target="_blank">又一个鱼眼效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/another-fisheye-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>45.<a href="http://www.cssplay.co.uk/menu/css3-animation.html" target="_blank">CSS3逐帧动画</a></h3>
<p>　　第一个CSS3逐帧动画实例需要你持续使用鼠标点击图片，当播放到最后一帧时它会自动播放第一帧，如此循环，但第一个实例只能在FireFox和Opera下实现。第二个Css3逐帧动画实例需要你使用鼠标在图片上移动，依旧是mousemove这个动作，只要鼠标在移动图片动画就会持续播放，但动画的播放速度是与你的鼠标移动速度有关的，该实例支持Safari, Google Chrome, IE8, Firefox 和 Opera。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/Frame-by-Frame-css3-animations.jpg" alt="CSS3逐帧动画" title="CSS3逐帧动画" width="500"  /></p>
<h3>46.<a href="http://anthonycalzadilla.com/css3-ATAT/index-bones.html" target="_blank">全地域装甲载具</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/at-walker-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>47.<a href="http://www.thecssninja.com/demo/css_accordion/#Section2" target="_blank">又一个CSS3手风琴效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/Another-Accordion-css3-animations.jpg" alt="又一个CSS3手风琴效果" title="又一个CSS3手风琴效果" width="500"  /></p>
<h3>48.<a href="http://files.myopera.com/FataL/pres/pres.xml#name" target="_blank">无Flash版动态展示</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/dynamic-presentation-without-flash-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>49.<a href="http://elliottrothman.com/files/smoothie.html" target="_blank">平滑菜单栏</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/smoothie-css3-animations.jpg" alt="平滑菜单栏" title="平滑菜单栏" width="500"  /></p>
<h3>50.<a href="http://24ways.org/2009/css-animations" target="_blank">loading旋转效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/spinner-css3-animations.jpg" alt="loading旋转效果" title="loading旋转效果" width="500"  /></p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看CSS3最新教程。 </p>
<p>英文原文:<a title="50 Awesome Animations made with CSS3" href="http://www.1stwebdesigner.com/development/50-awesome-css3-animations/" target="_blank">50 Awesome Animations made with CSS3</a><br />
翻译原文:<a title="用CSS3制作50个超棒动画效果教程" href="http://blog.bingo929.com/50-awesome-css3-animations.html">用CSS3制作50个超棒动画效果教程</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="用CSS3制作50个超棒动画效果教程" href="http://blog.bingo929.com/50-awesome-css3-animations.html">http://blog.bingo929.com/50-awesome-css3-animations.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/50-awesome-css3-animations.html/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>15款提高表格操作的jQuery插件</title>
		<link>http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html</link>
		<comments>http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html#comments</comments>
		<pubDate>Mon, 21 Dec 2009 06:40:55 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[前端开发]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2824</guid>
		<description><![CDATA[　　table表格由于它的浏览器兼容性和复杂的标签嵌套方式，可以算是添加样式最困难的对象之一了。大多数前端er都把网页中的table标签替换为div，主要就是因为div要比table更容易添加CSS样式。... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html"><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-table-plugins-title.jpg" alt="jquery-table-表格-插件" title="jquery-table-表格-插件" width="500" height="150" class="alignnone size-full wp-image-2825" /></a></p>
<p>　　table表格由于它的浏览器兼容性和复杂的标签嵌套方式，可以算是添加样式最困难的对象之一了。大多数前端er都把网页中的table标签替换为div，主要就是因为div要比table更容易添加CSS样式。但是我们在日常应用中仍然要用到table表格，其中最好的例子就是对照表。今天彬Go将向大家推荐<strong>15个jQuery表格插件</strong>让你对数据表格进行显示、排序、筛选和操控。如jQuery表格排序插件、jQuery表格拖拽插件、jQuery树形表格插件、设置颜色、点击、替换等效果。<br />
<span id="more-2824"></span><br />
<strong><span style="color: #ff6600;">您还可以参考以下javascript相关资源:</span></strong><br />
《<a href="http://blog.bingo929.com/useful-javascript-plot-charts-web.html">推荐9款很棒的网页绘制图表JavaScript框架脚本</a>》<br />
《<a href="http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html">了解jQuery技巧来提高你的代码</a>》<br />
《<a href="http://blog.bingo929.com/10-ajax-effects-website-fanciness.html">10种JavaScript特效实例让你的网站更吸引人</a>》<br />
《<a href="http://blog.bingo929.com/300-jquery-css-mootools-js-navigation-menus.html">300+Jquery, CSS, MooTools 和 JS的导航菜单资源</a>》</p>
<h3>1.<a href="http://www.datatables.net/" target="_blank">DataTables</a>-强大的jQuery表格插件</h3>
<p><a href="http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html"><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/datatables.jpg" alt="datatables-jquery-插件" title="datatables-jquery-插件" width="500" height="194" class="alignnone size-full wp-image-2827" /></a><br />
　　DataTables是提供了大量特性的强大jQuery表格插件。例如:你可以自动轻松筛选、Ajax预读取数据、分页、列排序、高亮排序列、扩展插件支持、使用CSS或jQuery UI ThemeRoller 定制主题和完整文档。</p>
<h3>2.<a href="http://gregweber.info/projects/uitablefilter" target="_blank">uiTableFilter</a>-jQuery表格过滤插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/ui-table-filter.jpg" alt="ui-table-filter-jquery-插件" title="ui-table-filter-jquery-插件" width="500" height="415" class="alignnone size-full wp-image-2829" /><br />
　　uiTableFilter是一个用于表格行筛选的jQuery插件。插件作者提供了详细的例子来告诉大家如何将自己的表格和插件整合在一起并完成表格筛选。</p>
<h3>3.<a href="http://www.webtoolkit.info/scrollable-html-table-plugin-for-jquery.html" target="_blank">Scrollable HTML Table</a>-jQuery表格滚动插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/scrollable-html-table.jpg" alt="scrollable-html-table-jquery" title="scrollable-html-table-jquery" width="500" height="171" class="alignnone size-full wp-image-2830" /><br />
　　Scrollable HTML Table <a href="http://blog.bingo929.com/category/technology/jquery-technology">jQuery</a>插件可以让你的表格变得可以滚动控制。</p>
<h3>4.<a href="http://tablesorter.com/docs/" target="_blank">Tablesorter</a>-jQuery表格排序插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/tablesorter.jpg" alt="tablesorter-jquery-插件" title="tablesorter-jquery-插件" width="500" height="120" class="alignnone size-full wp-image-2831" /><br />
　　Tablesorter这个jQuery插件是用来将一个包含thead和tbody标签的标准HTML表格转变为无页面刷新的可排序表格。它不但支持多列排序，而且跨浏览器兼容并且能通过widget系统进行扩展。</p>
<h3>5.<a href="http://flexigrid.info/" target="_blank">Flexigrid</a>-Web2.0 jQuery表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/flexigrid.jpg" alt="flexigrid-jquery" title="flexigrid-jquery" width="500" height="224" class="alignnone size-full wp-image-2833" /><br />
　　Flexigrid 是一个轻量级的Web2.0 jQuery插件。它包含很多非常Cool的特性，如主题定制、分页、工具栏、搜索、排序、ajax读取数据源、调整列宽高尺寸等。</p>
<h3>6.<a href="http://www.jnathanson.com/blog/client/jquery/heatcolor/index.cfm" target="_blank">HeatColor</a>-jQuery表格颜色插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/heatcolor.jpg" alt="heatcolor-jquery" title="heatcolor-jquery" width="500" height="340" class="alignnone size-full wp-image-2834" /><br />
　　HeatColor可以让你根据元素的值来为元素设置颜色。 它的值的比对时根据预设的一个范围来进行比较，最后对他们自动进行颜色值的设置。</p>
<h3>7.<a href="http://www.hanpau.com/index.php?page=jqtreetable" target="_blank">JQTreeTable</a>-jQuery树形表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jqtreetable.jpg" alt="jqtreetable-jquery" title="jqtreetable-jquery" width="500" height="220" class="alignnone size-full wp-image-2835" /><br />
　　使用JQTreeTable插件你可以得到一个树形表格，如果用户的浏览器禁用了JavaScript，那么他们也能看到普通形式的表格，并不影响可访问性。</p>
<h3>8.<a href="http://reconstrukt.com/ingrid/" target="_blank">Ingrid</a>-jQuery表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/ingrid.jpg" alt="ingrid-jquery-表格-插件" title="ingrid-jquery-表格-插件" width="500" height="224" class="alignnone size-full wp-image-2836" /><br />
　　Ingrid是另一个数据表jQuery插件。它也有很多特性，比如列调整尺寸、排序、行列添加样式等。在作者网站提供了很棒的文档来告诉大家如何从头使用这个jQuery表格插件。 </p>
<h3>9.<a href="http://p.sohei.org/jquery-plugins/columnmanager/" target="_blank">jQuery ColumnManager plugin</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-column-manager.jpg" alt="jquery-column-manager-jquery-表格插件" title="jquery-column-manager-jquery-表格插件" width="500" height="150" class="alignnone size-full wp-image-2837" /><br />
　　columnManager是可以控制任意表格列显示或隐藏的jQuery插件，而且它可以保存当前表格的状态至你下次访问。这个jQuery插件非常小巧轻量，仅3.6kb。 </p>
<h3>10.<a href="http://blog.cubicphuse.nl/2008/11/12/jquery-treetable-2-0" target="_blank">jQuery treeTable</a>-jQuery树形表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-treetable.jpg" alt="jquery-treetable-树形表格插件" title="jquery-treetable-树形表格插件" width="500" height="192" class="alignnone size-full wp-image-2844" /><br />
　　jQuery treeTable是小型的JQTreeTable, 它也能让你的table显示成树形结构. 这个插件也可以在浏览器禁用JavaScript时保持文档整洁并让原始table可以使用.</p>
<h3>11.<a href="http://plugins.jquery.com/project/csv2table" target="_blank">CSV2Table</a>-CSV格式读取表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/csv2table.jpg" alt="csv2table-jQuery表格插件" title="csv2table-jQuery表格插件" width="500" height="232" class="alignnone size-full wp-image-2845" /><br />
　　CSV2Table 可以读取CSV文件，它从CSV文件中读取内容并创建成table表格.</p>
<h3>12.<a href="http://neoalchemy.org/tablePagination.html" target="_blank">Table Pagination</a>-jQuery表格分页插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/table-pagination.jpg" alt="table-pagination-jQuery表格插件" title="table-pagination-jQuery表格插件" width="500" height="227" class="alignnone size-full wp-image-2846" /><br />
　　这个jQuery表格分页插件可以在表格下面创建分页元素，你还可以通过各种设置来定制分页。</p>
<h3>13.<a href="http://pure-essence.net/stuff/webTips/jqueryTableRowCheckboxToggle.html" target="_blank">jQuery TableRowCheckboxToggle</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-table-row-checkbox-toggle.jpg" alt="jquery-table-插件" title="jquery-table-插件" width="500" height="182" class="alignnone size-full wp-image-2848" /><br />
　　这个<a href="http://blog.bingo929.com/category/technology/jquery-technology">jQuery</a>表格插件可以让你再点击表格行的时候改变多选框状态并修改该行CSS样式。</p>
<h3>14.<a href="http://www.isocra.com/2008/02/table-drag-and-drop-jquery-plugin/" target="_blank">Table Drag and Drop jQuery plugin</a>-jQuery表格拖拽插件</h3>
<p>　　这个简单的jQuery插件可以让用户使用拖拽行的方式重新排列表格各行，任意单独行可以设置为不可拖拽和/或不可放置。</p>
<h3>15.<a href="http://gregweber.info/projects/uitableedit" target="_blank">uiTableEdit</a></h3>
<p>　　uiTableEdit是由Greg Weber制作的很酷的jQuery表格编辑插件，它允许用户编辑表格内容。</p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看网页设计最新资源。 </p>
<p>英文原文:<a title="15 Great jQuery Plugins For Better Table Manipulation" href="http://www.webdesignbooth.com/15-great-jquery-plugins-for-better-table-manipulation/" target="_blank">15 Great jQuery Plugins For Better Table Manipulation</a><br />
翻译原文:<a title="15款提高表格操作的jQuery插件" href="http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html">15款提高表格操作的jQuery插件</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="15款提高表格操作的jQuery插件" href="hhttp://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html">http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>了解jQuery技巧来提高你的代码</title>
		<link>http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html</link>
		<comments>http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html#comments</comments>
		<pubDate>Mon, 23 Nov 2009 02:01:13 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2760</guid>
		<description><![CDATA[　　jQuery之所以如此流行并被从大公司到个人博客的几乎每个人都广泛使用，是因为它上手和使用相当简单，而且为我们提供了一些人都不知道的相当棒的特性。我认为jQuery的大多数用户更趋... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html"><img src="http://blog.bingo929.com/wp-content/uploads/2009/11/jquery-tips-title.jpg" alt="jquery-技巧-教程" title="jquery-技巧-教程" width="500" height="200" class="alignnone size-full wp-image-2761" /></a></p>
<p>　　jQuery之所以如此流行并被从大公司到个人博客的几乎每个人都广泛使用，是因为它上手和使用相当简单，而且为我们提供了一些人都不知道的相当棒的特性。我认为jQuery的大多数用户更趋向于使用jQuery插件来解决面临的难题，这通常是明智的选择。但是当插件相对于你的需求有一定缺陷的时候，你也许更应该想办法自己来解决，下面来看看这些实用的jQuery技巧，他们肯定会能够派上用场的！<br />
<span id="more-2760"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下JavaScript相关教程及资源:</span></strong><br />
《<a href="http://blog.bingo929.com/10-ajax-effects-website-fanciness.html">10种JavaScript特效实例让你的网站更吸引人</a>》<br />
《<a href="http://blog.bingo929.com/300-jquery-css-mootools-js-navigation-menus.html">300+Jquery, CSS, MooTools 和 JS的导航菜单资源</a>》<br />
《<a href="http://blog.bingo929.com/10-sources-ajax-javascript.html">10个非常棒的Ajax及Javascript实例资源网站</a>》<br />
《<a href="http://blog.bingo929.com/useful-javascript-plot-charts-web.html">推荐9款很棒的网页绘制图表JavaScript框架脚本</a>》</p>
<h3>1.测试并提升你的jQuery选择器水平</h3>
<p>　　这个<a href="http://codylindley.com/jqueryselectors/" target="_blank">jQuery选择器实验室</a>非常酷，它能在线免费使用，当然你也能下来到本地离线使用。这个测试页面包含复杂的HTML组合字段，然后你能尝试预定义使用各种jQuery选择器。如果这还不够你也可以自定义选择器。<br />
<a href="http://blog.bingo929.com/wp-content/uploads/2009/11/jquery-tips-01.jpg" target="_blank"><img src="http://blog.bingo929.com/wp-content/uploads/2009/11/jquery-tips-01-500x280.jpg" alt="jquery-选择器-技巧" title="jquery-选择器-技巧" width="500" height="280" class="alignnone size-medium wp-image-2765" /></a><br />
<em>点击上图查看清晰大图</em></p>
<h3>2.测试jQuery包装集是否包含某些元素</h3>
<p>　　如果你想测试一下某个jQuery包装集中是否包含某些元素，你首先可以尝试使用验证首个元素是否存在：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>selector<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: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>...<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// 或者这样</span><br />
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>...<span style="color: #009900;">&#125;</span></div></div>
<p>来看看这个例子：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">//例子.如果你的页面有以下html代码<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;shopping_cart_items&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;in_stock&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Item-X&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>Item X<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;unknown&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Item-Y&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>Item Y<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;in_stock&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Item-Z&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>Item Z<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">pre</span> escaped<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript&quot;</span>&gt;</span>...<br />
//这个if条件将返回true，因为我们有两个<br />
// input域匹配了选择器，所以<span style="color: #009900;">&lt;statement&gt;</span>代码将会执行<br />
if($('#shopping_cart_items input.in_stock')[0]){<span style="color: #009900;">&lt;statement&gt;</span>}</div></div>
<h3>3.从jquery.org读取jQuery最新版本</h3>
<p>你可以使用这句代码读取jQuery的最新版本的代码文件。</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://code.jquery.com/jquery-latest.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></div></div>
<p>你可以使用这个方法来调用最近版本的jQuery框架，当然，你还可以使用下面这个代码从ajax.googleapis.com调用同样的最新版本jQuery：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></div></div>
<h3>4.存储数据</h3>
<p>　　使用data方法可以避免在DOM中存储数据，有些前端开发er喜欢使用HTML的属性来存储数据：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'alt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'data being stored'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">//之后可以这样读取数据:</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'alt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>　　使用&#8221;alt&#8221;属性来作为参数名存储数据其实对于HTML来说是不符合语义的，我们可以使用jQuery的data方法来为页面中的某个元素存储数据：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'参数名'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'要存储的数据'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">//之后这样取得数据:</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'参数'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>　　这个data方法能让你自己明明数据的参数，更语义更灵活，你可以在页面上的任何元素存储数据信息。如果想了解更多关于data()和removeData()方法的介绍，可以看看<a href="http://docs.jquery.com/Internals" target="_blank">jQuery官方讲解</a></p>
<p>这个方法的经典应用是给input域一个默认值，然后在聚焦的时候清空它:<br />
HTML部分:</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;testform&quot;</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clear&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Always cleared&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clear once&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Cleared only once&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Normal text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></div></div>
<p>JavaSript部分:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<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><br />
&nbsp;<span style="color: #006600; font-style: italic;">//取出有clear类的input域</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">//(注: &quot;clear once&quot; 是两个class clear 和 once)</span><br />
&nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#testform input.clear'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</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><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//使用data方法存储数据</span><br />
&nbsp; &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;">data</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;txt&quot;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">trim</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;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</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><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// 获得焦点时判断域内的值是否和默认值相同，如果相同则清空</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">trim</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;">val</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;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &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;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</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><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// 为有class clear的域添加blur时间来恢复默认值</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// 但如果class是once则忽略</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">trim</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;">val</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: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">&amp;&amp;</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;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;once&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//Restore saved data</span><br />
&nbsp; &nbsp; &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;">val</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;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><a href="http://www.tripwiremagazine.com/wp-content/uploads/images/stories/Articles/jquery-tips/demo.html" target="_blank">查看Demo</a></p>
<h3>5.jQuery手册常备身边</h3>
<p>　　大多数人都很难记住所有的编程细节，即使再好的程序员也会有对某个程序语言的疏忽大意，所以把相关的手册打印出来或随时放在桌面上进行查阅绝对是可以提高编程效率的。<br />
<a href="http://oscarotero.com/jquery/" target="_blank">oscarotero jquery 1.3</a> (<a href="http://www.gmtaz.com/index.php/jquery-13-cheatsheet-wallpaper/" target="_blank">壁纸版</a>)<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2009/11/oscarotero.jpg" alt="jQuery手册" title="jQuery手册" width="471" height="348" class="alignnone size-full wp-image-2777" /></p>
<h3>6.在FireBug控制台记录jQuery</h3>
<p>　　FireBug是我最喜欢用的一个浏览器扩展工具之一，这个工具可以让你快速的在可视化界面中了解当前页面的HTML+CSS+JavaScript，并在该工具下完成即时开发。作为jQuery或JavaScript开发人员，FireFox对于<a href="http://www.getfirebug.com/logging.html" target="_blank">记录你的JavaScript代码</a>也得到支持。</p>
<p>写入FireBug控制台的最简单方式如下:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello world&quot;</span><span style="color: #009900;">&#41;</span></div></div>
<p><a href="http://blog.bingo929.com/wp-content/uploads/2009/11/fire.jpg" target="_blank"><img src="http://blog.bingo929.com/wp-content/uploads/2009/11/fire-500x200.jpg" alt="firebug-jquery-控制台" title="firebug-jquery-控制台" width="500" height="200" class="alignnone size-medium wp-image-2783" /></a></p>
<p>你也可以按照你希望的方式写一些参数：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</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: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span>bar<span style="color: #009900;">&#41;</span></div></div>
<p>你也可以编写一个小扩展来记录jQuery对象到控制台：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">log</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;%s: %o&quot;</span><span style="color: #339933;">,</span> msg<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>对于这个扩展，你可以直接使用.log()方法来记录当前对象到控制台。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#some_div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li.source &gt; input:checkbox'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sources to uncheck&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;checked&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>7.尽可能使用ID选择器</h3>
<p>　　在使用jQuery之后，你会发现利用class属性来选择DOM元素变得相当简单。尽管如此，还是推荐大家尽量少用class选择器取而代之尽量多使用运行更快的ID选择器(IE浏览器下使用class选择器会在遍历整个DOM树之后返回相符的class包装集)。而ID选择器更快是因为DOM本身就有&#8221;天然的&#8221;getElementById这个方法，而class并没有。所以如果使用class选择器的话，浏览器会遍历整个DOM，如果你的网页DOM结构足够复杂，这些class选择器足矣把页面拖得越来越慢。让我们看看这段简单的HTML代码:</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Selectors in jQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span><br />
&nbsp; ...<br />
&nbsp; ...<br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main_button&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//使用class来调用submit按钮要比使用绝对的ID选择器慢很多</span><br />
<span style="color: #003366; font-weight: bold;">var</span> main_button <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#main .button'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> main_button <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#main_button'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>8.善于利用jQuery链</h3>
<p>　　jQuery链不但允许以简洁的方式写出强大的操作，而且提高了开发效率，因为它能够把多个命令应用到包装集，而不必重新计算包装集。从而你不用再这样写了：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Description: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'3px dashed yellow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'red'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;text updated&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>取而代之你可以使用jQuery链来完成简便的操作：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> input_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
input_text.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'3px dashed yellow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
input_text.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'red'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
input_text.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;text updated&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">//same with chaining:</span><br />
<span style="color: #003366; font-weight: bold;">var</span> input_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
input_text<br />
&nbsp;.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'3px dashed yellow'</span><span style="color: #009900;">&#41;</span><br />
&nbsp;.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'red'</span><span style="color: #009900;">&#41;</span><br />
&nbsp;.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;text updated&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>9.绑定jQuery函数到$(window).load事件</h3>
<p>　　大多数jQuery实例或教程都告诉我们绑定我们的jQuery代码到$(document).ready事件。虽然$(document).ready事件在大多数情况下都OK，但是它的解析顺序是在文档准备就绪，单文档中的图片等对象正在下载的时候开始运行的。所以在某些时候使用$(document).ready事件并不一定能达到我们预期的效果，比如一些视觉效果和动画、拖拽、预读取隐藏图片等&#8230;通过使用$(window).load事件便可以安全的在整个文档都准备就绪之后再开始运行你期望的代码。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</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><br />
&nbsp;<span style="color: #006600; font-style: italic;">// 将你希望在页面完全就绪之后运行的代码放在这里</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>10.使用jQuery链来限定选择器，让你的代码更简洁更优雅</h3>
<p>　　由于JavaScript支持链结构而且支持断行，所以你的代码可以写成下面这样，这个例子先在元素上移除一个class然后在同一个元素上添加另一个class：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.in_stock'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'in_stock'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'3-5_days'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>如果想让它更简单实用，你可以创建一个支持链结构的jQuery函数：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">makeNotInStock</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><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</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;">'in_stock'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'3-5_days'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#shopping_cart_items input.in_stock'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">makeNotInStock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>11.使用回调函数同步效果</h3>
<p>　　如果你想确保某个事件或动画效果要在另一个事件运行之后再调用，那你就要使用回调函数了。你可以在这些动画效果后面绑定回调函数：slideDown( speed, [回调] ) ie. $(&#8216;#sliding&#8217;).slideDown(&#8216;slow&#8217;, function(){&#8230; <a href="http://www.tripwiremagazine.com/wp-content/uploads/images/stories/Articles/jquery-tips2/demo.html" target="_blank">点击这里</a>预览这个例子.</p>
<div class="codecolorer-container css blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;style<span style="color: #00AA00;">&gt;</span><br />
&nbsp;div<span style="color: #6666ff;">.button</span> &nbsp; &nbsp;<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#cfd</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">50px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<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;">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;">cursor</span><span style="color: #00AA00;">:</span><span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #993333;">outset</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bolder</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span><br />
&nbsp;<span style="color: #cc00cc;">#sliding</span> &nbsp; &nbsp; &nbsp;<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: #00AA00;">&#125;</span><br />
&lt;/style<span style="color: #00AA00;">&gt;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<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><br />
<span style="color: #006600; font-style: italic;">// 使用jQuery的click事件改变视觉效果，并开启滑动效果</span><br />
&nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.button&quot;</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><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//div.button 现在看上去是按下的效果</span><br />
&nbsp; &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;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> borderStyle<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;inset&quot;</span><span style="color: #339933;">,</span> cursor<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;wait&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//#sliding 现在将渐隐并在完成动作之后开启渐显效果</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//slideup once it completes</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#sliding'</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: #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><br />
&nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#sliding'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</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><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//渐显效果完成后将会改变按钮的CSS属性</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.button'</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> borderStyle<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;outset&quot;</span><span style="color: #339933;">,</span> cursor<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;auto&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>12.学会使用自定义选择器</h3>
<p>　　jQuery允许我们在css选择器的基础上定义自定义选择器来让我们的代码更简洁:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">mycustomselector</span><span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> meta<span style="color: #339933;">,</span> stack<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// element- DOM元素</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// index - 堆栈中当前遍历的索引值</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// meta - 关于你的选择器的数据元</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// stack - 用于遍历所有元素的堆栈</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// 包含当前元素则返回true</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// 不包含当前元素则返回false</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// 自定义选择器的应用:</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.someClasses:test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>下面让我们来看看一个小例子，我们通过使用自定义选择器来锁定含有&#8221;rel&#8221;属性的元素集：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">withRel</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">//仅返回rel属性不为空的元素</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>$this.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rel'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
$<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><br />
<span style="color: #006600; font-style: italic;">//自定义选择器的使用很简单，它和其他选择器一样，返回一个元素包装集</span><br />
<span style="color: #006600; font-style: italic;">//你可以为他使用格式方法，比如下面这样修改它的css样式</span><br />
&nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a:withRel'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'green'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>without rel<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;somerel&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>with rel<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>without rel<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nofollow&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>a link with rel<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></div></div>
<h3>13.预加载图片</h3>
<p>通常使用JavaScript来预加载图片是个相当不错的方法:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//定义预加载图片列表的函数(有参数)</span><br />
&nbsp;<br />
jQuery.<span style="color: #660066;">preloadImages</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><br />
&nbsp; <span style="color: #006600; font-style: italic;">//遍历图片</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;img&gt;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// 你可以这样使用预加载函数</span><br />
$.<span style="color: #660066;">preloadImages</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;images/logo.png&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;images/logo-face.png&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;images/mission.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>14.将你的代码测试完好</h3>
<p>　　jQuery有一个名为QUnit单元测试框架。编写测试很容易，它能让您可以放心地修改您的代码，并确保它仍然按预期工作。下面是如何工作的：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//将测试分成若干模块.</span><br />
module<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Module B&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;some other test&quot;</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><br />
&nbsp; <span style="color: #006600; font-style: italic;">//指定多少个判断语句需要加入测试中.</span><br />
&nbsp; expect<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; equals<span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;failing test&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; equals<span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;passing test&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看JavaScript最新教程及资源。 </p>
<p>英文原文:<a title="More jQuery and General Javascript Tips to Improve Your Code" href="http://www.tripwiremagazine.com/ajax/developer-toolbox/more-jquery-and-general-javascript-tips-to-improve-your-code.html" target="_blank">More jQuery and General Javascript Tips to Improve Your Code</a><br />
翻译原文:<a title="了解更多jQuery技巧来提高你的代码" href="http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html">了解更多jQuery技巧来提高你的代码</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="了解更多jQuery技巧来提高你的代码" href="http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html">http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>让所有IE支持HTML5的解决方案</title>
		<link>http://blog.bingo929.com/html5-ie-enabling-script.html</link>
		<comments>http://blog.bingo929.com/html5-ie-enabling-script.html#comments</comments>
		<pubDate>Tue, 13 Oct 2009 09:08:11 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2686</guid>
		<description><![CDATA[　　自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注，然而唯一能让IE识别那些新元素(如&#60;article&#62;)的途径是使用HTML5 shiv,感谢remy sharp为我们提供了这个迷你脚本来解决IE... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/html5-ie-enabling-script.html"><img src="http://blog.bingo929.com/wp-content/uploads/2009/10/html5-ie.jpg" alt="html5-ie支持" title="html5-ie支持" width="500" height="150" class="alignnone size-full wp-image-2695" /></a></p>
<p>　　自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注，然而唯一能让IE识别那些新元素(如&lt;article&gt;)的途径是使用<a href="http://ejohn.org/blog/html5-shiv/" target="_blank">HTML5 shiv</a>,感谢<a href="http://remysharp.com/" target="_blank">remy sharp</a>为我们提供了这个迷你脚本来解决IE支持<a href="http://blog.bingo929.com/category/technology/html5">HTML5</a>的问题。<br />
<span id="more-2686"></span><br />
<strong><span style="color: #ff6600;">您还可以参考以下HTML5相关教程及资源:</span></strong><br />
《<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html">HTML5 Drag&#038;Drop 拖拽、FileReader实例教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-websockets.html">HTML5 WebSockets 基础使用教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-and-css3-tools-list.html">HTML5和CSS3工具资源汇总</a>》<br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》</p>
<h3>使用和下载</h3>
<p>　　<a href="http://html5shiv.googlecode.com/svn/trunk/html5.js" target="_blank">html5.js</a>必须在页面head元素内调用(因为IE必须在元素解析前知道这个元素，所以这个js文件不能在页面底部调用。)<br />
　　作者已经把js文件放在Google code project上并允许大家直接调用，当然，前提是你不在意调用额外的文件。<br />
<a href="http://html5shiv.googlecode.com/svn/trunk/html5.js" target="_blank">http://html5shiv.googlecode.com/svn/trunk/html5.js</a><br />
　　你可以使用IE条件注释来调用这个js文件，这样像FireFox等非IE浏览器就会忽视这段代码，也就不会有无谓的http请求了。下面这段代码仅会在IE浏览器下运行:</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!--[if IE]&gt;</span><br />
<span style="color: #808080; font-style: italic;"> &nbsp; &nbsp;&lt;script src=&quot;http://html5shiv.googlecode.com/svn/trunk/html5.js&quot;&gt;&lt;/script&gt;</span><br />
<span style="color: #808080; font-style: italic;">&lt;![endif]--&gt;</span></div></div>
<p>当然如果你不喜欢调用外网文件，你也可以下载下来上传到自己的服务器单独调用(作者允许)。</p>
<p>以下是js文件中的代码:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><span style="color: #339933;">!</span><span style="color: #006600; font-style: italic;">/*@cc_on!@*/</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;abbr,article,aside,audio,canvas,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video&quot;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>i<span style="color: #339933;">=</span>e.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</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></div></div>
<p>英文原文:<a title="HTML5 enabling script" href="http://remysharp.com/2009/01/07/html5-enabling-script/" target="_blank">JavaScript and the DOM Series: Lesson 1</a><br />
翻译原文:<a title="让所有IE支持HTML5的解决方案" href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>(<a href="http://blog.bingo929.com/">彬Go</a>) </p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="让所有IE支持HTML5的解决方案" href="http://blog.bingo929.com/html5-ie-enabling-script.html">http://blog.bingo929.com/html5-ie-enabling-script.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/html5-ie-enabling-script.html/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

