<?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>CSSrainbow.cn &#187; JavaScript</title>
	<atom:link href="http://cssrainbow.cn/category/code-snippets/javascript-code-snippets/feed" rel="self" type="application/rss+xml" />
	<link>http://cssrainbow.cn</link>
	<description>Sharing what I know and learn about (X)HTML,CSS,JavaScript,MooTools,Dojo,jQuery,PHP,ASP,Java,Python and ∞.</description>
	<lastBuildDate>Sun, 05 Sep 2010 15:08:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>本地图片预览二三事</title>
		<link>http://cssrainbow.cn/tutorials/javascript/1354.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/1354.html#comments</comments>
		<pubDate>Tue, 27 Jul 2010 13:55:47 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[图片预览]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=1354</guid>
		<description><![CDATA[上传图片是我们Web开发经常用到的一个功能，那当然少不了文件域标签的使用，如果是上传的是图片文件能够提前预览就好了。<input type="file"/>到目前为止，只有IE和FF可以预览本地图片。IE下可以直接浏览本地图片，通过input[type=file]的value属性就可以取到本地图片的路径。而在FF下，有getAsDataURL()方法可以生成图片的DataURL，然后赋值给img标签。]]></description>
			<content:encoded><![CDATA[<p>上传图片是我们Web开发经常用到的一个功能，那当然少不了文件域标签的使用，如果是上传的是图片文件能够提前预览就好了。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;file&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>到目前为止，只有IE和FF可以预览本地图片。IE下可以直接浏览本地图片，通过input[type=file]的value属性就可以取到本地图片的路径。<br />
而在FF下，有getAsDataURL()方法可以生成图片的DataURL，然后赋值给img标签。</p>
<h2>各种浏览器的支持情况</h2>
<h3>Internet Explorer &#038; Opera</h3>
<p>这两种浏览器返回的值是一样，如果把此站点添加到IE “受信任的站点”列表中，是一个完整的文件的路径。否则就是它：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">C:\fakepath\image.png</pre></td></tr></table></div>

<p><span id="more-1354"></span></p>
<p>如果还是不理解的话，请看这篇文章《<a href="http://acidmartin.wordpress.com/2009/06/09/the-mystery-of-cfakepath-unveiled/">揭露 c:\fakepath 的秘密</a>》。</p>
<h3>WebKit (Safari / Chrome)</h3>
<p>返回该文件的输入值没有任何价值。</p>
<h3>Firefox</h3>
<p>Firefox是独特的，它的返回值仅是文件名。不过Firefox提供了一个函数&#8217;getAsDataURL（）&#8217;方法可以获得图片文件的完整路径。</p>
<h2>IE 下效果的JavaScript Code：</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;upload&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onchange</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>
	 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;image&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>FF 下效果的JavaScript Code:</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;upload&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onchange</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>
	 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;image&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">files</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>IE and FF 的JavaScript Code:</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
   var ie=!-[1,]
   最短的IE浏览器判断
*/</span>
&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;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;upload&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onchange</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>
	          document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;image&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">files</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;upload&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onchange</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>
			  document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;image&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>jQuery Code:</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
  <span style="color: #006600; font-style: italic;">// File target is a &lt;input type=&quot;file&quot; /&gt;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#file-upload&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</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;
    <span style="color: #006600; font-style: italic;">// Browser supports `files` as part of DOM</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">//  This works in Firefox, #image-preview is an &lt;img src=&quot;&quot; /&gt;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#image-preview&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> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">files</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// This is just wishful thinking, but it's a security issue so the value of the input is never</span>
      <span style="color: #006600; font-style: italic;">// a true local file path</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#image-preview&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> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#file-upload&quot;</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>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Firefox gives you a file name </span>
    <span style="color: #006600; font-style: italic;">// Safari / Chrome gives you nothing</span>
    <span style="color: #006600; font-style: italic;">// IE / Opera gives you a weird /fakepath/filename.ext</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#file-upload-area&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#file-upload&quot;</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>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#preview-area&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Ajax 图片上传</h2>
<blockquote><p><a href="http://www.zurb.com/playground/ajax_upload">http://www.zurb.com/playground/ajax_upload</a></p>
<p><a href="http://cssrainbow.cn/tutorials/javascript/499.html">图片上传预览，基于JavaScript</a></p>
</blockquote>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2010/08/29 -- <a href="http://cssrainbow.cn/tutorials/javascript/1368.html" title="怎么写Tab?基于JavaScript实现的">怎么写Tab?基于JavaScript实现的</a> (0)</li><li>2010/03/30 -- <a href="http://cssrainbow.cn/tutorials/jquery/1225.html" title="关于‘TOP’置顶链接的那些事儿">关于‘TOP’置顶链接的那些事儿</a> (0)</li><li>2010/03/22 -- <a href="http://cssrainbow.cn/code-snippets/javascript-code-snippets/1187.html" title="简单的斑马纹表格">简单的斑马纹表格</a> (2)</li><li>2010/01/31 -- <a href="http://cssrainbow.cn/code-snippets/javascript-code-snippets/1058.html" title="如何在事件代理中正确使用 focus 和 blur 事件">如何在事件代理中正确使用 focus 和 blur 事件</a> (0)</li><li>2010/01/26 -- <a href="http://cssrainbow.cn/tutorials/javascript/1027.html" title="理解JavaScript中的事件处理">理解JavaScript中的事件处理</a> (3)</li><li>2009/10/29 -- <a href="http://cssrainbow.cn/tutorials/jquery/790.html" title="Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合">Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合</a> (0)</li><li>2009/09/10 -- <a href="http://cssrainbow.cn/tutorials/jquery/706.html" title="窥探jQuery — 面向JavaScript程序员(收藏)">窥探jQuery — 面向JavaScript程序员(收藏)</a> (1)</li><li>2009/09/08 -- <a href="http://cssrainbow.cn/tutorials/javascript/703.html" title="有滑动效果的导航条，基于JavaScript">有滑动效果的导航条，基于JavaScript</a> (0)</li><li>2009/09/04 -- <a href="http://cssrainbow.cn/tutorials/javascript/693.html" title="FireBug 控制台指令">FireBug 控制台指令</a> (0)</li><li>2009/08/31 -- <a href="http://cssrainbow.cn/articles/resources/671.html" title="Opera的Web标准课程（推荐）">Opera的Web标准课程（推荐）</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/javascript/1354.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>简单的斑马纹表格</title>
		<link>http://cssrainbow.cn/code-snippets/javascript-code-snippets/1187.html</link>
		<comments>http://cssrainbow.cn/code-snippets/javascript-code-snippets/1187.html#comments</comments>
		<pubDate>Mon, 22 Mar 2010 01:43:05 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Zebra Table]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=1187</guid>
		<description><![CDATA[简单的斑马纹表格，如果页面上有大量的表格数据时，隔行变色的斑马纹会帮助我们快速阅读，有利于用户体验。我们今天不讨论在动态语言中的方法，只讨论CSS 和JavaScript是如何实现的，并有三种可行性方案。]]></description>
			<content:encoded><![CDATA[<p>
简单的斑马纹表格，如果页面上有大量的表格数据时，隔行变色的斑马纹会帮助我们快速阅读，有利于用户体验。我们今天不讨论在动态语言中的方法，只讨论CSS，JavaScript，MooTools 是如何实现的，并有三种可行性方案。</p>
<h2>我们的表格</h2>
<h3>The Html Code:</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;playlist&quot;</span> <span style="color: #000066;">cellspacing</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Lost In The Plot<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>The Dears<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Poison<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>The Constantines<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Plea From A Cat Named Virtute<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>The Weakerthans<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Melissa Louise<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Chixdiggit!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Living Room<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Tegan And Sara<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;more-1187&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>6<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Speed<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Bran Van 3000<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>7<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Fast Money Blessing<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>King Cobb Steelie<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>8<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Sore<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Buck 65<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>9<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Love Travel<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Danko Jones<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>10<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>You Never Let Me Down<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Furnaceface<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>	
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></td></tr></table></div>

<p>我们上面所看到的表格，就是我们要美化的表格，要实现斑马纹的表格。</p>
<p><img src="http://cssrainbow.cn/dw-content/demo14/zebra-table.png" width="490" height="417"  alt="斑马纹的表格"/></p>
<p>&nbsp;</p>
<p><a class="button" href="http://cssrainbow.cn/dw-content/demo14/zebra-table-demo.htm">演示</a></p>
<p>&nbsp;</p>
<h2>方案一 :</h2>
<p>在CSS3中有许多的伪类选择器，其中的</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">E<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>n<span style="color: #00AA00;">&#41;</span> ： <span style="color: #00AA00;">&#123;</span>attribute<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>它可以匹配父元素中的第n个子元素E。</p>
<blockquote>
<p>了解更多：<a href="http://www.w3.org/TR/2005/WD-css3-selectors-20051215/#Structural pseudo-classes">http://www.w3.org/TR/2005/WD-css3-selectors-20051215/#Structural pseudo-classes</a></p>
</blockquote>
<h3>The CSS3 Code</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*获得奇偶数的子元素*/</span>
tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>odd<span style="color: #00AA00;">&#41;</span>		<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>//所有奇数序子元素
tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>even<span style="color: #00AA00;">&#41;</span>		<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>//所有偶数序子元素
<span style="color: #808080; font-style: italic;">/*同上一样的作用*/</span>
tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>2n<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span> //返回偶数序的子元素
tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>2n<span style="color: #00AA00;">+</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span> //返回奇数序的子元素</pre></td></tr></table></div>

<h2>方案二 :</h2>
<h3>The JavaScript Code</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
  <span style="color: #006600; font-style: italic;">// this function is need to work around </span>
  <span style="color: #006600; font-style: italic;">// a bug in IE related to element attributes</span>
  <span style="color: #003366; font-weight: bold;">function</span> hasClass<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">getAttributeNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         result <span style="color: #339933;">=</span> obj.<span style="color: #660066;">getAttributeNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>   
&nbsp;
 <span style="color: #003366; font-weight: bold;">function</span> stripe<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// the flag we'll use to keep track of </span>
    <span style="color: #006600; font-style: italic;">// whether the current row is odd or even</span>
    <span style="color: #003366; font-weight: bold;">var</span> even <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if arguments are provided to specify the colours</span>
    <span style="color: #006600; font-style: italic;">// of the even &amp; odd rows, then use the them;</span>
    <span style="color: #006600; font-style: italic;">// otherwise use the following defaults:</span>
    <span style="color: #003366; font-weight: bold;">var</span> evenColor <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">?</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#fff&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> oddColor <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">?</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#eee&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// obtain a reference to the desired table</span>
    <span style="color: #006600; font-style: italic;">// if no such table exists, abort</span>
    <span style="color: #003366; font-weight: bold;">var</span> table <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> table<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>
&nbsp;
    <span style="color: #006600; font-style: italic;">// by definition, tables can have more than one tbody</span>
    <span style="color: #006600; font-style: italic;">// element, so we'll have to get the list of child</span>
    <span style="color: #006600; font-style: italic;">// &amp;lt;tbody&amp;gt;s </span>
    <span style="color: #003366; font-weight: bold;">var</span> tbodies <span style="color: #339933;">=</span> table.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tbody&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// and iterate through them...</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> h <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> h <span style="color: #339933;">&lt;</span> tbodies.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> h<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #006600; font-style: italic;">// find all the &amp;lt;tr&amp;gt; elements... </span>
      <span style="color: #003366; font-weight: bold;">var</span> trs <span style="color: #339933;">=</span> tbodies<span style="color: #009900;">&#91;</span>h<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// ... and iterate through them</span>
      <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> trs.<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>
&nbsp;
	    <span style="color: #006600; font-style: italic;">// avoid rows that have a class attribute</span>
        <span style="color: #006600; font-style: italic;">// or backgroundColor style</span>
	    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>hasClass<span style="color: #009900;">&#40;</span>trs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> trs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
         <span style="color: #006600; font-style: italic;">// get all the cells in this row...</span>
          <span style="color: #003366; font-weight: bold;">var</span> tds <span style="color: #339933;">=</span> trs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #006600; font-style: italic;">// and iterate through them...</span>
          <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> tds.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> mytd <span style="color: #339933;">=</span> tds<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// avoid cells that have a class attribute</span>
            <span style="color: #006600; font-style: italic;">// or backgroundColor style</span>
	        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> hasClass<span style="color: #009900;">&#40;</span>mytd<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> mytd.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		      mytd.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> even <span style="color: #339933;">?</span> evenColor <span style="color: #339933;">:</span> oddColor<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #006600; font-style: italic;">// flip from odd to even, or vice-versa</span>
        even <span style="color: #339933;">=</span>  <span style="color: #339933;">!</span> even<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
window.<span style="color: #000066;">onload</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>stripe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'playlist'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#fff'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#eee'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a class="button" href="http://www.alistapart.com/d/stripedtables/example.html">演示</a> </p>
<p>
在以前MooTools1.1的老版本中是不支持CSS3选择器的，那又如何实现那。
</p>
<h2>方案三 :</h2>
<h3>The CSS Code:</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">.odd<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
.even<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#3d80df</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>	
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>The MooTools JavaScript：</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'domready'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> count <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table.shade-table tr'</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>el<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		el.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">++</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'odd'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'even'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote>
<p>注意：</p>
<p>关于el.addClass(count++ % 2 == 0 ? &#8216;odd&#8217; : &#8216;even&#8217;); 语句，<a href="http://nootn.com/blog/link/">asins</a> 老兄有更好的写法：<strong>el.addClass(['odd', 'even'][count++ % 2]);</strong></p>
</blockquote>
<p>如果你已经使用了MooTools1.2+的版本，我们就可以用MooTools Selectors的伪类选择器，它的语法是类似于CSS3的伪类选择器的。 </p>
<h3>The MooTools JavaScript：</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table#playlist tr:nth-child(odd)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'odd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table#playlist tr:nth-child(even)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'even'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">/*
$$('table#playlist tr:nth-child(2n+1)').addClass('odd');
&nbsp;
$$('table#playlist tr:nth-child(2n)').addClass('even');*/</span></pre></td></tr></table></div>

<h3>在鼠标经过时高亮表格行列</h3>
<h3>The CSS Code:</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">.over<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#F00</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>The MooTools JavaScript:</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table#playlist tr&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;over&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;over&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p><a class="button" href="http://cssrainbow.cn/dw-content/demo14/zebra-table-demo.htm">演示</a></p>
<p>&nbsp;</p>
<p>&#8211; The End &#8211;</p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2010/03/30 -- <a href="http://cssrainbow.cn/tutorials/jquery/1225.html" title="关于‘TOP’置顶链接的那些事儿">关于‘TOP’置顶链接的那些事儿</a> (0)</li><li>2009/11/21 -- <a href="http://cssrainbow.cn/tutorials/mootools/821.html" title="斑马纹的表格效果,基于MooTools">斑马纹的表格效果,基于MooTools</a> (0)</li><li>2009/10/29 -- <a href="http://cssrainbow.cn/tutorials/jquery/790.html" title="Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合">Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合</a> (0)</li><li>2009/08/19 -- <a href="http://cssrainbow.cn/articles/general/646.html" title="Web前端开发需要掌握的技能(图解)">Web前端开发需要掌握的技能(图解)</a> (0)</li><li>2009/05/18 -- <a href="http://cssrainbow.cn/tutorials/mootools/222.html" title="用mooStack，实现一个堆叠效果的幻灯片">用mooStack，实现一个堆叠效果的幻灯片</a> (0)</li><li>2010/09/05 -- <a href="http://cssrainbow.cn/tutorials/mootools/1378.html" title="MooTools TinyTooltips">MooTools TinyTooltips</a> (0)</li><li>2010/08/29 -- <a href="http://cssrainbow.cn/tutorials/javascript/1368.html" title="怎么写Tab?基于JavaScript实现的">怎么写Tab?基于JavaScript实现的</a> (0)</li><li>2010/08/25 -- <a href="http://cssrainbow.cn/tutorials/mootools/1365.html" title="自己动手写MooTools Tabs Class">自己动手写MooTools Tabs Class</a> (0)</li><li>2010/08/08 -- <a href="http://cssrainbow.cn/articles/resources/1357.html" title="9大MooTools Plugins来改善网站的用户体验">9大MooTools Plugins来改善网站的用户体验</a> (0)</li><li>2010/07/27 -- <a href="http://cssrainbow.cn/tutorials/javascript/1354.html" title="本地图片预览二三事">本地图片预览二三事</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/code-snippets/javascript-code-snippets/1187.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>如何在事件代理中正确使用 focus 和 blur 事件</title>
		<link>http://cssrainbow.cn/code-snippets/javascript-code-snippets/1058.html</link>
		<comments>http://cssrainbow.cn/code-snippets/javascript-code-snippets/1058.html#comments</comments>
		<pubDate>Sun, 31 Jan 2010 04:20:20 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Event Delegation]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=1058</guid>
		<description><![CDATA[让focus 和 blur 都支持冒泡事件的解决方案]]></description>
			<content:encoded><![CDATA[<p class="node">
<strong>让focus 和 blur 都支持冒泡事件的解决方案来自：</strong></p>
<p class="note">怿飞的《<a href="http://www.planabc.net/2010/01/30/how_to_use_focus_and_blur_event_in_event_delegation/">如何在事件代理中正确使用 focus 和 blur 事件</a>》文章。 </p>
<h2>什么是事件代理（Event Delegation）？</h2>
<blockquote>
<p><strong>如果不太了解的朋友，可详细阅读以下文章：</strong></p>
<ul>
<li><a href="http://www.nczonline.net/blog/2009/06/30/event-delegation-in-javascript/" target="_blank" title="Event delegation in JavaScript">Event delegation in JavaScript</a></li>
<li><a href="http://www.cnblogs.com/rubylouvre/archive/2009/08/09/1542174.html">JavaScript的事件代理</a></li>
<li>  <a href="http://developer.51cto.com/art/200901/107183.htm">如何使用JavaScript的事件代理</a></li>
<p><span id="more-1058"></span></p>
</ul>
</blockquote>
<h2>首先让我们一起来回顾一些常识：</h2>
<ol>
<li>通常支持事件冒泡（Event Bubbling）的事件类型为鼠标事件和键盘事件，例如：<strong>mouseover, mouseout, click, keydown, keypress</strong>。</li>
<li>接口事件则通常不支持事件冒泡（Event Bubbling），例如：<strong>load, change, submit, focus, blur</strong>。</li>
</ol>
<p>很明显：focus 和 blur 都属于不支持冒泡的接口事件。既然都不支持冒泡，那又如何实现事件代理呢？</p>
<p>可以换个角度，逆向思维，尝试事件捕获（Event Capturing，除了IE，现在流行的标准浏览器均支持）。</p>
<p>测试后会发现，如果你捕获 focus 或 blur 事件，目标元素的祖先元素均执行事件函数。至于为什么？或许是实现的一个 BUG。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span> focusHandler<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>
el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span> blurHandler<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></pre></td></tr></table></div>

<h3>那对于 IE ，我们如何实现呢？</h3>
<p>非常幸运，IE 下支持 focusin 和 focusout 事件，非常类似于 focus 和 blur 事件，唯一不同的是，这两种事件支持事件冒泡（Event Bubbling）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #660066;">onfocusin</span> <span style="color: #339933;">=</span> focusHandler<span style="color: #339933;">;</span>
el.<span style="color: #660066;">onfocusout</span> <span style="color: #339933;">=</span> blurHandler<span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>很完美的解决方案：</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span> focusHandler<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>
    el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span> blurHandler<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>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    el.<span style="color: #660066;">onfocusin</span> <span style="color: #339933;">=</span> focusHandler<span style="color: #339933;">;</span>
    el.<span style="color: #660066;">onfocusout</span> <span style="color: #339933;">=</span> blurHandler<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<h3>知识扩展：</h3>
<blockquote>
<p><strong>主流类库的事件代理</strong>：</p>
<ul>
<li>  <a href="http://davidwalsh.name/mootools-event-delegation">Event Delegation with MooTools</a></li>
<li>
<a href="http://www.danwebb.net/2008/2/8/event-delegation-made-easy-in-jquery">Event delegation with jQuery</a>
</li>
<li>
<a href="http://devthatweb.com/view/basic-event-delegation-in-prototype">Event delegation with Prototype</a>
</li>
<li>
<a href="http://icant.co.uk/sandbox/eventdelegation/">Event delegation with YUI</a>
</li>
</ul>
</blockquote>
<p>&nbsp;</p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2010/08/29 -- <a href="http://cssrainbow.cn/tutorials/javascript/1368.html" title="怎么写Tab?基于JavaScript实现的">怎么写Tab?基于JavaScript实现的</a> (0)</li><li>2010/07/27 -- <a href="http://cssrainbow.cn/tutorials/javascript/1354.html" title="本地图片预览二三事">本地图片预览二三事</a> (1)</li><li>2010/03/30 -- <a href="http://cssrainbow.cn/tutorials/jquery/1225.html" title="关于‘TOP’置顶链接的那些事儿">关于‘TOP’置顶链接的那些事儿</a> (0)</li><li>2010/03/22 -- <a href="http://cssrainbow.cn/code-snippets/javascript-code-snippets/1187.html" title="简单的斑马纹表格">简单的斑马纹表格</a> (2)</li><li>2010/01/26 -- <a href="http://cssrainbow.cn/tutorials/javascript/1027.html" title="理解JavaScript中的事件处理">理解JavaScript中的事件处理</a> (3)</li><li>2009/10/29 -- <a href="http://cssrainbow.cn/tutorials/jquery/790.html" title="Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合">Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合</a> (0)</li><li>2009/09/10 -- <a href="http://cssrainbow.cn/tutorials/jquery/706.html" title="窥探jQuery — 面向JavaScript程序员(收藏)">窥探jQuery — 面向JavaScript程序员(收藏)</a> (1)</li><li>2009/09/08 -- <a href="http://cssrainbow.cn/tutorials/javascript/703.html" title="有滑动效果的导航条，基于JavaScript">有滑动效果的导航条，基于JavaScript</a> (0)</li><li>2009/09/04 -- <a href="http://cssrainbow.cn/tutorials/javascript/693.html" title="FireBug 控制台指令">FireBug 控制台指令</a> (0)</li><li>2009/08/31 -- <a href="http://cssrainbow.cn/articles/resources/671.html" title="Opera的Web标准课程（推荐）">Opera的Web标准课程（推荐）</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/code-snippets/javascript-code-snippets/1058.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
