<?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/tutorials/javascript/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>怎么写Tab?基于JavaScript实现的</title>
		<link>http://cssrainbow.cn/tutorials/javascript/1368.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/1368.html#comments</comments>
		<pubDate>Sun, 29 Aug 2010 14:52:34 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=1368</guid>
		<description><![CDATA[跟着阿当一步一步的学习怎么写tab,基于JavaScript实现的。

原文作者：阿当
原文链接地址：怎么写tab?
阿当的书：《编写高质量代码——web前端开发修炼之道》

演示地址： http://www.adanghome.com/js_demo/3/
一个getElementsByClassName 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var getElementsByClassName = function&#40;str,root,tag&#41;&#123;
	if&#40;root&#41;&#123;
		root = typeof root == &#34;string&#34; ? document.getElementById&#40;root&#41; : root;
	&#125; else &#123;
		root = document.body;
	&#125;
	tag = tag &#124;&#124; &#34;*&#34;;
	var els = root.getElementsByTagName&#40;tag&#41;,arr = &#91;&#93;;
	for&#40;var i=0,n=els.length;i&#60;n;i++&#41;&#123;
		for&#40;var j=0,k=els&#91;i&#93;.className.split&#40;&#34; &#34;&#41;,l=k.length;j&#60;l;j++&#41;&#123;
			if&#40;k&#91;j&#93; == str&#41;&#123;
				arr.push&#40;els&#91;i&#93;&#41;;
				break;
			&#125;
		&#125;
	&#125;
	return arr;
&#125;


版本一
The CSS Code:

1
#t1_m1,#t1_m2,#t1_m3&#123;padding:5px;border:dashed 1px #333;margin:0 5px;&#125;

The HTML Code:

1
2
3
4
5
6
7
8
&#60;div id=&#34;demo1&#34;&#62;
    &#60;div&#62;
        &#60;span id=&#34;t1_m1&#34;&#62;menu1&#60;/span&#62;
 [...]]]></description>
			<content:encoded><![CDATA[<p>跟着阿当一步一步的学习怎么写tab,基于JavaScript实现的。</p>
<blockquote>
<p><strong>原文作者：</strong><a href="http://www.adanghome.com">阿当</a></p>
<p><strong>原文链接地址：</strong><a href="http://hi.baidu.com/cly84920/blog">怎么写tab?</a></p>
<p><strong>阿当的书：</strong>《<a href="http://www.china-pub.com/196861">编写高质量代码——web前端开发修炼之道</a>》</p>
</blockquote>
<p><strong>演示地址： </strong><a target="_blank" href="http://www.adanghome.com/js_demo/3/">http://www.adanghome.com/js_demo/3/</a></p>
<h2>一个getElementsByClassName 方法</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> getElementsByClassName <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span>root<span style="color: #339933;">,</span>tag<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>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		root <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">typeof</span> root <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;string&quot;</span> <span style="color: #339933;">?</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> root<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>
		root <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	tag <span style="color: #339933;">=</span> tag <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;*&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> els <span style="color: #339933;">=</span> root.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>tag<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</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>n<span style="color: #339933;">=</span>els.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>k<span style="color: #339933;">=</span>els<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span>.<span style="color: #660066;">split</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>l<span style="color: #339933;">=</span>k.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">;</span>j<span style="color: #339933;">++</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>k<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> str<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				arr.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>els<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> arr<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-1368"></span></p>
<h2>版本一</h2>
<h3>The CSS Code:</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#t1_m1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#t1_m2</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#t1_m3</span><span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>The HTML 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="html4strict" style="font-family:monospace;"><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;demo1&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
        <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;t1_m1&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
        <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;t1_m2&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
        <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;t1_m3&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <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;t1_c1&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> menu1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t1_m1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			menu2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t1_m2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			menu3 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t1_m3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			content <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t1_c1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		menu1.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			content.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;11111&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		menu2.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			content.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;22222&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		menu3.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			content.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;33333&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本二</h2>
<h3>The CSS Code :</h3>
<pre>
#t2_m1,#t2_m2,#t2_m3{padding:5px;border:dashed 1px #333;margin:0 5px;}
#t2_c2,#t2_c3{display:none;}
</pre>
<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo2&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<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;t2_m1&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
	<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;t2_m2&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
	<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;t2_m3&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<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;t2_c1&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<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;t2_c2&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<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;t2_c3&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> menu1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t2_m1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			menu2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t2_m2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			menu3 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t2_m3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			content1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t2_c1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			content2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t2_c2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			content3 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;t2_c3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		menu1.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			content1.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
			content2.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
			content3.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		menu2.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			content1.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
			content2.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
			content3.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		menu3.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			content1.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
			content2.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
			content3.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本三</h2>
<h3>The CSS Code :</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#tab_menu</span> span<span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo3&quot;</span>&gt;</span>
<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;tab_menu&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span>&gt;</span>menu4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<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;tab_content&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;none&quot;</span>&gt;</span>44444<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> menus <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tab_menu&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			contents <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tab_content&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>n<span style="color: #339933;">=</span>menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				menus<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					showContent<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>n<span style="color: #339933;">=</span>contents.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
					contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</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><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本四</h2>
<h3>The CSS Code :</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#demo4-1</span> ul<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.demo41_tabMenu</span><span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>display<span style="color: #00AA00;">:</span><span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo4-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo41_tabMenu&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo41_tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo41_tabMenu&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo41_tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo41_tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo41_tabContent none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&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;demo4-2&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sex&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo42_tabMenu&quot;</span> <span style="color: #000066;">checked</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checked&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span>&gt;</span>男<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sex&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;female&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo42_tabMenu&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;female&quot;</span>&gt;</span>女<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo42_tabContent&quot;</span>&gt;</span>I'm a boy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;demo42_tabContent none&quot;</span>&gt;</span>I'm a girl<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> menus <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo41_tabMenu&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo4-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			contents <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo41_tabContent&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo4-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			menus2 <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo42_tabMenu&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo4-2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			contents2 <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo42_tabContent&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo4-2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>n<span style="color: #339933;">=</span>menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				menus<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					showContent<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>n<span style="color: #339933;">=</span>contents.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
					contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&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>n<span style="color: #339933;">=</span>menus2.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				menus2<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					showContent2<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">function</span> showContent2<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>n<span style="color: #339933;">=</span>contents2.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					contents2<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
					contents2<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</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><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本五</h2>
<h3>The CSS Code :</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#demo5-1</span> ul<span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo5-2</span> ul<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#demo5-1</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo5-2</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>display<span style="color: #00AA00;">:</span><span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo5-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&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;demo5-2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">function</span> createTab<span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> menus <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabMenu&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				contents <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabContent&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>n<span style="color: #339933;">=</span>menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					menus<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						showContent<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>n<span style="color: #339933;">=</span>contents.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		createTab<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo5-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createTab<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo5-2&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: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本六</h2>
<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;"><span style="color: #cc00cc;">#demo6-1</span> ul<span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo6-2</span> ul<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#demo6-1</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo6-2</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>display<span style="color: #00AA00;">:</span><span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.demo61-tabMenu-selected</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span>color<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: #6666ff;">.demo62-tabMenu-selected</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>color<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 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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo6-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo61-tabMenu-selected&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&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;demo6-2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo62-tabMenu-selected&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">function</span> createTab<span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span>selectMenuClassname<span style="color: #339933;">,</span>eventType<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> menus <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabMenu&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				contents <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabContent&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				selectMenuClassname <span style="color: #339933;">=</span> selectMenuClassname <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
				eventType <span style="color: #339933;">=</span> eventType <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">;</span>
&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>n<span style="color: #339933;">=</span>menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					menus<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span><span style="color: #339933;">+</span>eventType<span style="color: #009900;">&#93;</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>
						showContent<span style="color: #009900;">&#40;</span>n<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>selectMenuClassname<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
							focusMenu<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>n<span style="color: #339933;">=</span>contents.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> focusMenu<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> menuFocused <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span>selectMenuClassname<span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				menuFocused.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> menuFocused.<span style="color: #660066;">className</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span>selectMenuClassname<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				menus<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> selectMenuClassname<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		createTab<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo6-1&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo61-tabMenu-selected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createTab<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo6-2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo62-tabMenu-selected&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mouseover&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: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本七</h2>
<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;"><span style="color: #cc00cc;">#demo7-1</span> ul<span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo7-2</span> ul<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#demo7-1</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo7-2</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>display<span style="color: #00AA00;">:</span><span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.demo71-tabMenu-selected</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span>color<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: #6666ff;">.demo72-tabMenu-selected</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>color<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 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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo7-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu  &quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo71-tabMenu-selected&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: block;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&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;demo7-2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo72-tabMenu-selected&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">function</span> createTab<span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span>selectMenuClassname<span style="color: #339933;">,</span>eventType<span style="color: #339933;">,</span>autoPlay<span style="color: #339933;">,</span>stayTime<span style="color: #339933;">,</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> menus <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabMenu&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				contents <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabContent&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				selectMenuClassname <span style="color: #339933;">=</span> selectMenuClassname <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
				eventType <span style="color: #339933;">=</span> eventType <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span>
				autoPlay <span style="color: #339933;">=</span> autoPlay <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
				stayTime <span style="color: #339933;">=</span> stayTime <span style="color: #339933;">||</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span>
				currentIndex <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&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>n<span style="color: #339933;">=</span>menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					menus<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span><span style="color: #339933;">+</span>eventType<span style="color: #009900;">&#93;</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>
						showItem<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>autoPlay<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				setInterval<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>
					itemsCount <span style="color: #339933;">=</span> menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
					currentIndex <span style="color: #339933;">=</span> currentIndex <span style="color: #339933;">&gt;=</span> itemsCount <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">:</span> currentIndex<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">;</span> 
					showItem<span style="color: #009900;">&#40;</span>currentIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>stayTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> showItem<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				showContent<span style="color: #009900;">&#40;</span>index<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>selectMenuClassname<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					focusMenu<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					callback<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				currentIndex <span style="color: #339933;">=</span> index<span style="color: #339933;">;</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>n<span style="color: #339933;">=</span>contents.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> focusMenu<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> menuFocused <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span>selectMenuClassname<span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				menuFocused.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> menuFocused.<span style="color: #660066;">className</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span>selectMenuClassname<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				menus<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> selectMenuClassname<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		createTab<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo7-1&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo71-tabMenu-selected&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createTab<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;demo7-2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;demo72-tabMenu-selected&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mouseover&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;您激活了第&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;个tab项&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>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>版本八</h2>
<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;"><span style="color: #cc00cc;">#demo8-1</span> ul<span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo8-2</span> ul<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#demo8-1</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#demo8-2</span> <span style="color: #6666ff;">.tabMenu</span><span style="color: #00AA00;">&#123;</span><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>border<span style="color: #00AA00;">:</span><span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>display<span style="color: #00AA00;">:</span><span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.demo81-tabMenu-selected</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span>color<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: #6666ff;">.demo82-tabMenu-selected</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>color<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 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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo8-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu  &quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo81-tabMenu-selected&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: block;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&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;demo8-2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo82-tabMenu-selected&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">function</span> createTab<span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> config.<span style="color: #660066;">root</span><span style="color: #339933;">,</span>
				menus <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabMenu&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				contents <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabContent&quot;</span><span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				selectMenuClassname <span style="color: #339933;">=</span> config.<span style="color: #660066;">selectMenuClassname</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
				eventType <span style="color: #339933;">=</span> config.<span style="color: #660066;">eventType</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span>
				autoPlay <span style="color: #339933;">=</span> config.<span style="color: #660066;">autoPlay</span> <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
				stayTime <span style="color: #339933;">=</span> config.<span style="color: #660066;">stayTime</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span>
				callback <span style="color: #339933;">=</span> config.<span style="color: #660066;">callback</span> <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
				currentIndex <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&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>n<span style="color: #339933;">=</span>menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					menus<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span><span style="color: #339933;">+</span>eventType<span style="color: #009900;">&#93;</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>
						showItem<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>autoPlay<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				setInterval<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>
					itemsCount <span style="color: #339933;">=</span> menus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
					currentIndex <span style="color: #339933;">=</span> currentIndex <span style="color: #339933;">&gt;=</span> itemsCount <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">:</span> currentIndex<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">;</span> 
					showItem<span style="color: #009900;">&#40;</span>currentIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>stayTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> showItem<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				showContent<span style="color: #009900;">&#40;</span>index<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>selectMenuClassname<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					focusMenu<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					callback<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> showContent<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				currentIndex <span style="color: #339933;">=</span> index<span style="color: #339933;">;</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>n<span style="color: #339933;">=</span>contents.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</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>
						contents<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> focusMenu<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> menuFocused <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span>selectMenuClassname<span style="color: #339933;">,</span>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				menuFocused.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> menuFocused.<span style="color: #660066;">className</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span>selectMenuClassname<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				menus<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> selectMenuClassname<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		createTab<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>root<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo8-1&quot;</span><span style="color: #339933;">,</span>selectMenuClassname<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo81-tabMenu-selected&quot;</span><span style="color: #339933;">,</span>autoPlay<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>stayTime<span style="color: #339933;">:</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createTab<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>root<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo8-2&quot;</span><span style="color: #339933;">,</span>selectMenuClassname<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo82-tabMenu-selected&quot;</span><span style="color: #339933;">,</span>eventType<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;mouseover&quot;</span><span style="color: #339933;">,</span>callback<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;您激活了第&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;个tab项&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<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
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
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">	<span style="color: #00AA00;">&#40;</span>function<span style="color: #00AA00;">&#40;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
		function createTab<span style="color: #00AA00;">&#40;</span>config<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
			var root <span style="color: #00AA00;">=</span> config.root<span style="color: #00AA00;">,</span>
				menus <span style="color: #00AA00;">=</span> getElementsByClassName<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span><span style="color: #00AA00;">,</span>root<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>
				contents <span style="color: #00AA00;">=</span> getElementsByClassName<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;tabContent&quot;</span><span style="color: #00AA00;">,</span>root<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>
				selectMenuClassname <span style="color: #00AA00;">=</span> config<span style="color: #6666ff;">.selectMenuClassname</span> || <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #00AA00;">,</span>
				eventType <span style="color: #00AA00;">=</span> config<span style="color: #6666ff;">.eventType</span> || <span style="color: #ff0000;">&quot;click&quot;</span><span style="color: #00AA00;">,</span>
				autoPlay <span style="color: #00AA00;">=</span> config<span style="color: #6666ff;">.autoPlay</span> || false<span style="color: #00AA00;">,</span>
				stayTime <span style="color: #00AA00;">=</span> config<span style="color: #6666ff;">.stayTime</span> || <span style="color: #cc66cc;">5000</span><span style="color: #00AA00;">,</span>
				callback <span style="color: #00AA00;">=</span> config<span style="color: #6666ff;">.callback</span> || null<span style="color: #00AA00;">,</span>
				currentIndex <span style="color: #00AA00;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
&nbsp;
			for<span style="color: #00AA00;">&#40;</span>var i <span style="color: #00AA00;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span>n<span style="color: #00AA00;">=</span>menus.length<span style="color: #00AA00;">;</span>i&lt;n<span style="color: #00AA00;">;</span>i<span style="color: #00AA00;">++</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
				<span style="color: #00AA00;">&#40;</span>function<span style="color: #00AA00;">&#40;</span>n<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
					menus<span style="color: #00AA00;">&#91;</span>n<span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">&#91;</span><span style="color: #ff0000;">&quot;on&quot;</span><span style="color: #00AA00;">+</span>eventType<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">=</span> function<span style="color: #00AA00;">&#40;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
						showItem<span style="color: #00AA00;">&#40;</span>n<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
					<span style="color: #00AA00;">&#125;</span>
				<span style="color: #00AA00;">&#125;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#40;</span>i<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
			<span style="color: #00AA00;">&#125;</span>
&nbsp;
			if<span style="color: #00AA00;">&#40;</span>autoPlay<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
				setInterval<span style="color: #00AA00;">&#40;</span>function<span style="color: #00AA00;">&#40;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
					itemsCount <span style="color: #00AA00;">=</span> menus.length<span style="color: #00AA00;">;</span>
					currentIndex <span style="color: #00AA00;">=</span> currentIndex <span style="color: #00AA00;">&gt;=</span> itemsCount - <span style="color: #cc66cc;">1</span> ? <span style="color: #cc66cc;">0</span> <span style="color: #00AA00;">:</span> currentIndex<span style="color: #00AA00;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #00AA00;">;</span> 
					showItem<span style="color: #00AA00;">&#40;</span>currentIndex<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span><span style="color: #00AA00;">,</span>stayTime<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
			<span style="color: #00AA00;">&#125;</span>
&nbsp;
			function showItem<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
				showContent<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
				if<span style="color: #00AA00;">&#40;</span>selectMenuClassname<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
					focusMenu<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span>
				if<span style="color: #00AA00;">&#40;</span>callback<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
					callback<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span>
			<span style="color: #00AA00;">&#125;</span>
&nbsp;
			function showContent<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
				currentIndex <span style="color: #00AA00;">=</span> index<span style="color: #00AA00;">;</span>
				for<span style="color: #00AA00;">&#40;</span>var i<span style="color: #00AA00;">=</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span>n<span style="color: #00AA00;">=</span>contents.length<span style="color: #00AA00;">;</span>i&lt;n<span style="color: #00AA00;">;</span>i<span style="color: #00AA00;">++</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
					if<span style="color: #00AA00;">&#40;</span>i<span style="color: #00AA00;">==</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
						contents<span style="color: #00AA00;">&#91;</span>i<span style="color: #00AA00;">&#93;</span><span style="color: #6666ff;">.style</span><span style="color: #6666ff;">.display</span> <span style="color: #00AA00;">=</span> <span style="color: #ff0000;">&quot;block&quot;</span><span style="color: #00AA00;">;</span>
					<span style="color: #00AA00;">&#125;</span> else <span style="color: #00AA00;">&#123;</span>
						contents<span style="color: #00AA00;">&#91;</span>i<span style="color: #00AA00;">&#93;</span><span style="color: #6666ff;">.style</span><span style="color: #6666ff;">.display</span> <span style="color: #00AA00;">=</span> <span style="color: #ff0000;">&quot;none&quot;</span><span style="color: #00AA00;">;</span>
					<span style="color: #00AA00;">&#125;</span>
				<span style="color: #00AA00;">&#125;</span>
			<span style="color: #00AA00;">&#125;</span>
&nbsp;
			function focusMenu<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>
				var menuFocused <span style="color: #00AA00;">=</span> getElementsByClassName<span style="color: #00AA00;">&#40;</span>selectMenuClassname<span style="color: #00AA00;">,</span>root<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">;</span>
				menuFocused<span style="color: #6666ff;">.className</span> <span style="color: #00AA00;">=</span> menuFocused<span style="color: #6666ff;">.className</span>.split<span style="color: #00AA00;">&#40;</span>selectMenuClassname<span style="color: #00AA00;">&#41;</span>.join<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
				menus<span style="color: #00AA00;">&#91;</span>index<span style="color: #00AA00;">&#93;</span><span style="color: #6666ff;">.className</span> <span style="color: #00AA00;">+=</span> <span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #00AA00;">+</span> selectMenuClassname<span style="color: #00AA00;">;</span>
			<span style="color: #00AA00;">&#125;</span>
		<span style="color: #00AA00;">&#125;</span>
&nbsp;
		createTab<span style="color: #00AA00;">&#40;</span><span style="color: #00AA00;">&#123;</span>root<span style="color: #00AA00;">:</span><span style="color: #ff0000;">&quot;demo8-1&quot;</span><span style="color: #00AA00;">,</span>selectMenuClassname<span style="color: #00AA00;">:</span><span style="color: #ff0000;">&quot;demo81-tabMenu-selected&quot;</span><span style="color: #00AA00;">,</span>autoPlay<span style="color: #3333ff;">:true</span><span style="color: #00AA00;">,</span>stayTime<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">8000</span><span style="color: #00AA00;">&#125;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
		createTab<span style="color: #00AA00;">&#40;</span><span style="color: #00AA00;">&#123;</span>root<span style="color: #00AA00;">:</span><span style="color: #ff0000;">&quot;demo8-2&quot;</span><span style="color: #00AA00;">,</span>selectMenuClassname<span style="color: #00AA00;">:</span><span style="color: #ff0000;">&quot;demo82-tabMenu-selected&quot;</span><span style="color: #00AA00;">,</span>eventType<span style="color: #00AA00;">:</span><span style="color: #ff0000;">&quot;mouseover&quot;</span><span style="color: #00AA00;">,</span>callback<span style="color: #00AA00;">:</span>function<span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#123;</span>alert<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;您激活了第&quot;</span><span style="color: #00AA00;">+</span><span style="color: #00AA00;">&#40;</span>index<span style="color: #00AA00;">+</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">+</span><span style="color: #ff0000;">&quot;个tab项&quot;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><span style="color: #00AA00;">&#125;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
	<span style="color: #00AA00;">&#125;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#40;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;demo9-1&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu  &quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo91-tabMenu-selected&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: block;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>33333<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;激活第3个tab&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;btn&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<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;demo9-2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu demo92-tabMenu-selected&quot;</span>&gt;</span>menu1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabMenu&quot;</span>&gt;</span>menu2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mt30&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent&quot;</span>&gt;</span>11111<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>一一一一一一一<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabContent none&quot;</span>&gt;</span>22222<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>二二二<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<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;">	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">function</span> Tab<span style="color: #009900;">&#40;</span>config<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;">root</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">root</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">menus</span> <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabMenu&quot;</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contents</span> <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabContent&quot;</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectMenuClassname</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">selectMenuClassname</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">eventType</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">eventType</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">autoPlay</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">autoPlay</span> <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;">this</span>.<span style="color: #660066;">stayTime</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">stayTime</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callback</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">callback</span> <span style="color: #339933;">||</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentIndex</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">this</span>._init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		Tab.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
			_init <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>._bindEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">this</span>._autoPlay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			_bindEvent <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> <span style="color: #000066; font-weight: bold;">This</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</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>n<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">menus</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<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;">menus</span><span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">eventType</span><span style="color: #009900;">&#93;</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;">showItem</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			_autoPlay <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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">autoPlay</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> <span style="color: #000066; font-weight: bold;">This</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
				setInterval<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>
					itemsCount <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">menus</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
					<span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">currentIndex</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">currentIndex</span> <span style="color: #339933;">&gt;=</span> itemsCount <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">currentIndex</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">;</span> 
					<span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">showItem</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">currentIndex</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">This</span>.<span style="color: #660066;">stayTime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			showItem <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">this</span>._showContent<span style="color: #009900;">&#40;</span>index<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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectMenuClassname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000066; font-weight: bold;">this</span>._focusMenu<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</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;">callback</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;">callback</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			_showContent <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<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;">currentIndex</span> <span style="color: #339933;">=</span> index<span style="color: #339933;">;</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>n<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contents</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</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>i<span style="color: #339933;">==</span>index<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;">contents</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">contents</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			_focusMenu <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> menuFocused <span style="color: #339933;">=</span> getElementsByClassName<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectMenuClassname</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">root</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				menuFocused.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> menuFocused.<span style="color: #660066;">className</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectMenuClassname</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">menus</span><span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectMenuClassname</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> tab1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tab<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>root<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo9-1&quot;</span><span style="color: #339933;">,</span>selectMenuClassname<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo91-tabMenu-selected&quot;</span><span style="color: #339933;">,</span>autoPlay<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>stayTime<span style="color: #339933;">:</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">new</span> Tab<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>root<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo9-2&quot;</span><span style="color: #339933;">,</span>selectMenuClassname<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo92-tabMenu-selected&quot;</span><span style="color: #339933;">,</span>eventType<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;mouseover&quot;</span><span style="color: #339933;">,</span>callback<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;您激活了第&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;个tab项&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;btn&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			tab1.<span style="color: #660066;">showItem</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>演示地址： </strong><a target="_blank" href="http://www.adanghome.com/js_demo/3/">http://www.adanghome.com/js_demo/3/</a></p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><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/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/1368.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>关于‘TOP’置顶链接的那些事儿</title>
		<link>http://cssrainbow.cn/tutorials/jquery/1225.html</link>
		<comments>http://cssrainbow.cn/tutorials/jquery/1225.html#comments</comments>
		<pubDate>Tue, 30 Mar 2010 05:50:19 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery 实例]]></category>
		<category><![CDATA[MooTools 实例]]></category>
		<category><![CDATA[交互设计]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=1225</guid>
		<description><![CDATA[‘TOP’置顶链接，说的通俗一点就是‘返回顶部的链接’，‘go to top ’一般都放在页面的底部，它可以快速返回页面顶部，以节省用户浏览页面的时间。 它主要的应用场景是
当你有一个很长的网页内容时，您通常要 把 ‘TOP’锚点链接 添加在页面底部，只要用户一点击‘TOP’链接 ,就可以马上回到 页面的顶部了。]]></description>
			<content:encoded><![CDATA[<p>‘TOP’置顶链接，说的通俗一点就是‘返回顶部的链接’，‘go to top ’一般都放在页面的底部，它可以快速返回页面顶部，以节省用户浏览页面的时间。 它主要的应用场景是<br />
当你有一个很长的网页内容时，您通常要 把 ‘TOP’锚点链接 添加在页面底部，只要用户一点击‘TOP’链接 ,就可以马上回到 页面的顶部了。</p>
<p>我们遇到的问题是：不是滚动到页面底部的时候才看到了‘TOP’,如果页面内容超过两屏以上时，用户有些心烦，我不想看了，我想回到顶部看一些其他的内容。<br />
如果我们只看了第一屏的文章，不想看了，可是‘TOP’在第二屏才会出现。</p>
<p>这时候有三种情况发生：</p>
<ol>
<li>发挥鼠标特长就是拖动浏览器的滚动条或是滚动鼠标滑轮，回到页面顶部。</li>
<li>继续硬着头皮往下看，看有没有‘TOP’,幸运的话，马上找到了，可以回到顶部了。（一般人心中是没有‘TOP’概念的，只有选择1 和3 的方法了）</li>
<li>直接关闭网页，或者重新打开网站，或者离开网站。</li>
</ol>
<p><span id="more-1225"></span></p>
<p>我想我们已经找到了问题的所在了。</p>
<p>我们有三种方案可以给用户带来良好的用户体验：</p>
<h2>方案一：在合适的地方，手动加入一个或多个‘TOP’链接。</h2>
<p>这是最原始的做法了，如果滚动太快，验，那就是我们给用户用脚本实现一下缓冲效果，而不是直接飙到顶部。</p>
<h3>The HTML :</h3>

<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;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gototop&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:void(0);&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;goTop();return false;&quot;</span>&gt;</span>Top of Page<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

<h3>The JavaScript :</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * 作者：我是UED ，http://www.iamued.com/qianduan/816.html
 * 回到页面顶部
 * @param acceleration 加速度
 * @param time 时间间隔 (毫秒)
 **/</span>
<span style="color: #003366; font-weight: bold;">function</span> goTop<span style="color: #009900;">&#40;</span>acceleration<span style="color: #339933;">,</span> time<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	acceleration <span style="color: #339933;">=</span> acceleration <span style="color: #339933;">||</span> <span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
	time <span style="color: #339933;">=</span> time <span style="color: #339933;">||</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> y2 <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> x3 <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> y3 <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">documentElement</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		x1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">scrollLeft</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		y1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">scrollTop</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		x2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">scrollLeft</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		y2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">scrollTop</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">var</span> x3 <span style="color: #339933;">=</span> window.<span style="color: #660066;">scrollX</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> y3 <span style="color: #339933;">=</span> window.<span style="color: #660066;">scrollY</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// 滚动条到页面顶部的水平距离</span>
	<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>x2<span style="color: #339933;">,</span> x3<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// 滚动条到页面顶部的垂直距离</span>
	<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>y1<span style="color: #339933;">,</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>y2<span style="color: #339933;">,</span> y3<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// 滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小</span>
	<span style="color: #003366; font-weight: bold;">var</span> speed <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">+</span> acceleration<span style="color: #339933;">;</span>
	window.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>x <span style="color: #009966; font-style: italic;">/ speed), Math.floor(y /</span> speed<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// 如果距离不为零, 继续调用迭代本函数</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> y <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> invokeFunction <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;goTop(&quot;</span> <span style="color: #339933;">+</span> acceleration <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;, &quot;</span> <span style="color: #339933;">+</span> time <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
		window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>invokeFunction<span style="color: #339933;">,</span> time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p><a href="http://cssrainbow.cn/dw-content/demo15/JavaScript-Top/top-link-javascript.htm" class="button">View Demo</a></p>
<p>&nbsp;</p>
<h2>方案二：‘TOP’默认是隐藏的，只要滚动条，滚动到一定高度时就显示，否则就隐藏。</h2>
<p>这样我可能想从中间某处回到页面的顶部成为可能。</p>
<h3>The HTML :</h3>

<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;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#top&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gototop&quot;</span> &gt;</span>Top of Page<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

<h3>The CSS :</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#gototop</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #993333;">green</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;">bold</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">green</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span>Lightgreen<span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#gototop</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">underline</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>The MooTools JavaScript :</h3>
<blockquote>
<p><strong>注意：</strong></p>
<p> 我们需要MooTools Core 库的同时，也需要MooTools More 库中的 Fx.Scroll.js 和 Fx.SmoothScroll.js 两大文件。</p>
</blockquote>

<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
</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;">new</span> SmoothScroll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span><span style="color: #CC0000;">700</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009966; font-style: italic;">/* go to top */</span>
	<span style="color: #003366; font-weight: bold;">var</span> go <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gototop'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	go.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'scroll'</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>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Browser.<span style="color: #660066;">Engine</span>.<span style="color: #660066;">trident4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		 go.<span style="color: #660066;">setStyles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			 <span style="color: #3366CC;">'position'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'absolute'</span><span style="color: #339933;">,</span>
			 <span style="color: #3366CC;">'bottom'</span><span style="color: #339933;">:</span> window.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
			 <span style="color: #3366CC;">'width'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span>
		 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		go.<span style="color: #660066;">fade</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">getScroll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'in'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'out'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p><a href="http://cssrainbow.cn/dw-content/demo15/MooTools-Top/top-link.htm" class="button">View Demo</a></p>
<p>&nbsp;</p>
<p>还有一个例子是动态生成‘TOP’。</p>
<h3>The MooTools JavaScript :</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * back-to-top: unobtrusive global 'back to top' link using mootools 1.2.x 
 * This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
 *   http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// hide the effect from IE6, better not having it at all than being choppy.</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Browser.<span style="color: #660066;">Engine</span>.<span style="color: #660066;">trident4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// element added onload for IE to avoid the &quot;operation aborted&quot; bug. not yet verified for IE8 as it's still on beta as of this modification.</span>
  window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Browser.<span style="color: #660066;">Engine</span>.<span style="color: #660066;">trident</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'load'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'domready'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> target_opacity <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.64</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">'id'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'back-to-top'</span><span style="color: #339933;">,</span> 
      <span style="color: #3366CC;">'styles'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        opacity<span style="color: #339933;">:</span> target_opacity<span style="color: #339933;">,</span>
        display<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
        position<span style="color: #339933;">:</span> <span style="color: #3366CC;">'fixed'</span><span style="color: #339933;">,</span>
        bottom<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        right<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        cursor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'pointer'</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">'text'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'back to top'</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">'tween'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span>
        onComplete<span style="color: #339933;">:</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> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>el.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'opacity'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> el.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">'events'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		  <span style="color: #006600; font-style: italic;">/*location是javascript里边管理地址栏的内置对象，比如location.href就管理页面的url，用 location.href=url就可以直接将页面重定向url。而location.hash则可以用来获取或设置页面的标签值。比如 http://ued.alimama.com#admin的location.hash=”#admin”,利用这个属性值可以实现很多效果。*/</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#top'</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> window.<span style="color: #660066;">scrollTo</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: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">/*把窗口内容滚动到指定的坐标。*/</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inject</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'scroll'</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> visible <span style="color: #339933;">=</span> window.<span style="color: #660066;">getScroll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">getSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>visible <span style="color: #339933;">==</span> arguments.<span style="color: #660066;">callee</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">last_state</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// fade if supported</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Fx <span style="color: #339933;">&amp;&amp;</span> Fx.<span style="color: #660066;">Tween</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>visible<span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'back-to-top'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fade</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hide'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'inline'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fade</span><span style="color: #009900;">&#40;</span>target_opacity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">else</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'back-to-top'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fade</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'out'</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>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'back-to-top'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>visible <span style="color: #339933;">?</span> <span style="color: #3366CC;">'inline'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      arguments.<span style="color: #660066;">callee</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">last_state</span> <span style="color: #339933;">=</span> visible
    <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>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p><a href="http://cssrainbow.cn/dw-content/demo15/MooTools-Top/back-to-top.html" class="button"> View Demo</a></p>
<p>&nbsp;</p>
<h3>The jQuery JavaScript :</h3>
<p>需要<a href="http://flesler.blogspot.com/2007/10/jqueryscrollto.html">jQuery&rsquo;s ScrollTo plugin</a> 插件添加一些平滑的锚。</p>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//plugin</span>
jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">topLink</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	settings <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		min<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
		fadeSpeed<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> settings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</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>
		<span style="color: #006600; font-style: italic;">//listen for scroll</span>
		<span style="color: #003366; font-weight: bold;">var</span> el <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: #339933;">;</span>
		el.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//in case the user forgot</span>
		$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">scroll</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> settings.<span style="color: #660066;">min</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				el.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">fadeSpeed</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;">fadeOut</span><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">fadeSpeed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//usage w/ smoothscroll</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//set the link</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#gototop'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">topLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		min<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>
		fadeSpeed<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">//smoothscroll</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#gototop'</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>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">300</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>

<p>&nbsp;</p>
<p><a href="http://cssrainbow.cn/dw-content/demo15/jQuery-Top/top-link-jQuery.htm" class="button"> View Demo</a></p>
<p>&nbsp;</p>
<blockquote>
<p><strong>注意：</strong></p>
<p>Please note that this version doesn’t work with Internet Explorer due to IE’s lack of CSS “position:fixed” support. Here is a shotty attempt at IE support:</p>
</blockquote>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
    <span style="color: #006600; font-style: italic;">//plugin</span>
    jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">topLink</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		settings <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			min<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
			fadeSpeed<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span>
			ieOffset<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> settings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</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>
			<span style="color: #006600; font-style: italic;">//listen for scroll</span>
			<span style="color: #003366; font-weight: bold;">var</span> el <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: #339933;">;</span>
			el.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//in case the user forgot</span>
			$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">scroll</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">//stupid IE hack</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>jQuery.<span style="color: #660066;">support</span>.<span style="color: #660066;">hrefNormalized</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					el.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
						<span style="color: #3366CC;">'position'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'absolute'</span><span style="color: #339933;">,</span>
						<span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> settings.<span style="color: #660066;">ieOffset</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> settings.<span style="color: #660066;">min</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					el.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">fadeSpeed</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;">fadeOut</span><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">fadeSpeed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#gototop'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">topLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		 min<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>
		 fadeSpeed<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #006600; font-style: italic;">//smoothscroll</span>
	 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#gototop'</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>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		 e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 $.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">300</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>

<p>&nbsp;</p>
<p><a href="http://cssrainbow.cn/dw-content/demo15/jQuery-Top/top-link-jQuery-IE.htm" class="button"> View Demo</a></p>
<p>&nbsp;</p>
<h2>方案三：在浮动的工具条中加入‘Top’ 或 ‘Bottom’锚点链接。</h2>
<p>
我说的浮动工具条，可以是顶部工具条，也可以是侧边栏工具条。</p>
<p><a href="http://cssrainbow.cn/tutorials/xhtml-css/882.html"><br />
<img src="http://cssrainbow.cn//dw-content/demo15/ScrollSidebar.png" width="552" height="401" alt="ScrollSidebar"/></a></p>
<p>&nbsp;</p>
<p><a class="button" href="http://cssrainbow.cn/tutorials/xhtml-css/882.html">View Demo </a></p>
<p>&nbsp;</p>
<p><img src="http://cssrainbow.cn/dw-content/demo15/fixedFadeOutMenu.png" width="550" height="160" alt="fixedFadeOutMenu" /></p>
<p>&nbsp;</p>
<p><a class="button" href="http://tympanus.net/Tutorials/FixedFadeOutMenu/">View Demo</a></p>
<p>&#8212; The End &#8212;</p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2010/06/30 -- <a href="http://cssrainbow.cn/tutorials/jquery/1347.html" title="盒子（Box）中间的链接（Links）,基于MooTools or jQuery ">盒子（Box）中间的链接（Links）,基于MooTools or jQuery </a> (5)</li><li>2010/06/24 -- <a href="http://cssrainbow.cn/tutorials/jquery/1331.html" title="打破重复的动画效果,基于CSS,MooTools or jQuery">打破重复的动画效果,基于CSS,MooTools or jQuery</a> (3)</li><li>2010/03/05 -- <a href="http://cssrainbow.cn/code-snippets/mootools-code-snippets/1141.html" title="删除残破的图片（Broken Images），基于 MooTools or jQuery">删除残破的图片（Broken Images），基于 MooTools or jQuery</a> (0)</li><li>2010/02/05 -- <a href="http://cssrainbow.cn/tutorials/mootools/1104.html" title="网页换肤,基于MooTools or jQuery">网页换肤,基于MooTools or jQuery</a> (1)</li><li>2010/02/03 -- <a href="http://cssrainbow.cn/tutorials/jquery/1078.html" title="Google-Style之元素褪色（Element Fading），基于MooTools or jQuery">Google-Style之元素褪色（Element Fading），基于MooTools or jQuery</a> (0)</li><li>2009/12/02 -- <a href="http://cssrainbow.cn/tutorials/jquery/835.html" title="在网站外部链接旁边添加Favorite Icons的方法，基于MooTools or jQuery">在网站外部链接旁边添加Favorite Icons的方法，基于MooTools or jQuery</a> (0)</li><li>2009/11/10 -- <a href="http://cssrainbow.cn/tutorials/jquery/819.html" title="学会做一个简易的 Tooltip 效果，基于 MooTools or jQuery">学会做一个简易的 Tooltip 效果，基于 MooTools or jQuery</a> (3)</li><li>2009/11/04 -- <a href="http://cssrainbow.cn/tutorials/jquery/805.html" title="创建一个‘精灵’导航菜单，基于 CSS Sprites And MooTools or jQuery ">创建一个‘精灵’导航菜单，基于 CSS Sprites And MooTools or jQuery </a> (0)</li><li>2009/09/24 -- <a href="http://cssrainbow.cn/tutorials/jquery/740.html" title="让链接(link)充满活力,基于MooTools &#038; jQuery实现的">让链接(link)充满活力,基于MooTools &#038; jQuery实现的</a> (0)</li><li>2010/01/14 -- <a href="http://cssrainbow.cn/tutorials/xhtml-css/882.html" title="实现侧边栏滚动的多种技术，基于CSS,jQuery or MooTools ">实现侧边栏滚动的多种技术，基于CSS,jQuery or MooTools </a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/jquery/1225.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>理解JavaScript中的事件处理</title>
		<link>http://cssrainbow.cn/tutorials/javascript/1027.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/1027.html#comments</comments>
		<pubDate>Tue, 26 Jan 2010 05:23:41 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript Events]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=1027</guid>
		<description><![CDATA[事件是JavaScript和DOM之间进行交互的桥梁，当某个事件发生时，通过它的处理函数执行相应的JavaScript代码.通过本文我们可以理解JavaScript中的事件处理模式，涉及到的知识点，包括事件流机制、事件处理模型（事件捕捉和事件冒泡）、事件处理函数（事件句柄）、事件接听器和如何停止事件冒泡和阻止事件的默认行为等。]]></description>
			<content:encoded><![CDATA[<h2>什么是事件？</h2>
<p>事件(Event)是JavaScript应用跳动的心脏  ，也是把所有东西粘在一起的胶水。当我们与浏览器中 Web 页面进行某些类型的交互时，事件就发生了。事件可能是用户在某些内容上的点击、鼠标经过某个特定元素或按下键盘上的某些按键。事件还可能是 Web 浏览器中发生的事情，比如说某个 Web 页面加载完成，或者是用户滚动窗口或改变窗口大小。  </p>
<p>通过使用 JavaScript ，你可以监听特定事件的发生，并规定让某些事件发生以对这些事件做出响应。 </p>
<h2>今天的事件</h2>
<p>
在漫长的演变史，我们已经告别了内嵌式的事件处理方式（直接将事件处理器放在 HTML 元素之内来使用）。今天的事件，它已是DOM的重要组成部分，遗憾的是，	IE继续保留它最早在IE4.0中实现的事件模型，以后的IE版本中也没有做太大的改变，这也就是说IE还是使用的是一种专有的事件模型（冒泡型）,而其它的主流浏览器直到DOM  级别 3 规定定案后，才陆陆续续支持DOM标准的事件处理模型 — 捕获型与冒泡型。</p>
<p><span id="more-1027"></span></p>
<p><strong>历史原因是：</strong>W3C 规范 在DOM 级别 1中并没有定义任何的事件，直到发布于 2000 年 11 月 的DOM 级别 2 才定义了一小部分子集，DOM 级别 2中已经提供了提供了一种更详细的更细致的方式以控制 Web 页面中的事件，最后，完整的事件是在2004年 DOM 级别 3的规定中才最终定案。因为IE4是1995推出的并已实现了自己的事件模型（冒泡型），当时根本就没有DOM标准，不过在以后的DOM标准规范过程中已经把IE的事件模型吸收到了其中。</p>
<p>目前除IE浏览器外，其它主流的Firefox, Opera,<br />
Safari都支持标准的DOM事件处理模型。IE仍然使用自己专有的事件模型，即冒泡型，它事件模型的一部份被DOM标准采用，这点对于开发者来说也是有好处的，只有使用<br />
DOM标准，IE都共有的事件处理方式才能有效的跨浏览器。</p>
<h2>DOM事件流</h2>
<p>DOM(文档对象模型)结构是一个树型结构，当一个HTML元素产生一个事件时，该事件会在元素结点与根节点之间按特定的顺序传播，路径所经过的节点都会收到该事件，这个传播过程可称为DOM事件流。</p>
<p>事件顺序有两种类型：<strong>事件捕捉</strong>和<strong>事件冒泡</strong>。</p>
<h3><strong> 冒泡型事件(Event Bubbling)</strong></h3>
<p>
这是IE浏览器对事件模型的实现，也是最容易理解的，至少笔者觉得比较符合实际的。冒泡，顾名思义，事件像个水中的气泡一样一直往上冒，直到顶端。从<br />
DOM树型结构上理解，就是事件由叶子节点沿祖先结点一直向上传递直到根节点；从浏览器界面视图HTML元素排列层次上理解就是事件由具有从属关系的最确定的目标元素一直传递到最不确定的目标元素.冒泡技术.冒泡型事件的基本思想,事件按照从特定的事件目标开始到最不确定的事件目标.</p>
<h3><strong> 捕获型事件(Event Capturing)</strong></h3>
<p>  Netscape 的实现，它与冒泡型刚好相反，由DOM树最顶层元素一直到最精确的元素，这个事件模型对于开发者来说（至少是我..）有点费解，因为直观上的理解应该如同冒泡型，事件传递应该由最确定的元素，即事件产生元素开始。</p>
<p>&nbsp;</p>
<p><img src="http://cssrainbow.cn/Project/JavaScript/demo6/event-buddle.jpg"  alt="event-buddle"/>
</p>
<h3>DOM标准的事件模型</h3>
<p>我们已经对上面两个不同的事件模型进行了解释和对比。DOM标准同时支持两种事件模型，即<strong>捕获型事件</strong>与<strong>冒泡型事件</strong>，但是，捕获型事件先发生。两种事件流都会触发DOM中的所有对象，从document对象开始，也在document对象结束（大部分兼容标准的浏览器会继续将事件是捕捉/冒泡延续到window对象）。</p>
<p><img src="http://cssrainbow.cn/Project/JavaScript/demo6/domevent.jpg"  alt="domevent"/></p>
<p>如图：首先是捕获式传递事件，接着是冒泡式传递，所以，如果一个处理函数既注册了捕获型事件的监听，又注册冒泡型事件监听，那么在DOM事件模型中它就会被调用两次。</p>
<p class="node">DOM标准的事件模型最独特的性质是,文本节点也会触发事件(在IE不会)。</p>
<p class="node"><img src="http://cssrainbow.cn/Project/JavaScript/demo6/capturing-bubbling.png" width="394" height="357"  alt="capturing-bubbling"/></p>
<h3 class="node">事件传送</h3>
<p>为了更好的说明DOM标准中的事件流原理，我们把它放在“事件传送”小结里来更具体的解释。</p>
<p>显然，如果为一个超链接添加了click事件监听器，那么当该链接被点击时该事件监听器就会被执行。但如果把该事件监听器指派给了包含该链接的p元素或者位于DOM树顶端的document节点，那么点击该链接也同样会触发该事件监听器。这是因为事件不仅仅对触发的目标元素产生影响，它们还会对沿着DOM结构的所有元素产生影响。这就是大家所熟悉的<strong>事件转送</strong>。</p>
<p>W3C事件模型中明确地指出了事件转送的原理。事件传送可以分为3个阶段。</p>
<p><img src="http://cssrainbow.cn/Project/JavaScript/demo6/Standard-event-propagation.png" width="721" height="616" alt="Standard-event-propagation" /></p>
<p>如图：标准的事件转送模式</p>
<p>（1）.在事件捕捉（Capturing）阶段，事件将沿着DOM树向下转送，目标节点的每一个祖先节点，直至目标节点。例如，若用户单击了一个超链接，则该单击事件将从document节点转送到html元素，body元素以及包含该链接的p元素。</p>
<p>在此过程中，浏览器都会检测针对该事件的捕捉事件监听器，并且运行这件事件监听器。</p>
<p> (2). 在目标（target）阶段，浏览器在查找到已经指定给目标事件的事件监听器之后，就会运行 该事件监听器。目标节点就是触发事件的DOM节点。例如，如果用户单击一个超链接，那么该链接就是目标节点（此时的目标节点实际上是超链接内的文本节点）。</p>
<p>（3）.在冒泡（Bubbling）阶段，事件将沿着DOM树向上转送，再次逐个访问目标元素的祖先节点到document节点。该过程中的每一步。浏览器都将检测那些不是捕捉事件监听器的事件监听器，并执行它们。</p>
<blockquote>
<p><strong>并非所有的事件都会经过冒泡阶段的</strong></p>
<p>所有的事件都要经过捕捉阶段和目标阶段，但是有些事件会跳过冒泡阶段。例如，让元素获得输入焦点的focus事件以及失去输入焦点的blur事件就都不会冒泡。</p>
</p>
</blockquote>
<h2>事件句柄和事件接听器</h2>
<h3>事件句柄</h3>
<p>事件句柄（又称事件处理函数，DOM称之为事件监听函数），用于响应某个事件而调用的函数称为事件处理函数<br />
。每一个事件均对应一个事件句柄，在程序执行时，将相应的函数或语句指定给事件句柄，则在该事件发生时，浏览器便执行指定的函数或语句，从而实现网页内容与用户操作的交互。当浏览器检测到某事件发生时，便查找该事件对应的事件句柄有没有被赋值，如果有，则执行该事件句柄。 </p>
<p>我们认为响应点击事件的函数是onclick事件处理函数。以前，事件处理函数有两种分配方式：在<strong>JavaScript中</strong>或者在<strong>HTML中</strong>。</p>
<p>如果在JavaScript 中分配事件处理函数， 则需要首先获得要处理的对象的一引用，然后将函数赋值给对应的事件处理函数属性，请看一个简单的例子：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> link<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mylink&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
link.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was clicked !&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>从我们看到的例子中，我们发现使用事件句柄很容易，<br />
  不过事件处理函数名称必须是小写的，还有就是只有在<br />
  元素载入完成之后才能将事件句柄赋给元素，不然会有异常。 </p>
<p>关于文档载入技术，请看<a href="http://cssrainbow.cn/index.php/tutorials/javascript/526.html" rel="bookmark" title="Permanent Link to 关于window.onload加载的多种解决方案">《window.onload加载的多种解决方案》 </a>的文章。</p>
<p>如果在HTML中分配事件句柄的话，则直接通过HTML属性来设置事件处理函数就行了，并在其中包含合适的脚本作为特性值就可以了，例如：</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><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> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;JavaScript code here&quot;</span>&gt;</span>......<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>这种JavaScript 代码和通过HTML的style属性直接将CSS属性赋给元素类似。这样会代码看起来一团糟，也违背了将实现动态行为的代码与显示文档静态内容的代码相分离的原则。从1998年开始，这种写法就过时了。</p>
<h3>这种传统的事件绑定技术，优缺点是显然的：</h3>
<ul>
<li>简单方便，在HTML中直接书写处理函数的代码块，在JS中给元素对应事件属性赋值即可。</li>
<li>IE与DOM标准都支持的一种方法，它在IE与DOM标准中都是在事件冒泡过程中被调用的。</li>
<li>可以在处理函数块内直接用this引用注册事件的元素，this引用的是当前元素。</li>
<li>要给元素注册多个监听器，就不能用这方法了。</li>
</ul>
<h3>事件接听器</h3>
<p>除了前面已经介绍的简单事件句柄之外，现在大多数浏览器都内置了一些更高级的事件处理方式，即，事件监听器，这种处理方式就不受一个元素只能绑定一个事件句柄的限制。</p>
<p>我们已经知道了事件句柄与事件监听器的最大不同之处是使用事件句柄时一次只能插接一个事件句柄，但对于事件监听器，一次可以插接多个。</p>
<h4>IE下的事件监听器：</h4>
<p>IE提供的却是一种自有的，完全不同的甚至存在BUG的事件监听器，因此如果要让脚本在本浏览器中正常运行的话，就必须使用IE所支持的事件监听器。另外，Safari 浏览器中的事件监听器有时也存在一点不同。</p>
<p>在IE中，每个元素和window对象都有两个方法：<span class="">attachEvent</span>方法和detachEvent方法。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">element.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onevent&quot;</span><span style="color: #339933;">,</span>eventListener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>此方法的意思是在IE中要想给一个元素的事件附加事件处理函数，必须调用<span class="">attachEvent</span>方法才能创建一个事件监听器。attachEvent方法允许外界注册该元素多个事件监听器。</p>
<p>attachEvent接受两个参数。第一个参数是事件类型名，第二个参数eventListener是回调处理函数。这里得说明一下，有个经常会出错的地方，IE下<br />
利用attachEvent注册的处理函数调用时this指向不再是先前注册事件的元素，这时的this为window对象。还有一点是此方法的事件类型名称必须加上一个&#8221;on&#8221;的前缀（如onclick）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">element.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onevent&quot;</span><span style="color: #339933;">,</span>eventListener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>要想移除先前元素注册的事件监听器,可以使用detachEvent方法进行删除，参数相同。</p>
<h4>DOM标准下的事件监听器：</h4>
<p>在支持W3C标准事件监听器的浏览器中，对每个支持事件的对象都可以使用addEventListener方法。该方法既支持注册冒泡型事件处理，又支持捕获型事件处理。所以与IE浏览器中注册元素事件监听器方式有所不同的。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//标准语法 </span>
element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'event'</span><span style="color: #339933;">,</span> eventListener<span style="color: #339933;">,</span> useCapture<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//默认</span>
element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'event'</span><span style="color: #339933;">,</span> eventListener<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></pre></td></tr></table></div>

<p>addEventListener方法接受三个参数。第一个参数是事件类型名，值得注意的是，这里事件类型名称与IE的不同，事件类型名是没’on’开头的;第二个参数eventListener是回调处理函数（即监听器函数）;第三个参数注明该处理回调函数是在事件传递过程中的捕获阶段被调用还是冒泡阶段被调用 ，通常此参数通常会设置为false（为false时是冒泡）,那么，如果将其值设置为true，那就创建一个捕捉事件监听器。</p>
<p>移除已注册的事件监听器调用element的removeEventListener方法即可，参数相同.。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//标准语法 </span>
element.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'event'</span><span style="color: #339933;">,</span> eventListener<span style="color: #339933;">,</span> useCapture<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//默认</span>
element.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'event'</span><span style="color: #339933;">,</span> eventListener<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></pre></td></tr></table></div>

<blockquote>
<p>  通过addEventListener方法添加的事件处理函数，必须使用removeEventListener方法才能删除，而且要求参数与添加事件处理函数时addEventListener方法的参数完全一致（包括useCapture参数），否则将不能成功删除事件处理函数。
  </p>
</blockquote>
<h3>跨浏览器的注册与移除元素事件监听器方案</h3>
<p>我们现在已经知道，对于支持addEventListener方法的浏览器，只要需要事件监听器脚本就都需要调用addEventListener方法；而对于不支持该方法的IE浏览器，使用事件监听器时则需要调用<span class="">attachEvent</span>方法。要确保浏览器使用正确的方法其实并不困难，只需要通过一个if-else语句来检测当前浏览器中是否存在addEventListener方法或<span class="">attachEvent</span>方法即可。</p>
<p>这样的方式就可以实现一个跨浏览器的注册与移除元素事件监听器方案:</p>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> EventUtil <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #006600; font-style: italic;">//注册</span>
     addHandler<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> type<span style="color: #339933;">,</span> handler<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>element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
                 element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> handler<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> 
          <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>element.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
                 element.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #339933;">,</span> handler<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> 
                 element<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> handler<span style="color: #339933;">;</span> 
          <span style="color: #009900;">&#125;</span> 
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
     <span style="color: #006600; font-style: italic;">//移除注册</span>
      removeHandler<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> type<span style="color: #339933;">,</span> handler<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>element.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
                 element.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> handler<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> 
          <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>element.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
                 element.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #339933;">,</span> handler<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> 
                 element<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span> 
          <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span> 
&nbsp;
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>事件对象引用</h2>
<p>为了更好的处理事件，你可以根据所发生的事件的特定属性来采取不同的操作。</p>
<p>如事件模型一样，IE 和其他浏览器处理方法不同：IE 使用一个叫做 event 的全局事件对象来处理对象（它可以在全局变量window.event中找到），而其它所有浏览器采用的 W3C 推荐的方式，则使用独立的包含事件对象的参数传递。 </p>
<p> 跨浏览器实现这样的功能时，最常见的问题就是获取事件本身的引用及获取该事件的目标元素的引用。</p>
<p>下面这段代码就为你解决了这个问题： </p>

<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="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> EventUtil <span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>
    getEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>       
        <span style="color: #000066; font-weight: bold;">return</span> event <span style="color: #339933;">?</span> event <span style="color: #339933;">:</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>        
    getTarget<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>        
       <span style="color: #000066; font-weight: bold;">return</span> event.<span style="color: #660066;">target</span> <span style="color: #339933;">||</span> event.<span style="color: #660066;">srcElement</span><span style="color: #339933;">;</span>              
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>停止事件冒泡和阻止事件的默认行为</h2>
<p>“停止事件冒泡“和”阻止浏览器的默认行为“，这两个概念非常重要，它们对复杂的应用程序处理非常有用。</p>
<h3>1.停止事件冒泡</h3>
<p>停止事件冒泡是指，停止冒泡型事件的进一步传递（取消事件传递,不只是停止IE和DOM标准共有的冒泡型事件，我们还可以停止支持DOM标准浏览器的捕捉型事件，用topPropagation()方法）。例如上图中的冒泡型事件传递中，在body处理停止事件传递后，位于上层的document的事件监听器就不再收到通知，不再被处理。</p>
<h3>2.阻止事件的默认行为</h3>
<p>停止事件的默认行为是指，通常浏览器在事件传递并处理完后会执行与该事件关联的默认动作（如果存在这样的动作）。例如，如果表单中input type 属性是 “submit”，点击后在事件传播完浏览器就自动提交表单。又例如，input 元素的 keydown 事件发生并处理后，浏览器默认会将用户键入的字符自动追加到 input 元素的值中。 </p>
<p><strong>停止事件冒泡的处理方法</strong>：</p>
<p>在IE下,通过设置event对象的cancelBubble为true即可。</p>

<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;"><span style="color: #003366; font-weight: bold;">function</span> someHandle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     window.<span style="color: #660066;">event</span>.<span style="color: #660066;">cancelBubble</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>DOM标准通过调用event对象的stopPropagation()方法即可。</p>

<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;"><span style="color: #003366; font-weight: bold;">function</span> someHandle<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    event.<span style="color: #660066;">stopPropagation</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>

<p>因些，跨浏览器的停止事件传递的方法是:</p>

<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="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> someHandle<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    event <span style="color: #339933;">=</span> event <span style="color: #339933;">||</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       event.<span style="color: #660066;">stopPropagation</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
       event.<span style="color: #660066;">cancelBubble</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>阻止事件的默认行为的处理方法</strong>：</p>
<p>就像事件模型和事件对象差异一样，在IE和其它所有浏览器中阻止事件的默认行为的方法也不同。</p>
<p>在IE下,通过设置event对象的returnValue为false即可。</p>

<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;"><span style="color: #003366; font-weight: bold;">function</span> someHandle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   window.<span style="color: #660066;">event</span>.<span style="color: #660066;">returnValue</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>DOM标准通过调用event对象的preventDefault()方法即可。</p>

<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;"><span style="color: #003366; font-weight: bold;">function</span> someHandle<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   event.<span style="color: #660066;">preventDefault</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>

<p>因些，跨浏览器的取消事件传递后的默认处理方法是：</p>

<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="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> someHandle<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   event <span style="color: #339933;">=</span> event <span style="color: #339933;">||</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      event.<span style="color: #660066;">preventDefault</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: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
      event.<span style="color: #660066;">returnValue</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>完整的事件处理兼容性函数</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
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> EventUtil <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>             
    addHandler<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> type<span style="color: #339933;">,</span> handler<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>element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
          element.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> handler<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>      
       <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>element.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>        
           element.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #339933;">,</span> handler<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>        
           element<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> handler<span style="color: #339933;">;</span>       
       <span style="color: #009900;">&#125;</span>   
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>           
    removeHandler<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> type<span style="color: #339933;">,</span> handler<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>element.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>         
           element.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> handler<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>      
       <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>element.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>         
           element.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #339933;">,</span> handler<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>       
           element<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;on&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>    
       <span style="color: #009900;">&#125;</span>    
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>  
    getEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>     
        <span style="color: #000066; font-weight: bold;">return</span> event <span style="color: #339933;">?</span> event <span style="color: #339933;">:</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>        
    getTarget<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>       
        <span style="color: #000066; font-weight: bold;">return</span> event.<span style="color: #660066;">target</span> <span style="color: #339933;">||</span> event.<span style="color: #660066;">srcElement</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>        
    preventDefault<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
           event.<span style="color: #660066;">preventDefault</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>  
           event.<span style="color: #660066;">returnValue</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>      
       <span style="color: #009900;">&#125;</span>   
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>                    
    stopPropagation<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>      
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>        
         event.<span style="color: #660066;">stopPropagation</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>         
         event.<span style="color: #660066;">cancelBubble</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>     
      <span style="color: #009900;">&#125;</span>    
    <span style="color: #009900;">&#125;</span>         
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>捕获型事件模型与冒泡型事件模型的应用场合</h2>
<p>标准事件模型为我们提供了两种方案，可能很多朋友分不清这两种不同模型有啥好处，为什么不只采取一种模型。<br />
这里抛开IE浏览器讨论（IE只有一种，没法选择）什么情况下适合哪种事件模型。</p>
<h3>1. 捕获型应用场合</h3>
<p>
捕获型事件传递由最不精确的祖先元素一直到最精确的事件源元素，传递方式与操作系统中的全局快捷键与应用程序快捷键相似。当一个系统组合键发生时，如果注<br />
册了系统全局快捷键监听器，该事件就先被操作系统层捕获，全局监听器就先于应用程序快捷键监听器得到通知，也就是全局的先获得控制权，它有权阻止事件的进<br />
一步传递。所以捕获型事件模型适用于作全局范围内的监听，这里的全局是相对的全局，相对于某个顶层结点与该结点所有子孙结点形成的集合范围。</p>
<p> 例如你想作全局的点击事件监听，相对于document结点与document下所有的子结点，在某个条件下要求所有的子结点点击无效，这种情况下冒泡模型就解决不了了，而捕获型却非常适合，可以在最顶层结点添加捕获型事件监听器，伪码如下:</p>

<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="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> globalClickListener<span style="color: #009900;">&#40;</span>event<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>canEventPass <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #006600; font-style: italic;">//取消事件进一步向子结点传递和冒泡传递</span>
         event.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #006600; font-style: italic;">//取消浏览器事件后的默认执行</span>
         event.<span style="color: #660066;">preventDefault</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></pre></td></tr></table></div>

<p>这样一来，当canEventPass条件为假时，document下所有的子结点click注册事件都不会被浏览器处理。</p>
<h3>2. 冒泡型的应用场合</h3>
<p>
可以说我们平时用的都是冒泡事件模型，因为IE只支持这模型。这里还是说说，在恰当利用该模型可以提高脚本性能。在元素一些频繁触发的事件中，如<br />
onmousemove,<br />
onmouseover,onmouseout,如果明确事件处理后没必要进一步传递，那么就可以大胆的取消它。此外，对于子结点事件监听器的处理会对父<br />
层监听器处理造成负面影响的，也应该在子结点监听器中禁止事件进一步向上传递以消除影响。</p>
<h2>综合案例分析</h2>
<p>最后结合下面HTML代码作分析:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;alert('current is body');&quot;</span>&gt;</span>
 <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;div0&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;alert('current is '+this.id)&quot;</span>&gt;</span>
      <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;div1&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;alert('current is '+this.id)&quot;</span>&gt;</span>
         <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;div2&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;alert('current is '+this.id)&quot;</span>&gt;</span>
            <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;event_source&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;alert('current is '+this.id)&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;height:200px;width:200px;background-color:red;&quot;</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
         <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
   <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
 <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></pre></td></tr></table></div>

<p>
HTML运行后点击红色区域,这是最里层的DIV,根据上面说明,无论是DOM标准还是IE,直接写在html里的监听处理函数是事件冒泡传递时调用的,由最里层一直往上传递,所以会先后出现<br />
  current is event_source<br />
  current is div2<br />
  current is div1<br />
  current is div0<br />
  current is body
</p>
<p>添加以下片段:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> div2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
EventUtil.<span style="color: #660066;">addHandler</span><span style="color: #009900;">&#40;</span>div2<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     event <span style="color: #339933;">=</span> EventUtil.<span style="color: #660066;">getEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 EventUtil.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a  href="http://cssrainbow.cn/Project/JavaScript/demo6/JavaScript-Event-Demo.htm" class="button">Demo 演示</a></p>
<p>当点击红色区域后,根据上面说明,在泡冒泡处理期间,事件传递到div2后被停止传递了,所以div2上层的元素收不到通知,所以会先后出现:<br />
  current is event_source<br />
  current is div2</p>
<p>在支持DOM标准的浏览器中,添加以下代码:</p>

<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;">body</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    event.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>以上代码中的监听函数由于是捕获型传递时被调用的,所以点击红色区域后,虽然事件源是ID为event_source的元素,但捕获型选传递,从最顶层开始,body结点监听函数先被调用,并且取消了事件进一步向下传递,所以只会出现 current is body .</p>
<p>&nbsp;</p>
<p>
参考文章来：<a href="http://www.9demo.com/2009/07/30/dom%E6%A0%87%E5%87%86%E4%B8%8Eie%E7%9A%84html%E5%85%83%E7%B4%A0%E4%BA%8B%E4%BB%B6%E6%A8%A1%E5%9E%8B%E5%8C%BA%E5%88%AB/">DOM标准与IE的html元素事件模型区别</a>
</p>
<p>&#8212;THE END&#8212;</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/31 -- <a href="http://cssrainbow.cn/code-snippets/javascript-code-snippets/1058.html" title="如何在事件代理中正确使用 focus 和 blur 事件">如何在事件代理中正确使用 focus 和 blur 事件</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/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/1027.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合</title>
		<link>http://cssrainbow.cn/tutorials/jquery/790.html</link>
		<comments>http://cssrainbow.cn/tutorials/jquery/790.html#comments</comments>
		<pubDate>Thu, 29 Oct 2009 02:17:28 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=790</guid>
		<description><![CDATA[你还记得你第一次看到lighbox特效吗？我敢打赌，你很惊讶。哇，太好了，我必须在我的网站上应用。 自从那时以来，许多人使用它，添加一些功能。有一些人重写代码，以使它达到完美效果。 

知识扩展：lightbox 百度知道

现在，以Lightbox为原型的克隆版的Lightbox有很多了，你在《The Lightbox Clones Matrix》日志里，可以尽情的筛选你想要的 Lightbox libraries，并且想要啥样的功能，任你筛选和过滤。 ]]></description>
			<content:encoded><![CDATA[<h1 align="center">Lightbox Clones &#8211; 基于主流JavaScript库的Lightbox项目大集合</h1>
<p>你还记得你第一次看到lighbox特效吗？我敢打赌，你很惊讶。哇，太好了，我必须在我的网站上应用。<br />
  自从那时以来，许多人使用它，添加一些功能。有一些人重写代码，以使它达到完美效果。
</p>
<p><strong>知识扩展</strong>：<a href="http://baike.baidu.com/view/1247096.htm?fr=ala0">lightbox 百度知道</a></p>
<p>现在，以Lightbox为原型的克隆版的Lightbox有很多了，你在《<a href="http://planetozh.com/projects/lightbox-clones/">The Lightbox Clones Matrix</a>》日志里，可以尽情的筛选你想要的 Lightbox libraries，并且想要啥样的功能，任你筛选和过滤。
</p>
<p><img src="http://cssrainbow.cn/Project/The-Lightbox-Clones-Matrix.jpg" width="544" height="423"  alt="The-Lightbox-Clones-Matrix"/></p>
<p><span id="more-790"></span></p>
<p>&nbsp;</p>
<p>还等什么！赶快去看一下吧！也许能找到你想要的Lightbox。 </p>
<p>资源网址：<a href="http://planetozh.com/projects/lightbox-clones/">The Lightbox Clones Matrix</a></p>
<p>&nbsp;</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>2010/01/19 -- <a href="http://cssrainbow.cn/articles/web-roundups/905.html" title="Google 排名中的 10 个最著名的 JavaScript 库">Google 排名中的 10 个最著名的 JavaScript 库</a> (0)</li><li>2009/10/18 -- <a href="http://cssrainbow.cn/tutorials/jquery/770.html" title="3个流行的JavaScript库一起学">3个流行的JavaScript库一起学</a> (0)</li><li>2010/06/30 -- <a href="http://cssrainbow.cn/tutorials/jquery/1347.html" title="盒子（Box）中间的链接（Links）,基于MooTools or jQuery ">盒子（Box）中间的链接（Links）,基于MooTools or jQuery </a> (5)</li><li>2010/06/29 -- <a href="http://cssrainbow.cn/code-snippets/mootools-code-snippets/1342.html" title="使用MooTools, jQuery, and Dojo 来完成常见任务的语法归类">使用MooTools, jQuery, and Dojo 来完成常见任务的语法归类</a> (0)</li><li>2010/06/27 -- <a href="http://cssrainbow.cn/code-snippets/mootools-code-snippets/1333.html" title="在我喜欢的JavaScript Frameworks中使用匿名方法">在我喜欢的JavaScript Frameworks中使用匿名方法</a> (4)</li><li>2010/06/24 -- <a href="http://cssrainbow.cn/tutorials/jquery/1331.html" title="打破重复的动画效果,基于CSS,MooTools or jQuery">打破重复的动画效果,基于CSS,MooTools or jQuery</a> (3)</li><li>2010/03/26 -- <a href="http://cssrainbow.cn/articles/web-roundups/1218.html" title="jQuery vs MooTools 专题系列">jQuery vs MooTools 专题系列</a> (5)</li><li>2010/03/22 -- <a href="http://cssrainbow.cn/code-snippets/javascript-code-snippets/1187.html" title="简单的斑马纹表格">简单的斑马纹表格</a> (2)</li><li>2010/03/18 -- <a href="http://cssrainbow.cn/tutorials/mootools/1183.html" title="jQuery 1.4, MooTools 1.2 比较">jQuery 1.4, MooTools 1.2 比较</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/jquery/790.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>窥探jQuery — 面向JavaScript程序员(收藏)</title>
		<link>http://cssrainbow.cn/tutorials/jquery/706.html</link>
		<comments>http://cssrainbow.cn/tutorials/jquery/706.html#comments</comments>
		<pubDate>Thu, 10 Sep 2009 01:45:40 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=706</guid>
		<description><![CDATA[当 jQuery 在2006年1月现身时，给我的第一印象，是这玩意儿构造得很精明。基于CSS选择器（CSS selectors）来打点一切，其思路相当灵巧（参考getElementsBySelector）。但链盒工事（chaining stuff）看起来更像个噱头，并且整体看来，jQuery库提供的功能并不能覆盖所有基础性的东西。因此我断定，jQuery只会昙花一现。

几个月以来，我逐渐明白自己想错了。从技术工艺上考量，jQuery十分凌厉。它用简洁的方法，把大量常用功能封装起来，并提供精巧的插入式API，来满足标准库之外的功能模块的实现。jQuery秉持的核心，乃DOM元素的集合（译注：通常是某些子集合）——它把元素集合作为一个根本，给高度抽象出来了。最重要的，是这种遵循最佳实践的抽象，能让jQuery与其他JavaScript代码相处融洽。

很多对jQuery的介绍，都是针对设计师和初级开发人员。接下来我想说明，为什么jQuery也会吸引那些富有经验的开发人员。]]></description>
			<content:encoded><![CDATA[<h1 align="center">窥探jQuery &#8212; 面向JavaScript程序员</h1>
<blockquote>
<p><strong>简介</strong></p>
<p>Simon  Willison来自英国，是一名经验丰富的开发人员。曾工作于Yahoo，是Web开发框架Django的创始人之一，也是OpenID的推动者。这是  一篇针对jQuery的描述性文章，由于作者出色的分析，该文被引入jquery.com的教程库，对jQuery的推动有一定作用。 <br />
      英文版权归Simon Willison所有，中译文作者yangyang（aka davidkoree）。双语版可用于非商业传播，但须注明英文版作者、版权信息，以及中译文作者。翻译水平有限，请广大读者指正。
    </p>
</blockquote>
<p> 当 <a href="http://jquery.com/">jQuery</a> 在2006年1月现身时，给我的第一印象，是这玩意儿构造得很精明。基于CSS选择器（CSS selectors）来打点一切，其思路相当灵巧（参考<a href="http://simonwillison.net/2003/Mar/25/getElementsBySelector/">getElementsBySelector</a>）。但链盒工事（chaining stuff）看起来更像个噱头，并且整体看来，jQuery库提供的功能并不能覆盖所有基础性的东西。因此我断定，jQuery只会昙花一现。    </p>
<p><span id="more-706"></span></p>
<p>    几  个月以来，我逐渐明白自己想错了。从技术工艺上考量，jQuery十分凌厉。它用简洁的方法，把大量常用功能封装起来，并提供精巧的插入式API，来满足  标准库之外的功能模块的实现。jQuery秉持的核心，乃DOM元素的集合（译注：通常是某些子集合）——它把元素集合作为一个根本，给高度抽象出来了。  最重要的，是这种遵循最佳实践的抽象，能让jQuery与其他JavaScript代码相处融洽。        </p>
<p>      很多对jQuery的介绍，都是针对设计师和初级开发人员。接下来我想说明，为什么jQuery也会吸引那些富有经验的开发人员。    </p>
<h2>名称空间（Namespacing）</h2>
<p>
      编写可重用的、优秀的JavaScript代码，其关键在于对名称空间的积极把控。JavaScript只拥有单一的、全局的名称空间（即window对象），而很多程序员（以及一些库）恣意地为之添加各种东西。要知道<a title="全局变量是魔鬼" href="http://yuiblog.com/blog/2006/06/01/global-domination/">全局变量是魔鬼</a>！聪明的开发人员，会使用类似<a title="组件模式" href="http://yuiblog.com/blog/2007/06/12/module-pattern/">组件模式</a>的技术，来尽力减少全局对象的数量。</p>
<p>      jQuery仅向全局名称空间引入一个标记：<strong>jQuery</strong>函数/对象。其余的要么是<strong>jQuery</strong>的直接属性（译注：原文‘directy property’系笔误，应是‘direct property’），要么就是调用jQuery函数所返回的对象的方法。</p>
<p>      那  “语言升级”（language  enhancements）又是什么呢？大多数库会提供映射，过滤，剥离，往往是浏览器的JavaScript引擎所缺少的那些功能。还有一些库，直接扩  展了JavaScript内置的String和Array类，但这是冒险的做法。String.prototype和Array.prototype也有  各自的名称空间，在其内添加的属性一旦发生冲突，所带来的风险，不亚于在全局环境下的草率大意。</p>
<p>      在语言升级方面，jQuery提供了很多函数（功能），但每个函数都被赋给<strong>jQuery</strong>对象的属性：jQuery.each，jQuery.extend，jQuery.grep，jQuery.map，jQuery.merge以及jQuery.trim。如此一来，它们就不会跟其他代码产生冲突。</p>
<h2> 声名狼藉的$函数（The infamous $ function）</h2>
<p>
      刚才我说到，<strong>jQuery</strong>是唯一被引入的全局标记，其实并不尽然：<strong>$</strong>标记作为<strong>jQuery</strong>的快捷方式，也被引入进来。庆幸的是，<strong>$</strong>的存在不会带来负面影响：如果你需要让原始的<strong>$</strong>起死回生（比如，这之前你的代码使用了Prototype），你可以调用jQuery.noConflict()来恢复它。</p>
<p>      如果你既想拥有<strong>$</strong>的便利，又不希望jQuery跟其他同样使用了全局$函数的代码发生冲突，可遵循jQuery文档所建议的惯用方式：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 在这个函数体里，$可作为jQuery的引用</span>
    <span style="color: #006600; font-style: italic;">// 很方便，对吧？</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>把一切都附加到<strong>$</strong>标记的做法，曾让我认为jQuery华而不实。不过，从体系的角度来审视这种设计，一切又是非常明了的——尽管我常喜欢在代码中定义自己的$快捷方式。</p>
<h2>选取元素（Selecting some elements）</h2>
<p>
      jQuery的每个操作，都以选取DOM中一个或更多的节点（nodes）作为开始。jQuery（拥有一种真正的面向特定领域）的<a title="选取语法" href="http://docs.jquery.com/DOM/Traversing/Selectors">选取语法</a>，是十分有趣的，它结合了CSS 1，CSS 2，部分CSS 3语法，一些XPath语法，以及一些特定的扩展。在这里我不会做详细介绍，我只列出几个有用的例子：</p>
<dl>
<dt>jQuery(&#8216;div.panel&#8217;)</dt>
<dd>选取了所有class=&quot;panel&quot;的div</dd>
<dt>jQuery(&#8216;p#intro&#8217;)</dt>
<dd>选取了所有id=&quot;intro&quot;的段落</dd>
<dt>jQuery(&#8216;div#content a:visible&#8217;)</dt>
<dd>选取了id=&quot;content&quot;的div中所有可见的链接</dd>
<dt>jQuery(&#8216;input[@name=email]&#8216;)</dt>
<dd>选取了所有name=&quot;email&quot;的输入域</dd>
<dt>jQuery(&#8216;table.orders tr:odd&#8217;)</dt>
<dd>选取了类名为“orders”的表中所有的奇数行
      </dd>
<dt>jQuery(&#8216;a[@href^=&quot;http://&quot;]&#8216;)</dt>
<dd>选取了所有（以http://开头的）外部链接</dd>
<dt>jQuery(&#8216;p[a]&#8216;)</dt>
<dd>选取了所有包含一个或多个链接的段落
      </dd>
</dl>
<p>上述例子中，:visible和:odd是jQuery实现的扩展，很具特色。而属性的选取使用@作为标记，其方式和XPath一样，要优于CSS 2。
    </p>
<p>
    </p>
<p>jQuery的这套选取语法包罗万象，有些类似正则表达式，想完全消化是需要花上一段时间的。</p>
<h2>把玩一下（Doing stuff with them）</h2>
<p>
      通过jQuery的选取操作，我们能得到一些很棒的“素材”（beast）。它们是一个集合，包含了DOM元素，并且类似数组那样，拥有length属性；通过索引可以访问集合中的元素。在<a title="Firebug" href="http://www.getfirebug.com/">Firebug</a> console的交互模式下，集合也被显示成一个数组，这个特性非常有用。集合实际上是一个jQuery对象，这个对象被赋予了很多方法（methods），用来查询，修改，扩展集合中的元素。</p>
<p>      jQuery的方法（methods），本质上可分成三种：一种可以操作那些符合匹配的元素；一种可以返回第一个匹配到的对象的值；一种可以变更被选取的集合。</p>
<p>      我不会列出所有的方法（可参考<a title="visualjquery.com" href="http://visualjquery.com/">visual</a><a target="_blank" href="http://www.yeeyan.com/articles/tag/jquery"><em>jquery</em></a>.com），但我用例子做一下说明。如果你的浏览器装了Firebug，你可以以交互方式运行这些示例代码：首先使用这个<a title="bookmarklet">bookmarklet（译注</a>[1]）把jQuery库载入至浏览器的任意页面，然后把示例代码粘贴到Firebug console中。
    </p>
<dl>
<dt>jQuery(&#8216;div#primary&#8217;).width(300);</dt>
<dd>把id=&quot;primary&quot;的div的宽度设为300px</dd>
<dt>jQuery(&#8216;p&#8217;).css(&#8216;line-height&#8217;, &#8216;1.8em&#8217;);</dt>
<dd>把所有段落的line-height设为1.8em</dd>
<dt>jQuery(&#8216;li:odd&#8217;).css({color: &#8216;white&#8217;, backgroundColor: &#8216;black&#8217;});</dt>
<dd>向间隔的list项添加两个CSS规则；注意css()函数可以用一个对象来代替两个字符串作为参数</dd>
<dt>jQuery(&#8216;a[@href^=&quot;http://&quot;]&#8216;).addClass(&#8216;external&#8217;).attr(&#8216;target&#8217;, &#8216;_blank&#8217;);</dt>
<dd>向所有（以http://开头的）外部链接添加“external”类，然后策略性地加上target=&quot;_blank&quot;属性。这个示例用到了<strong>链盒</strong>（chaining），稍后会做介绍。</dd>
<dt>jQuery(&#8216;blockquote&#8217;).each(function(el) { alert(jQuery(this).text()) });</dt>
<dd>遍历页面上的每个&lt;blockquote&gt;，并显示出它的文字内容（包括HTML标签）</dd>
<dt>jQuery(&#8216;a&#8217;).html(&#8216;Click here!&#8217;);</dt>
<dd>用阴险的“Click here!”代替页面上所有的链接&lt;a&gt;的文字</dd>
</dl>
<p>下面的示例展示了jQuery如何取得第一个匹配到的对象的值：
    </p>
<dl>
<dt>var width = jQuery(&#8216;div&#8217;).width();</dt>
<dd>页面上第一个div的宽度</dd>
<dt>var src = jQuery(&#8216;img&#8217;).attr(&#8217;src&#8217;);</dt>
<dd>页面上第一张图片的src属性值</dd>
<dt>var color = jQuery(&#8216;h1&#8242;).css(&#8216;color&#8217;);</dt>
<dd>第一个&lt;h1&gt;的颜色样式值</dd>
</dl>
<p>在jQuery  的方法构造中，蕴含着令人惬意的对称性：当向方法传递两个参数或一个对象时，方法可被用来执行设置操作；如果只向方法传递一个参数，则可以让它执行取值操  作（译注：读者可对照上面的示例代码感受一下）。这种对称性设计贯穿了jQuery体系，使得API的文法更容易被记忆。</p>
<p>      本节最后的例子，展示了一些可变更被选取的元素集合的方法。这些方法大多都提高了检索DOM的简易程度：
    </p>
<dl>
<dt>jQuery(&#8216;div&#8217;).not(&#8216;[@id]&#8216;)</dt>
<dd>返回那些没有id属性的div</dd>
<dt>jQuery(&#8216;h2&#8242;).parent()</dt>
<dd>返回那些是&lt;h2&gt;的直接父节点元素</dd>
<dt>jQuery(&#8216;blockquote&#8217;).children()</dt>
<dd>返回所有&lt;blockquote&gt;的子节点元素</dd>
<dt>jQuery(&#8216;p&#8217;).eq(4).next()</dt>
<dd>在页面上找到第五个段落（译注：因为集合的元素索引从0开始），然后根据节点的树层结构关系，找到并返回这个段落节点右侧的兄弟节点元素</dd>
<dt>jQuery(&#8216;input:text:first&#8217;).parents(&#8216;form&#8217;)</dt>
<dd>找到并返回页面上第一个type=&quot;text&quot;的输入域所在的form节点元素，parents()的可选参数是另一个选择器</dd>
</dl>
<h2>链盒（Chaining）</h2>
<p>
      jQuery  开发团队经常夸耀jQuery的链盒理念（译注[2]），甚至在网站首页上宣扬“jQuery将改变你编写JavaScript的方式”。我个人感觉，这  么做多少有点误导大众，我愿意告诉大家，你完全可以取jQuery之长，却应避免冗长的方法链盒（chains of methods）。</p>
<p>      也  就是说，链盒有时会像变戏法一样。除了使用链盒将各种操作DOM的方法粘到一起，你也可以使用jQuery的end()方法，来实现在特定范围内推进或回  溯你需要得到的元素。这个概念很难解释清楚。本质上讲，每次使用（诸如children()或filter()）方法来改变元素集合时，你可以在这些方法  之后使用end()，来重新定位你最初选取的元素集合。关于这点，Jesse Skinner在他的<a title="Simplify Ajax development with jQuery" href="http://www-128.ibm.com/developerworks/library/x-ajaxjquery.html">Simplify Ajax development with jQuery</a>（译注[3]）教程中给出了实例：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form#login'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #006600; font-style: italic;">// 第一步，隐藏表单中那些带有'optional'类的&amp;lt;label&amp;gt;</span>
   .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'label.optional'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  
   <span style="color: #006600; font-style: italic;">// 第二步，为表单的密码输入域渲染上红色边框    </span>
   .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input:password'</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;">'1px solid red'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>   
    <span style="color: #006600; font-style: italic;">// 第三步，为表单加上提交处理 </span>
    .<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">confirm</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Are you sure you want to submit?'</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>这个示例读起来就像句俏皮话。整个过程是，先选取一个表单，再在其中选取一些元素做修改，然后回溯到表单，为它定义一个submit()处理。
    </p>
<p>
    </p>
<p>示例很酷，但如果你不习惯，也可以不这么用。我就很乐意用自定义变量来规划代码。
    </p>
<p>
    </p>
<h2>操作DOM（DOM Manipulation）</h2>
<p>
      jQuery提供了几个大规模操作DOM的卓越方法。第一种非常让人惊叹：jQuery()函数能把HTML片段插入DOM元素中（实际上，函数会留意以&#8217;&lt;&#8217;打头的字符串参数）：
    </p>

<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: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;div&amp;gt;Some text&amp;lt;/div&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>一旦你创建好了div，便可以继续用链盒向其添加属性：
    </p>

<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: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;div&amp;gt;Some text&amp;lt;/div&amp;gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'inserted'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>现在把div加到body上：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">div.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>或用选择器把div加到已知元素的前面：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">div.<span style="color: #660066;">prependTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div#primary'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<h2> 处理事件（Handling events） </h2>
<p>
      任  何JavaScript库都需要事件处理能力，jQuery也不例外。类似attr()和css()的行为，各种与事件处理相关的方法也有双重用途：一种  是把函数当作参数，赋给事件处理器；一种是不带参数，可以模拟事件被触发（译注：前提是事件已经定义，可参考visual<a target="_blank" href="http://www.yeeyan.com/articles/tag/jquery"><em>jquery</em></a>.com &gt; Events &gt; click()）：
    </p>
<dl>
<dt>jQuery(&#8216;p&#8217;).click(function() { jQuery(this).css(&#8216;background-color&#8217;, &#8216;red&#8217;); });</dt>
<dd>为所有段落增加点击事件，当你点击它们时，段落背景会变成红色</dd>
<dt>jQuery(&#8216;p:first&#8217;).click()</dt>
<dd>然后在第一个段落上模拟点击的动作，它的背景会变成红色
      </dd>
</dl>
<p>类似的函数还包括mouseover，keyup等，对应着浏览器通常支持的那些动作。留意一下事件处理中的&#8217;this&#8217;关键字，它代表触发事件的元素；jQuery(this)是一种惯用语法，可以让this所代表的元素应用各种jQuery方法。</p>
<p>      这里有两个与事件相关的函数值得仔细说一下：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'white'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>hover()可设定两个函数，分别对应onmouseover和onmouseout事件。
    </p>

<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;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">one</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>one()设定的事件在第一次被触发后便被移除。上面的示例会让所有段落在第一次被点击时显示其文字内容。</p>
<p>      凭借bind()和trigger()方法，jQuery也可以支持自定义事件（click()家族仅仅是便捷方法，只支持有限的事件）。自定义事件可接受参数，trigger()可接受数组作为参数，来做各种处理操作：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stuffHappened'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stuff happened: '</span> <span style="color: #339933;">+</span> msg<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>
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stuffHappened'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Hello!'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>渐进式编码（Unobtrusive scripting）</h2>
<p>
      本小节的标题很令我钟意。我一直认为，最好的Web应用程序，往往是那些在脚本被禁用后仍能正常使用的程序。想建立这样的应用程序，最好的方法就是遵循渐进式编码，让普通页面完全加载后，再为页面中的元素赋以事件处理（更多信息可参考<a title="渐进式编码" href="http://onlinetools.org/articles/unobtrusivejavascript/">渐进式编码</a>和<a title="Hijax" href="http://domscripting.com/blog/display/41">Hijax</a>）。</p>
<p>      jQuery对这种编码策略提供了绝好支持。首先，从整体上看，节点选取暗合jQuery以及渐进式编码的核心理念。其次，针对<a title="window.onload" href="http://peter.michaux.ca/article/553">window.onload</a>问题，jQuery提供了一套解决方案，这套方案借鉴了Dean Edward的成果，使得以“DOM加载完毕”为信号的事件能跨浏览器工作。你可以在浏览器完全加载DOM后设定并运行一个函数，如下所示：
    </p>

<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;">jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The DOM is ready!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>你甚至可以直接传递一个函数给jQuery()，以更简洁的方式达到同样效果：
    </p>

<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;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The DOM is ready!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>jQuery与Ajax（jQuery and Ajax）</h2>
<p>
      在我所知道的主流JavaScript库中，jQuery拥有最棒的Ajax API。最简单的Ajax调用如：
    </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div#intro'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/some/fragment.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>代码以GET请求方式，从/some/fragment.html文件中获取HTML片段，并把片段装载到id=&quot;intro&quot;的div中。</p>
<p>      当  我第一次看到这行代码时，几乎对它没什么印象。这看起来非常简洁，但如果你想用jQuery做些更复杂的事情，比如显示Ajax装载进度，该如何做呢？  jQuery为你准备了一些可自定义的事件（ajaxStart，ajaxComplete，ajaxError等等），来实现你想要的代码。同时  jQuery也提供了广泛的底层API，来实现更复杂的Ajax交互：
    </p>

<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;">jQuery.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/some/script.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Simon'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The server said: '</span> <span style="color: #339933;">+</span> data<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: #006600; font-style: italic;">// 以GET方式通过/some/script.php?name=Simon获取数据</span>
jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/some/script.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Simon'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The server said: '</span> <span style="color: #339933;">+</span> data<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: #006600; font-style: italic;">// 以POST方式向/some/script.php发送请求</span>
jQuery.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/some.&lt;a target=&quot;_blank&quot; href=&quot;http://www.yeeyan.com/articles/tag/js&quot;&gt;&lt;em&gt;js&lt;/em&gt;&lt;/a&gt;on'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'JSON rocks: '</span> <span style="color: #339933;">+</span> <span style="color: #339933;">&lt;</span>a target<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;_blank&quot;</span> href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://www.yeeyan.com/articles/tag/js&quot;</span><span style="color: #339933;">&gt;&lt;</span>em<span style="color: #339933;">&gt;</span>js<span style="color: #339933;">&lt;/</span>em<span style="color: #339933;">&gt;&lt;/</span>a<span style="color: #339933;">&gt;</span>on.<span style="color: #660066;">foo</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> json.<span style="color: #660066;">bar</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: #006600; font-style: italic;">// 从/some.&lt;a target=&quot;_blank&quot; href=&quot;http://www.yeeyan.com/articles/tag/js&quot;&gt;&lt;em&gt;js&lt;/em&gt;&lt;/a&gt;on接收并解析数据，把数据转换成JSON格式</span>
jQuery.<span style="color: #660066;">getScript</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/script.&lt;a target=&quot;_blank&quot; href=&quot;http://www.yeeyan.com/articles/tag/js&quot;&gt;&lt;em&gt;js&lt;/em&gt;&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #006600; font-style: italic;">// 以GET方式获取/script.js脚本并用eval()执行</span></pre></td></tr></table></div>

<h2> 插件（Plugins）</h2>
<p>
      就你所能获得的功能的数量而言，jQuery库其实是相当小的——对代码做紧凑处理后只有20KB左右，甚至用gzip压缩后会变得更小。向标准库添加额外功能时，需用插件的方式来做，它可以（也确实能够）向现有的jQuery实例对象添加全新的方法。如果你想执行：
    </p>

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

<p>jQuery的插件机制提供了文档说明型的挂载方式（documented hooks），可以实现把上述方法添加到jQuery中。这种简易的创建形式，吸引了很多插件作者，他们让人印象深刻；现在<a title="插件目录" href="http://jquery.com/plugins/">插件目录</a>中已经有超过100个插件了。</p>
<p>      真正绝妙的，是你可以像自定义方法那样，来定义选择器。比如，<a title="moreSelectors" href="http://jquery.com/plugins/project/moreSelectors">moreSelectors</a>插件实现了诸如“div:color(red)”的方法，来匹配包含红色文本的div。</p>
<h2>  并非天衣无缝</strong>（Leaky abstractions）</h2>
<p>
      在  发掘jQuery各种特性的同时，我也被某个我视之为教条（philosophical  blocker）的东西所折磨着。几年来，我总是建议大家使用一种JavaScript库，前提是你们愿意梳理它的源码，并把它的工作原理彻底搞懂。我发  出如此论调，是基于Joel Spolsky的<a title="不健全抽象的法则" href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">不健全抽象的法则</a>（译注[4]）。在那篇文章中，Joel指出，API把复杂性隐藏的越多，当它出现无法应付的意外时，你越有可能遭遇更多的麻烦。浏览器平台是不健全抽象的最佳代表，所以当库无法帮你摆脱困境时，你要自寻解药。保持警觉非常重要。</p>
<p>      jQuery  使用了相当不可思议的技术，以求实现它所设想的各种功能——其中一些（比如选择器的代码）真是震天骇地。如果有必要彻底搞懂一个库的工作原理，那么对大多  数开发人员来说，jQuery不会是上佳之选。然而，jQuery拥有极高的人气，并且没有太多与之相关的恐怖经典流于街巷（译注：原文是a  distinct lack of horror  stories，比如微软Win95的“蓝屏”就是恐怖经典:），所以具体到jQuery所用技艺的邪正之分，也就变得不那么重要了。</p>
<p>      我  想，我必须重新审视曾给大家的建议。库的运作机制并不是问题焦点：关键是应看清更具普遍性的潜在问题，知晓浏览器之间的差别，以及你使用库的哪种技术，来  消除差别造成的负面影响。没有哪种库可以一劳永逸地帮你克服浏览器的古怪行为。但只要你对应付潜在问题训练有素，便可把握经脉，指出问题的源头——无论它  们来自你自己编写的代码，还是库或者应付策略本身。</p>
<h2> 结语（To conclude）</h2>
<p>
      我费了那么多口舌，希望能让大家明白，jQuery不只是<strong>又一个JavaScript库</strong>那么简单——它蕴含了很多值得品味揣摩的理念，甚至能启迪那些骨灰级的JavaScript程序员。如果你不打算尝试jQuery，但仍值得去花些时间探索一下jQuery的生态体系（the jQuery ecosystem）。</p>
<p>      Simon Willison写于2007年8月15日 凌晨2:27</p>
<p>      <strong>译注：</strong></p>
<p>      [1]  bookmarklet在原文中指的是一段“Insert jQuery”的JavaScript代码，由于译者使用Google  Docs进行在线翻译，链接中的JS代码被编辑器屏蔽掉了，下面列出的代码可粘贴到浏览器的地址栏中执行，执行后才可以继续用示例代码查看jQuery的  选取效果：<br />
      <a target="_blank" href="http://www.yeeyan.com/articles/tag/javascript"><em>javascript</em></a>:void(function(){var s=document.createElement(&#8217;script&#8217;);s.src=&#8217;http://code.<a target="_blank" href="http://www.yeeyan.com/articles/tag/jquery"><em>jquery</em></a>.com/jquery-1.1.2.js&#8217;;document.getElementsByTagName(&#8216;head&#8217;)[0].appendChild(s);}())</p>
<p>      [2] 本文使用的术语“链盒”，大抵可对应单词chain/chaining/chainable；译者在参考<a title="jQuery Magazine issue 1" href="http://visualjquery.com/magazine/issue1.pdf">jQuery Magazine issue 1</a>对jQuery  选择器运行方式的图解后，确定了这种译法。汉字是象形文字，按“盒”字的构造来体会jQuery颇有意趣：比如上面的“人”字，不正是选择器“吐出”特定  元素的“嘴”吗？而“人”字下面“一”“口”“皿”的逐层累积，其形状又类似jQuery的Logo，并让人联想到链式选取过程中不同的元素集合；“盒”  即是“桶”（bucket），译者自以为绝妙！</p>
<p>      [3] IBM developerWorks中国的官方翻译版本《<a title="使用jQuery简化Ajax开发" href="http://www.ibm.com/developerworks/cn/xml/x-ajaxjquery.html">使用jQuery简化Ajax开发</a>》；译言版本分<a title="1" href="http://yeeyan.com/articles/view/linghucong/1395">1</a>、<a title="2" href="http://yeeyan.com/articles/view/linghucong/1414">2</a>两部分，由<a title="令狐葱" href="http://yeeyan.com/space/show/linghucong">令狐葱</a>翻译。</p>
<p>      [4] Law of Leaky Abstractions: <strong>All non-trivial abstractions, to some degree, are leaky.</strong> 详细解读请见<a title="原文" href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">原文</a>。</p>
<h2>中文译文来源：</h2>
<p>原文网站：<a href="http://www.yeeyan.com">译言网</a>      </p>
<p>原文地址：<a href="http://www.yeeyan.com/articles/view/davidkoree/4568"> http://www.yeeyan.com/articles/view/davidkoree/4568</a></p>
<p>译者：<a href="http://www.yeeyan.com/space/show/davidkoree">davidkoree</a></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/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/05/22 -- <a href="http://cssrainbow.cn/tutorials/jquery/247.html" title="Tipsy &#8211; Facebook风格的Tooltip">Tipsy &#8211; Facebook风格的Tooltip</a> (0)</li><li>2009/05/18 -- <a href="http://cssrainbow.cn/tutorials/jquery/226.html" title="jQuery Page Peel ">jQuery Page Peel </a> (0)</li><li>2009/04/30 -- <a href="http://cssrainbow.cn/articles/resources/180.html" title="10个基于jQuery或JavaScript的WYSIWYG编辑器">10个基于jQuery或JavaScript的WYSIWYG编辑器</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/07/27 -- <a href="http://cssrainbow.cn/tutorials/javascript/1354.html" title="本地图片预览二三事">本地图片预览二三事</a> (1)</li><li>2010/06/30 -- <a href="http://cssrainbow.cn/tutorials/jquery/1347.html" title="盒子（Box）中间的链接（Links）,基于MooTools or jQuery ">盒子（Box）中间的链接（Links）,基于MooTools or jQuery </a> (5)</li><li>2010/06/29 -- <a href="http://cssrainbow.cn/code-snippets/mootools-code-snippets/1342.html" title="使用MooTools, jQuery, and Dojo 来完成常见任务的语法归类">使用MooTools, jQuery, and Dojo 来完成常见任务的语法归类</a> (0)</li><li>2010/06/27 -- <a href="http://cssrainbow.cn/code-snippets/mootools-code-snippets/1333.html" title="在我喜欢的JavaScript Frameworks中使用匿名方法">在我喜欢的JavaScript Frameworks中使用匿名方法</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/jquery/706.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>有滑动效果的导航条，基于JavaScript</title>
		<link>http://cssrainbow.cn/tutorials/javascript/703.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/703.html#comments</comments>
		<pubDate>Tue, 08 Sep 2009 09:53:52 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Menu]]></category>
		<category><![CDATA[Navigation]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=703</guid>
		<description><![CDATA[此例子是基于JavaScript写的Sliding Menu。构思巧妙，代码短小，高效。通过主流的浏览器测试。]]></description>
			<content:encoded><![CDATA[<p><P>此例子是基于JavaScript写的Sliding Menu。构思巧妙，代码短小，高效。通过主流的浏览器测试。</P></p>
<p><img src="http://cssrainbow.cn/Project/JavaScript/demo5/s-m1.jpg" width="463" height="221"  alt="Sliding Menu"/></p>
<p><img src="http://cssrainbow.cn/Project/JavaScript/demo5/s-m2.jpg" width="463" height="221"  alt="Sliding Menu"/></p>
<p>演示一：<a href="http://cssrainbow.cn/Project/JavaScript/demo5/menueffect/index.html">http://cssrainbow.cn/Project/JavaScript/demo5/menueffect/index.html</a></p>
<p>演示二：<a href="http://cssrainbow.cn/Project/JavaScript/demo5/menueffect/index2.html">http://cssrainbow.cn/Project/JavaScript/demo5/menueffect/index2.html</a></p>
<p>下载：<a href="http://cssrainbow.cn/Project/JavaScript/demo5/menueffect.rar">http://cssrainbow.cn/Project/JavaScript/demo5/</a></p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2009/09/24 -- <a href="http://cssrainbow.cn/tutorials/jquery/742.html" title="模仿Flickr的下拉导航条，基于MooTools &#038; jQuery实现的">模仿Flickr的下拉导航条，基于MooTools &#038; jQuery实现的</a> (0)</li><li>2009/09/21 -- <a href="http://cssrainbow.cn/tutorials/mootools/731.html" title="基于MooTools 的下拉菜单(drop down menu)">基于MooTools 的下拉菜单(drop down menu)</a> (0)</li><li>2009/07/07 -- <a href="http://cssrainbow.cn/tutorials/jquery/389.html" title="jQuery竖向的导航栏">jQuery竖向的导航栏</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/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/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></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/javascript/703.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FireBug 控制台指令</title>
		<link>http://cssrainbow.cn/tutorials/javascript/693.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/693.html#comments</comments>
		<pubDate>Fri, 04 Sep 2009 01:06:23 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[FireBug]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=693</guid>
		<description><![CDATA[FireBug 是一个非常实用的JavaScript以及DOM查看调试工具，是 Firefox 的一个插件。

使用 FireBug 调试 AJAX 应用非常方便，终于可以告别 alert 时代了！ Console Logging 函数FireBug 为所有 Web 页面提供了一个 console 对象。

接下来我们就学习一下这个对象所包含的函数： ]]></description>
			<content:encoded><![CDATA[<p>FireBug 是一个非常实用的JavaScript以及DOM查看调试工具，是 Firefox 的一个插件。</p>
<p>使用 FireBug 调试 AJAX   应用非常方便，终于可以告别 alert 时代了！<br />
  Console Logging 函数FireBug 为所有 Web 页面提供了一个 console   对象。</p>
<p>接下来我们就学习一下这个对象所包含的函数： </p>
<h2>Logging 基础</h2>
<p>console.log(&quot;message&quot; [,objects]) &#8211;   将一个字符串打印到控制台。</p>
<p>字符串可以包含任何“String   Formatting”小节描述的模式。</p>
<p>字符串后面的对象应该用来取代之前字符串中的模式。（译者注：大家用过C里面 printf   吧，效果基本是一样的。）</p>
<p>
  Logging 等级通常根据不同的等级来区分Logging的严重程度是很有帮助的。</p>
<p><span id="more-693"></span></p>
<p>FireBug   提供了4个等级。</p>
<p>为了达到视觉分离的效果，这些函数与 log   不同的地方就是它们在被调用的时候会自动包含一个指向代码行数的链接。</p>
<p>console.debug(&quot;message&quot; [,objects]) &#8211; 记录一个   debug 消息。</p>
<p>console.info(&quot;message&quot; [,objects]) &#8211;   记录一个信息.</p>
<p>console.warn(&quot;message&quot; [,objects]) &#8211;   记录一个警告.</p>
<p> console.error(&quot;message&quot; [,objects]) &#8211;   记录一个错误.</p>
<h3>断言断言是一条确保代码规则的非常好的途径.</h3>
<p>console   对象包含了一系列各种类型的断言函数，并且允许你编写自己的断言函数。</p>
<p>
console.assert(a, &quot;message&quot; [,objects]) &#8211;   Asserts that an a is true.</p>
<p>
console.assertEquals(a, b, &quot;message&quot; [,objects]) &#8211;   Asserts that a is equal to b.</p>
<p>
console.assertNotEquals(a, b, &quot;message&quot;   [,objects]) &#8211; Asserts that a is not equal to b.</p>
<p>
console.assertGreater(a, b,   &quot;message&quot; [,objects]) &#8211; Asserts that a is greater than   b.</p>
<p>
console.assertNotGreater(a, b, &quot;message&quot; [,objects]) &#8211; Asserts that a is   not greater than b.</p>
<p>
console.assertLess(a, b, &quot;message&quot; [,objects]) &#8211; Asserts   that a is less than b.</p>
<p>
console.assertNotLess(a, b, &quot;message&quot; [,objects]) &#8211;   Asserts that a is not less than b.</p>
<p>
console.assertContains(a, b, &quot;message&quot;   [,objects]) &#8211; Asserts that a is in the array b.</p>
<p>
console.assertNotContains(a,   b, &quot;message&quot; [,objects]) &#8211; Asserts that a is not in the array   b.</p>
<p>
console.assertTrue(a, &quot;message&quot; [,objects]) &#8211; Asserts that a is equal to   true.</p>
<p>
console.assertFalse(a, &quot;message&quot; [,objects]) &#8211; Asserts that a is equal   to false.</p>
<p>
console.assertNull(a, &quot;message&quot; [,objects]) &#8211; Asserts that a is   equal to null.</p>
<p>
console.assertNotNull(a, &quot;message&quot; [,objects]) &#8211; Asserts that   a is not equal to null.</p>
<p>
console.assertUndefined(a, &quot;message&quot; [,objects]) &#8211;   Asserts that a is equal to undefined.</p>
<p>
console.assertNotUndefined(a, &quot;message&quot;   [,objects]) &#8211; Asserts that a is not equal to   undefined.</p>
<p>
console.assertInstanceOf(a, b, &quot;message&quot; [,objects]) &#8211; Asserts   that a is an instance of type b.</p>
<p>
console.assertNotInstanceOf(a, b, &quot;message&quot;   [,objects]) &#8211; Asserts that a is not an instance of type   b.</p>
<p>
console.assertTypeOf(a, b, &quot;message&quot; [,objects]) &#8211; Asserts that the type   of a is equal to the string b.</p>
<p>
console.assertNotTypeOf(a, b, &quot;message&quot;   [,objects]) &#8211; Asserts that the type of a is not equal to the string   b.</p>
<h3>测量（Measurement）下面的一些函数可以让你方便的测量你的一些代码。</h3>
<p>
console.trace() &#8211;   记录执行点的堆栈信息。</p>
<p> console.time(&quot;name&quot;) &#8211; 根据 name   创建一个唯一的计时器。</p>
<p> console.timeEnd(&quot;name&quot;) &#8211; 根据 name   停止计时器，并且记录消耗的时间，以毫秒为单位。</p>
<p> console.count(&quot;name&quot;) &#8211; 记录该行代码执行的次数。</p>
<h3>字符串格式化所有   console 的 logging 函数都可以通过以下模式格式化字符串：</h3>
<p>
%s &#8211; 将对象格式化为字符串。</p>
<p>
  %d, %i, %l, %f &#8211;   将对象格式化为数字。</p>
<p>
%o &#8211; 将对象格式化成一个指向 inspector 的超链接。</p>
<p>
%1.o, %2.0, etc.. &#8211;   将对象格式化成包含自己属性的可交互的表格。</p>
<p>
%.o &#8211; 将对象格式化成具有自身属性的一个数组。</p>
<p>
%x &#8211; 将对象格式化成一个可交互的 XML   树形结构。</p>
<p>
%1.x, %2.x, etc.. &#8211; 将对象格式化成一个可交互的 XML 数型结构，并且展开 n 层节点。</p>
<p>
如果你需要一个真实的 %   符号，你可以通过一个转移符号就像这样 &quot;\%&quot;。</p>
<h3>
  命令行函数内建的命令行函数可以通过以下命令行使用：</h3>
<p>
$(&quot;id&quot;) &#8211;   document.getElementById() 的简写。（译者注：跟 prototype.js 学来的吧？）</p>
<p>
$$(&quot;css&quot;) &#8211; 返回一个符合   CSS 选择器的元素数组。</p>
<p>
$x(&quot;xpath&quot;) &#8211; 返回一个符合 XPath 选择器的元素数组。</p>
<p>
$0 &#8211;   返回最近被检查（inspected）的对象。</p>
<p>
$1 &#8211; 返回最近被检查（inspected）的下一个对象。</p>
<p>
$n(5) &#8211;   返回最近被检查的第n个对象。</p>
<p>
inspect(object) &#8211; 将对象显示在 Inspector 中。</p>
<p>
dir(object) &#8211;   返回一个对象的属性名数组。（译者注：跟 Python 学的？）</p>
<p>
  clear() &#8211; 清除控制台信息。 </p>
<h2>&nbsp;</h2>
<p>文章来源：<a href="http://www.nearking.com/">爱在戏院前 </a></p>
<p>原文链接地址：<a href="http://www.nearking.com/article.asp?id=11">http://www.nearking.com/article.asp?id=11</a></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/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/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/693.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>快速掌握JavaScript正则表达式</title>
		<link>http://cssrainbow.cn/tutorials/javascript/604.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/604.html#comments</comments>
		<pubDate>Mon, 10 Aug 2009 15:34:55 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript基础]]></category>
		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=604</guid>
		<description><![CDATA[快速掌握JavaScript正则表达式]]></description>
			<content:encoded><![CDATA[<h2>EcmaScript正則表達式( 深入淺出系列之淺出 ^_^ )</h2>
<h3>使用方法</h3>
<p>創建對象<br />
  var r = new RegExp(&quot;表達式&quot;,&quot;ig&quot;);<br />
  適合在動態構造匹配對象時使用；<br />
  表達式中轉義符需要使用兩次，如\\d;</p>
<p>
  直接量<br />
  var r = /表達式/ig;<br />
  使用在任何固定模式的匹配;</p>
<h3>常用屬性</h3>
<p>$1&#8230;$9<br />
  返回九个在模式匹配期间找到的、最近保存的部分。只读。<br />
  index<br />
  返回第一个成功匹配的开始位置。只读。<br />
  lastIndex??? *(注意)<br />
  返回最后一次成功匹配的位置(下一次匹配開始的位置)。讀寫。<br />
  只有當使用exec()或test()函數時才會寫入。<br />
  lastMatch<br />
  返回最后匹配的字符。只读。<br />
  input<br />
  返回查找的字符串。只读。</p>
<p><span id="more-604"></span></p>
<h3>lastIndex</h3>
<p>考慮以下代碼：</p>
<p>  &lt;script type=&quot;text/javascript&quot;&gt;</p>
<p>  var r = /\d/g;</p>
<p>  alert(r.test(&#8216;abc1&#8242;));<br />
  alert(r.lastIndex);</p>
<p>  alert(r.test(&#8216;ab1&#8242;));<br />
  alert(r.lastIndex);</p>
<p>  alert(r.test(&#8216;a1&#8242;));<br />
  alert(r.lastIndex);</p>
<p>  &lt;/script&gt;</p>
<p>lastIndex屬于實例屬性</p>
<p>  當一個正則表達式實例多次test或exec時，它的lastIndex將每一次都被更改。</p>
<h3>量詞</h3>
<p>?<br />
  出現0次或1次<br />
  *<br />
  出現0次或多次<br />
  +<br />
  出現1次或多次<br />
  {n}<br />
  一定出現n次<br />
  {n,m}<br />
  至少出現n次，最多出現m次<br />
  {n,}<br />
  至少出現n次</p>
<h3>貪婪與非貪婪（惰性）的量詞</h3>
<p>貪婪匹配：<br />
  ?<br />
  *<br />
  +<br />
  {n}<br />
  {n,m}<br />
  {n,}</p>
<p>非貪婪匹配：<br />
  ??<br />
  *?<br />
  +?<br />
  {n}?<br />
  {n,m}?<br />
  {n,}?</p>
<h3>貪婪與非貪婪的匹配過程</h3>
<p>考慮以下代碼：</p>
<p>  &lt;script type=&quot;text/javascript&quot;&gt;</p>
<p>  var s = &#8216;http://www/hello/world.php&#8217;;</p>
<p>  var r1 = /.*\//g;</p>
<p>  var r2 = /.*?\//g;</p>
<p>
  alert(s.match(r1));</p>
<p>  alert(s.match(r2));</p>
<p>  &lt;/script&gt;</p>
<p>//貪婪匹配<br />
  匹配整個字符串<br />
  從后逐一舍去最后一個字符繼續</p>
<p>http://www/hello/</p>
<p>//非貪婪匹配<br />
  從第一個字符開始匹配<br />
  如果失敗，讀入下一個字符繼續<br />
  http:/,??? /,??? www/,??? hello/</p>
<h3>分組、反向引用與非捕獲性分組</h3>
<p>用來匹配以一系列字符為一個單位的模式<br />
  (一系列的字符作為一個整體哦)<br />
  每創建一個分組，在執行test()、match()、search()、replace()后，可以通過全局$1&#8230;$9屬性來獲取匹配到的每一個分組。<br />
  分組匹配將帶來一定的效率損耗，如果有時候不需要獲取分組的結果，可以使用非捕獲性分組：(?:一系列的字符)</p>
<h3>候選</h3>
<p>用來匹配a or b or n&#8230;模式。比如，匹配文件類型：</p>
<p>  &lt;script type=&quot;text/javascript&quot;&gt;</p>
<p>  var s = &#8216;http://www/hello/world.png.php&#8217;;</p>
<p>  var r = /(?:jpg|gif|png)$/i;</p>
<p>  alert(r.test(s));</p>
<p>  &lt;/script&gt;</p>
<h3>前瞻</h3>
<p>正向前瞻<br />
  var r = /(aaa(?=bbb))/;<br />
  用于匹配后面緊挨著的是bbb的串aaa</p>
<p>
  負向前瞻<br />
  var r = /(aaa(?!bbb))/;<br />
  用于匹配后面緊挨著的不是bbb的串aaa</p>
<h3>Replace</h3>
<p>Replace中使用正則，第二個參數是函數!</p>
<p>  &lt;script type=&quot;text/javascript&quot;&gt;<br />
  var str = &#8216;aaZbbZccZ&#8217;;<br />
  var s = str.replace(/(.*?)Z/g,<br />
  function(){<br />
  var ar = [];<br />
  for(var i=0;i&lt;arguments.length;i++){<br />
  ar.push(arguments[i]);<br />
  }<br />
  alert(ar);<br />
  ar.length = 0;<br />
  return arguments[2];<br />
  }<br />
  );<br />
  alert(s);<br />
  &lt;/script&gt;</p>
<p>
  當有反向引用時：<br />
  arguments[0]：匹配到的串<br />
  arguments[1]：$1<br />
  arguments[2]：$2<br />
  &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<br />
  arguments[arguments.length-2]：匹配到的位置<br />
  arguments[arguments.length-1]：要匹配的字符串</p>
<p>  否則，有3個形參：<br />
  arguments[0]：匹配到的串<br />
  arguments[1]：匹配到的位置<br />
  arguments[2]：要匹配的字符串
</p>
<p>Replace應用</p>
<p>不用循环输出一至一百：</p>
<p>  &lt;script type=&quot;text/javascript&quot;&gt;</p>
<p>  var sAAA = new Array(100+1).join(&quot;a&quot;);</p>
<p>  var sNum = sAAA.replace(/a/g,function(){<br />
  return arguments[1]+1+&quot;&lt;br/&gt;&quot;<br />
  });</p>
<p>  document.write(sNum);</p>
<p>  &lt;/script&gt;</p>
<p>替換所有&lt;&gt;為[]：</p>
<p>  &lt;script type=&quot;text/javascript&quot;&gt;</p>
<p>  var str = &#8216;&lt;a&gt;a&lt;/a&gt;&lt;b&gt;b&lt;/b&gt;&lt;c&gt;c&lt;/c&gt;&#8217;;</p>
<p>  var s = str.replace(/(&lt;)|&gt;/g,function(x,y){<br />
  return (y)?&#8217;[':']&#8216;;<br />
  });</p>
<p>  alert(s);</p>
<p>  &lt;/script&gt;</p>
<p><a href="http://www.v-ec.com/dh20156/code/EcmaScriptRegExp.ppt" target="_blank">快速掌握JavaScript正则表达式PPT下载：http://www.v-ec.com/dh20156/code/EcmaScriptRegExp.ppt</a></p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2009/07/25 -- <a href="http://cssrainbow.cn/tutorials/javascript/528.html" title="JavaScript基础：变量的作用域">JavaScript基础：变量的作用域</a> (0)</li><li>2009/07/24 -- <a href="http://cssrainbow.cn/tutorials/javascript/526.html" title="关于window.onload加载的多种解决方案">关于window.onload加载的多种解决方案</a> (2)</li><li>2009/07/22 -- <a href="http://cssrainbow.cn/tutorials/javascript/521.html" title="JavaScript基础：JS函数中声明的所有变量在整个函数中都是有定义的">JavaScript基础：JS函数中声明的所有变量在整个函数中都是有定义的</a> (0)</li><li>2009/07/21 -- <a href="http://cssrainbow.cn/tutorials/javascript/494.html" title="Javascript的匿名函数(收藏)">Javascript的匿名函数(收藏)</a> (0)</li><li>2009/07/14 -- <a href="http://cssrainbow.cn/tutorials/javascript/419.html" title="给JavaScript新手的24条建议">给JavaScript新手的24条建议</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/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/31 -- <a href="http://cssrainbow.cn/code-snippets/javascript-code-snippets/1058.html" title="如何在事件代理中正确使用 focus 和 blur 事件">如何在事件代理中正确使用 focus 和 blur 事件</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/javascript/604.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>七(4) . 最常见的JavaScript DOM方法实战-创建和克隆元素</title>
		<link>http://cssrainbow.cn/tutorials/javascript/589.html</link>
		<comments>http://cssrainbow.cn/tutorials/javascript/589.html#comments</comments>
		<pubDate>Tue, 04 Aug 2009 14:57:11 +0000</pubDate>
		<dc:creator>Rainbow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript-DOM]]></category>

		<guid isPermaLink="false">http://cssrainbow.cn/?p=589</guid>
		<description><![CDATA[W3C DOM允许你创建自己的元素和文本节点，并把它们添加大文档树中。理论上上说，你可以从树中移除所有的元素，创建新元素，或者重整文档树，从而完全改变页面。但是在实践中，这个特性用起来有些限制。 

DOM也允许你克隆现有的元素，已便你能方便地复制文档的某一部分并在文档中分发副本。]]></description>
			<content:encoded><![CDATA[<h2>JavaScript DOM 创建和克隆元素</h2>
<p>
W3C DOM允许你创建自己的元素和文本节点，并把它们添加大文档树中。理论上上说，你可以从树中移除所有的元素，创建新元素，或者重整文档树，从而完全改变页面。但是在实践中，这个特性用起来有些限制。
</p>
<p>DOM也允许你克隆现有的元素，已便你能方便地复制文档的某一部分并在文档中分发副本。</p>
<p>
在我们学习之前，我还是把总结好的DOM知识体系表放在前面以方便我们查阅。
</p>
<h2>Node接口定义的节点类型都包含的特性和方法</h2>
<p><strong>特性和方法后面的 “冒号:” 紧跟的单词是“返回值类型</strong>”</p>
<div><!--Node#start--></p>
<dt><strong>Node</strong></dt>
<dd>
    <strong style="color:#696">属性</strong></p>
<dl>
<dt>遍历节点（短途旅行）:</dt>
<dd>parentNode : Node </dd>
<dd>firstChild : Node</dd>
<dd>lastChild : Node </dd>
<dd>nextSibling : Node </dd>
<dd>previousSibling : Node</dd>
<dd>childNodes : NodeList</dd>
<dt>节点信息:</dt>
<dd> nodeName :String  </dd>
<dd> nodeType :number </dd>
<dd> nodeValue :String</dd>
<dt>返回一个节点的根元素（文档对象）:</dt>
<dd> ownerDocument : Document </dd>
<dt>包含了代表一个元素的特性的Attr对象；仅用于Element节点：</dt>
<dd> attributes : NamedNodeMap</dd>
<p><span id="more-589"></span></p>
<dt>获取对象层次中的父对象：</dt>
<dd> parentElement [IE] :Node </dd>
</dl>
</dd>
<dd>
    <strong style="color:#696">方法</strong></p>
<dl>
<dt>修改文档树:</dt>
<dd>  appendChild(Node newChild)   : Node</dd>
<dd> insertBefore(Node newChild, Node refChild) : Node</dd>
<dd> removeChild(Node oldChild): Node</dd>
<dd> replaceChild(Node newChild, Node refChild) : Node</dd>
<dt>克隆一个节点：</dt>
<dd> cloneNode(boolean deep) : Node</dd>
<dt>删除一个子节点: </dt>
<dd> removeNode(boolean removeChildren) : Node     </dd>
<dt>判断childNodes包含是否包含节点：</dt>
<dd> hasChildNodes() : boolean</dd>
</dl>
</dd>
<p> <!--Node#end-->  </p>
<dt><strong>Node</strong></dt>
<p>    <!--Document#start--></p>
<dd>
<dl>
<dt><strong>Document</strong></dt>
<dd>
                <strong style="color:#696">属性</strong></p>
<dl>
<dt>自己的：</dt>
<dd>documentElement : Element </dd>
<dt> 继承 Node : </dt>
<dd>attributes, childNodes, firstChild, lastChild, nextSibling, nodeName, nodeType, nodeValue, ownerDocument, parentElement, parentNode, previousSibling </dd>
</dl>
</dd>
<dd>
               <strong style="color:#696">方法</strong></p>
<dl>
<dt>自己的：</dt>
<dd>
<dl>
<dt>创建元素：</dt>
<dd> createElement(String tagName)  : Element</dd>
<dd> createTextNode(String data) : Text</dd>
<dt>查找元素：</dt>
<dd>getElementById(String elementId)  : Element</dd>
<dd> getElementsByTagName(String tagname)  : NodeList </dd>
</dl>
</dd>
<dt>继承 Node : </dt>
<dd>appendChild, cloneNode, hasChildNodes, insertBefore, removeChild, removeNode, replaceChild </dd>
</dl>
</dd>
</dl>
</dd>
<p>    <!--Document # end --></p>
<p>    <!--Element#start--></p>
<dd>
<dl>
<dt><strong>Element</strong></dt>
<dd>
                <strong style="color:#696">属性</strong></p>
<dl>
<dt> 自己的： </dt>
<dd>tagName:  String</dd>
<dt> 继承 Node : </dt>
<dd>
attributes, childNodes, firstChild, lastChild, nextSibling, nodeName, nodeType, nodeValue, ownerDocument, parentElement, parentNode, previousSibling</dd>
</dl>
</dd>
<dd>
               <strong style="color:#696">方法</strong></p>
<dl>
<dt>自己的：</dt>
<dd>
<dl>
<dt>属性的读写：</dt>
<dd>getAttribute(String name) :   String  </dd>
<dd>setAttribute(String name, String value) :  void</dd>
</dl>
</dd>
<dd>
<dl>
<dt>其它：</dt>
<dd>getElementsByTagName(String name) Stub : NodeList</dd>
<dd>normalize() Stub :  void</dd>
<dd>removeAttribute(String name) :  void </dd>
</dl>
</dd>
<dt>继承 Node : </dt>
<dd> appendChild, cloneNode, hasChildNodes, insertBefore, removeChild, removeNode, replaceChild </dd>
</dl>
</dd>
</dl>
</dd>
<p>   <!--Element # end -->
</dl>
</div>
<h2>createElement()和createTextNode()</h2>
<p>createElement()和createTextNode()做的事情正如它们的名字所说的那样。<br />
我在 第<a title="Permanent Link to 七(3) . 最常见的JavaScript DOM方法实战-修改文档树" href="http://cssrainbow.cn/index.php/javascript/579.html" rel="bookmark">七(3) .   最常见的JavaScript DOM方法实战-修改文档树</a>中已经用过它们。</p>
<p>当时的目的是把新创建的元素添加到文档树里，使它成为某个元素的最后一个子节点。</p>

<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;"> <span style="color: #006600; font-style: italic;">//创建一个li新元素</span>
 <span style="color: #003366; font-weight: bold;">var</span> newChild<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #006600; font-style: italic;">//创建一个a 新元素</span>
 <span style="color: #003366; font-weight: bold;">var</span> newLink<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>
 <span style="color: #006600; font-style: italic;">//创建一个 Text 节点</span>
 <span style="color: #003366; font-weight: bold;">var</span> newText<span style="color: #339933;">=</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'My Wiki'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>newChild指向新创建的&lt;li&gt;元素对象，newLink指向新创建的&lt;a&gt;元素对象,而newText指向新创建的文本节点对象。<br />
这些节点都还没有被插入文档中。正如我们上章<a title="Permanent Link to 七(3) . 最常见的JavaScript DOM方法实战-修改文档树" href="http://cssrainbow.cn/index.php/javascript/579.html" rel="bookmark"> 七(3) .   最常见的JavaScript DOM方法实战-修改文档树</a><br />
中，用到的appendChild()或insertBefore()将它们附加到文档树中。</p>
<p>例如：</p>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> nav<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;nav&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//创建一个li新元素</span>
  <span style="color: #003366; font-weight: bold;">var</span> newChild<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//创建一个a 新元素</span>
  <span style="color: #003366; font-weight: bold;">var</span> newLink<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #006600; font-style: italic;">//创建一个 Text 节点</span>
  <span style="color: #003366; font-weight: bold;">var</span> newText<span style="color: #339933;">=</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'My Wiki'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//把Text添加到a元素节点中</span>
  newLink.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//给a元素节点设置属性href和内容</span>
  newLink.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//把a元素节点添加到新的li元素节点中</span>
  newChild.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newLink<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//把新的li元素节点添加到 ul 元素节点里</span>
  nav.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newChild<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>这先将文本节点附加到&lt;a&gt;中，然后再将包含文本节点的&lt;a&gt;附加到&lt;li&gt;中，最后把包含&lt;a&gt;和文本的&lt;li&gt;附加到&lt;ul&gt;中。此时我的导航条ul中多了一个 li 子节点。</p>
<h2>createTextNode()和HTML实体</h2>
<p><strong>createTextNode()有一个问题：</strong></p>
<p> 它不能创建类似于&amp;euro;（&euro; 欧元符号）&amp;yen;（&yen; 人民币符号） &copy; （&amp;copy; 版权符号）&amp;#8220;（&#8220;左双引号）&amp;#8221;（&#8221; 右双引号）等，这样的HTML实体元素。  </p>
<p>它会按字面创建文本，而不是创建你所需要的符号 。 </p>

<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;"><span style="color: #339933;">&lt;</span>script  type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
window.<span style="color: #000066;">onload</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">=</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;copy; Copyrights reserved&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><img src="http://cssrainbow.cn/Project/JavaScript-DOM/img/createTextNode.jpg"  alt="createTextNode"/>&nbsp;</p>
<p>不过，我们可以使用innerHTML来代替：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script  type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
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>	
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&amp;copy; Copyrights reserved&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><img src="http://cssrainbow.cn/Project/JavaScript-DOM/img/createTextNode or innerHTML.jpg" width="314" height="211" alt="createTextNode or innerHTML"/></p>
<p>关于innerHTML属性的用法，我们会在下一节中作为专题来具体的讨论。</p>
<h2>cloneNode()</h2>
<p>cloneNode()方法克隆一个节点，即它能对节点做一个完美的复制，使你可以在随后将其插入到文档树中</p>
<p>我还是以我的导航条为例说明：</p>
<p>我的导航条 HTML代码</p>

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;menu&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>我的导航条<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>          
    <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;nav&quot;</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>HOME<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>(X)Html / Css<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>Ajax / RIA<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>GoF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>JavaScript<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>JavaWeb<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>jQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>MooTools<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>Python<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
       <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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>Resources<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>测试cloneNode()</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script  type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
window.<span style="color: #000066;">onload</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> nav_list<span style="color: #339933;">=</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	 <span style="color: #003366; font-weight: bold;">var</span> nav<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;nav&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     nav_list<span style="color: #339933;">=</span>nav.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">=</span>nav_list<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	 <span style="color: #003366; font-weight: bold;">var</span> y<span style="color: #339933;">=</span>x.<span style="color: #660066;">cloneNode</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 nav.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><img src="http://cssrainbow.cn/Project/JavaScript-DOM/img/cloneNode.jpg" width="445" height="327" /></p>
<p>要想正确的使用cloneNode(),你必须了解它的一下二个特征：</p>
<p>1. cloneNode()接受一个可选值为true或false的参数。True 表示<strong>克隆元素和它的所有子节点</strong>。False表示<strong>克隆元素但不包含它的子节点</strong>。通常，我们在实践中用true,我从来没有遇到过想要克隆一个节点但不包含它的子节点的情形。
</p>
<p>2. cloneNode()不会克隆事件处理程序。这相当的让人恼火，不知道这个方法是怎么定义的（原因我也不知道）所以每次你克隆一个节点，你不得不在克隆上重新定义事件处理程序。 </p>
<h3  class="related_post_title">相关日志 »</h3><ul class="related_post"><li>2009/08/02 -- <a href="http://cssrainbow.cn/tutorials/javascript/579.html" title="七(3) . 最常见的JavaScript DOM方法实战-修改文档树">七(3) . 最常见的JavaScript DOM方法实战-修改文档树</a> (0)</li><li>2009/08/01 -- <a href="http://cssrainbow.cn/tutorials/javascript/565.html" title="七(2) . 最常见的JavaScript DOM方法实战-文档遍历">七(2) . 最常见的JavaScript DOM方法实战-文档遍历</a> (0)</li><li>2009/07/29 -- <a href="http://cssrainbow.cn/tutorials/javascript/555.html" title="最常见的JavaScript DOM方法一览表">最常见的JavaScript DOM方法一览表</a> (0)</li><li>2009/07/26 -- <a href="http://cssrainbow.cn/tutorials/javascript/536.html" title="七(1).DOM的核心对象:Node 分析">七(1).DOM的核心对象:Node 分析</a> (0)</li><li>2009/07/20 -- <a href="http://cssrainbow.cn/tutorials/javascript/486.html" title="六（2）.自己动手生产一个getElementsByClass()函数">六（2）.自己动手生产一个getElementsByClass()函数</a> (0)</li><li>2009/07/20 -- <a href="http://cssrainbow.cn/tutorials/javascript/480.html" title="JavaScript DOM Tutorials 文章列表(更新中&#8230;)">JavaScript DOM Tutorials 文章列表(更新中&#8230;)</a> (1)</li><li>2009/07/19 -- <a href="http://cssrainbow.cn/tutorials/javascript/473.html" title="六. W3C DOM之旅的第一步,文档访问">六. W3C DOM之旅的第一步,文档访问</a> (0)</li><li>2009/07/18 -- <a href="http://cssrainbow.cn/tutorials/javascript/470.html" title="五.W3C DOM之旅的前奏曲，回顾前四章">五.W3C DOM之旅的前奏曲，回顾前四章</a> (0)</li><li>2009/07/16 -- <a href="http://cssrainbow.cn/tutorials/javascript/456.html" title="四.(2)W3C DOM 活动列表">四.(2)W3C DOM 活动列表</a> (0)</li><li>2009/07/16 -- <a href="http://cssrainbow.cn/tutorials/javascript/454.html" title="四. (1)详解“0级DOM==遗留DOM”—回顾Document对象的属性和方法">四. (1)详解“0级DOM==遗留DOM”—回顾Document对象的属性和方法</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cssrainbow.cn/tutorials/javascript/589.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
