<?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>NeoEase &#187; Application</title>
	<atom:link href="http://www.neoease.com/category/application/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.neoease.com</link>
	<description>MG12&#039;s Blog - Just Another WordPress Blog</description>
	<lastBuildDate>Sun, 08 Jan 2012 03:00:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CSS z-index 属性的使用方法和层级树的概念</title>
		<link>http://www.neoease.com/css-z-index-property-and-layering-tree/</link>
		<comments>http://www.neoease.com/css-z-index-property-and-layering-tree/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 06:39:29 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Overlay]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[z-index]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=2218</guid>
		<description><![CDATA[CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面, 这是我们对 z-index 属性普遍的认识. 与此同时, 我们总是对堆叠顺序捉摸不透, 将 z-index 的值设得很大也未必能将节点显示在最前面. 本文将通过一些例子对 z-index 的使用方法进行分析, 并且为各位带入 z-index 层级树的概念. 这个星期我们团队做了一次内部的技术分享, 南瓜小米粥为我们分享了他对 CSS z-index 的理解和引入层级树这个概念. 这个分享的现场效果很好, 所以我也将 z-index 和层级树话题搬到博客来谈一谈. 本文谈及多个影响节点显示层级的规则, 其中用到的所有例子全部罗列在《position 属性和 z-index 属性对页面节点层级影响的例子》中. 目录 顺序规则 定位规则 参与规则 默认值规则 从父规则 层级树规则 参与规则 2 总结 顺序规则 如果不对节点设定 position 属性, 位于文档流后面的节点会遮盖前面的节点. &#60;div id=&#34;a&#34;&#62;A&#60;/div&#62; &#60;div id=&#34;b&#34;&#62;B&#60;/div&#62; 定位规则 如果将 position [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
<strong><a href="http://www.neoease.com/category/application/css-application/">CSS</a></strong> 中的 <strong>z-index 属性</strong>用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面, 这是我们对 z-index 属性普遍的认识. 与此同时, 我们总是对堆叠顺序捉摸不透, 将 z-index 的值设得很大也未必能将节点显示在最前面. 本文将通过一些例子对 z-index 的使用方法进行分析, 并且为各位带入 z-index <strong>层级树</strong>的概念.
</p>
<p><span id="more-2218"></span></p>
<p>
这个星期我们团队做了一次内部的技术分享, <a href="http://www.etosun.com/" rel="nofollow external">南瓜小米粥</a>为我们分享了他对 CSS z-index 的理解和引入层级树这个概念. 这个分享的现场效果很好, 所以我也将 z-index 和层级树话题搬到博客来谈一谈.
</p>
<p>
本文谈及多个影响节点显示层级的规则, 其中用到的所有例子全部罗列在《<a href="http://www.neoease.com/tutorials/z-index/" rel="external">position 属性和 z-index 属性对页面节点层级影响的例子</a>》中.
</p>
<h3>目录</h3>
<ul>
<li><a href="#sequence-rule">顺序规则</a></li>
<li><a href="#position-rule">定位规则</a></li>
<li><a href="#join-rule">参与规则</a></li>
<li><a href="#default-value-rule">默认值规则</a></li>
<li><a href="#parent-rule">从父规则</a></li>
<li><a href="#layering-tree-rule">层级树规则</a></li>
<li><a href="#join-rule-2">参与规则 2</a></li>
<li><a href="#article-summary">总结</a></li>
</ul>
<h3 id="sequence-rule">顺序规则</h3>
<p>
如果不对节点设定 position 属性, 位于文档流后面的节点会遮盖前面的节点.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span>&gt;</span>A<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;b&quot;</span>&gt;</span>B<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368342355_o.gif" alt="CSS z-index 属性顺序规则的例子" />
</p>
<h3 id="position-rule">定位规则</h3>
<p>
如果将 position 设为 static, 位于文档流后面的节点依然会遮盖前面的节点浮动, 所以 <code>position:static</code> 不会影响节点的遮盖关系.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:static;&quot;</span>&gt;</span>A<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;b&quot;</span>&gt;</span>B<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368342355_o.gif" alt="CSS z-index 属性定位规则的例子, static" />
</p>
<p>
如果将 position 设为 relative (相对定位), absolute (绝对定位) 或者 fixed (固定定位), 这样的节点会覆盖没有设置 position 属性或者属性值为 static 的节点, 说明前者比后者的默认层级高.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&quot;</span>&gt;</span>A<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;b&quot;</span>&gt;</span>B<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368341627_o.gif" alt="CSS z-index 属性定位规则的例子, relative | absolute | fixed" />
</p>
<p>
在没有 z-index 属性干扰的情况下, 根据这顺序规则和定位规则, 我们可以做出更加复杂的结构. 这里我们对 A 和 B 都不设定 position, 但对 A 的子节点 A-1 设定 <code>position:relative</code>. 根据顺序规则, B 会覆盖 A, 又根据定位规则 A' 会覆盖 B.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&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;a-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&quot;</span>&gt;</span>A-1<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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;b&quot;</span>&gt;</span>B<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368340973_o.gif" alt="CSS z-index 属性互相覆盖的例子" />
</p>
<p>
上面互相覆盖在什么时候用到这样的实现? 看起来偏门, 其实很常用, 比如说, 电子商务网站侧栏的类目展示列表就可以用这个技巧来实现.
</p>
<p>
下图是某网站的类目展示区域, 二级类目的悬浮层覆盖一级类目列表外框, 而一级类目的节点覆盖二级类目的悬浮层. 如果使用 CSS 实现展示效果, 一级类目的外框相当于上面例子中的 A, 一级类目的节点相当于 A-1, 二级类目的悬浮层相当于 B.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322370030406_o.jpg" alt="电子商务网站侧栏的类目展示列表" />
</p>
<h3 id="join-rule">参与规则</h3>
<p>
我们尝试不用 position 属性, 但为节点加上 z-index 属性. 发现 z-index 对节点没起作用.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;z-index:2;&quot;</span>&gt;</span>A<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;b&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;z-index:1;&quot;</span>&gt;</span>B<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;c&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;z-index:0;&quot;</span>&gt;</span>C<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368340298_o.gif" alt="CSS z-index 属性参与规则的例子, 没有明确定位的时候" />
</p>
<p>
W3C 对 z-index 属性的描述中提到 在 z-index 属性仅在节点的 position 属性为 relative, absolute 或者 fixed 时生效.
</p>
<blockquote><p>
The z-index property specifies the stack order of an element. Only works on positioned elements(position: absolute;, position: relative; or position: fixed;).
</p></blockquote>

<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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;z-index:2;&quot;</span>&gt;</span>A<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;b&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>B<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;c&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&quot;</span>&gt;</span>C<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368339626_o.gif" alt="CSS z-index 属性参与规则的例子, 明确定位的节点才能使用 z-index 属性" />
</p>
<h3 id="default-value-rule">默认值规则</h3>
<p>
如果所有节点都定义了 position:relative. z-index 为 0 的节点与没有定义 z-index 在同一层级内没有高低之分; 但 z-index 大于等于 1 的节点会遮盖没有定义 z-index 的节点; z-index 的值为负数的节点将被没有定义 z-index 的节点覆盖.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>A<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;b&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&quot;</span>&gt;</span>B<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;c&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&quot;</span>&gt;</span>C<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;d&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&quot;</span>&gt;</span>D<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368338899_o.gif" alt="CSS z-index 属性默认值规则的例子" />
</p>
<p>
通过检查我们还发现, 当 position 设为 relative, absolute 或者 fixed, 而没有设置 z-index 时, IE8 以上和 W3C 浏览器 (下文我们统称为 W3C 浏览器) 的 z-index 默认值是 auto, 但 IE6 和 IE7 是 0.
</p>
<h3 id="parent-rule">从父规则</h3>
<p>
如果 A, B 节点都定义了 position:relative, A 节点的 z-index 比 B 节点大, 那么 A 的子节点必定覆盖在 B 的子节点前面.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&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;a-1&quot;</span>&gt;</span>A-1<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;b&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&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;b-1&quot;</span>&gt;</span>B-1<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></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368338186_o.gif" alt="CSS z-index 属性从父规则的例子, 子节点不设定层级" />
</p>
<p>
如果所有节点都定义了 position:relative, A 节点的 z-index 和 B 节点一样大, 但因为顺序规则, B 节点覆盖在 A 节点前面. 就算 A 的子节点 z-index 值比 B 的子节点大, B 的子节点还是会覆盖在 A 的子节点前面.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&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;a-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:2;&quot;</span>&gt;</span>A-1<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;b&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&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;b-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>B-1<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></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368337407_o.gif" alt="CSS z-index 属性从父规则的例子, 不可逾越的层级" />
</p>
<p>
很多人将 z-index 设得很大, 9999 什么的都出来了, 如果不考虑父节点的影响, 设得再大也没用, 那是无法逾越的层级.
</p>
<h3 id="layering-tree-rule">层级树规则</h3>
<p>
可能你会觉得在 DOM 结构中的兄弟节点会拎出来进行比较并确定层级, 其实不然.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:2;&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;a-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&quot;</span>&gt;</span>A-1<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;b&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;b-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>B-1<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></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322368338186_o.gif" alt="CSS z-index 属性层级树规则的例子" />
</p>
<p>
我们认为同时将 position 设为 relative, absolute 或者 fixed, 并且 z-index 经过整数赋值的节点, 会被放置到一个与 DOM 不一样的层级树里面, 并且在层级树中通过对比 z-index 决定显示的层级. 上面的例子如果用层级树来表示的话, 应该如下图所示.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322372145036_o.gif" alt="CSS z-index 的层级树" />
</p>
<p>
图中虽然 A-1 (<code>z-index:0</code>) 的值比 B-1 (<code>z-index:1</code>) 小, 但因为在层级树里 A (<code>z-index:2</code>) 和 B-1 在一个层级, 而 A 的值比 B-1 大, 根据从父规则, A-1 显示在 B-1 前面.
</p>
<h3 id="join-rule-2">参与规则 2</h3>
<p>
前面提到的参与规则认为只要节点的 position 属性为 relative, absolute 或者 fixed, 即可参与层级比较, 其实不准确. 如果所有节点都定义了 position:relative, 并且将 z-index 设为整数值, 根据从父规则, 父节点的层级决定了子节点所在层级.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&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;a-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:100;&quot;</span>&gt;</span>A-1<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;b&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;b-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&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;b-1-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:10;&quot;</span>&gt;</span>B-1-1<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;c&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:0;&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;c-1&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;c-1-1&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;c-1-1-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>C-1-1-1<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></pre></div></div>

<p>
例子中 A, B-1, C-1-1 作为父节点, z-index 的值相同, 根据顺序规则, C-1-1 在 B-1 之前, B-1 在 A 之前; 又根据从父规则, 无论子节点的 z-index 值是什么, C-1-1-1 在 B-1-1 之前, B-1-1 在 A-1 之前.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322374028068_o.gif" alt="CSS z-index 属性参与规则 2 的例子, 所有节点参与层级比较" />
</p>
<p>
如果我们将所有父节点的 z-index 属性去除, 诡异的事情发生了. IE6 和 IE7 浏览器显示效果不变, 而 W3C 浏览器的子节点不再从父, 而是根据自身的 z-index 确定层级.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&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;a-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:100;&quot;</span>&gt;</span>A-1<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;b&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;b-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&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;b-1-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:10;&quot;</span>&gt;</span>B-1-1<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;c&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&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;c-1&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;c-1-1&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;c-1-1-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>C-1-1-1<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></pre></div></div>

<p>
根据默认值规则, IE6 / IE7 和 W3C 浏览器上的元素存在 z-index 默认值的区别. 我们相信, 仅当 position 设为 relative, absolute 或者 fixed, 并且 z-index 赋整数值时, 节点被放置到层级树; 而 z-index 为默认值时, 只在 document 兄弟节点间比较层级. 在 W3C 浏览器中, A, B-1 和 C-1-1 的 z-index 均为 auto, 不参与层级比较.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322374029705_o.gif" alt="CSS z-index 属性参与规则 2 的例子, z-index 为 auto 的节点不参与层级比较" />
</p>
<p>
而在 IE6 和 IE7 中, 因为 z-index 的默认值是 0, 所以也参与了层级比较.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322374028936_o.gif" alt="CSS z-index 属性参与规则 2 的例子, IE6 和 IE7 中 z-index 默认为 0" />
</p>
<p>
设置了 position 而没有 z-index 的节点虽然不参与层级树的比较, 但还会在 DOM 中与兄弟节点进行层级比较.
</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;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&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;a-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:100;&quot;</span>&gt;</span>A-1<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;b&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;b-1&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;b-1-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:10;&quot;</span>&gt;</span>B-1-1<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;c&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;&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;c-1&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;c-1-1&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;c-1-1-1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:relative;z-index:1;&quot;</span>&gt;</span>C-1-1-1<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></pre></div></div>

<p>
我们对上个例子改造一下, 将 B-1 的 position 属性删除后, W3C 浏览器显示如下图. 根据定位规则,  A 和 C-1-1 会显示在 B-1 的前面; 而根据顺序规则, C-1-1 又显示在 A 前面.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322374905929_o.gif" alt="CSS z-index 属性参与规则 2 的例子, position 为 auto 的节点不参与层级树比较, 但仍参与 DOM 兄弟节点间的层级比较, W3C 浏览器" />
</p>
<p>
在 IE6 和 IE7 中, 因为 A 和 C-1-1 设置了 <code>position:relative</code>, 而且 z-index 的默认值为 0, 所以也参与层级树比较, 所以有如下效果.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1149/1127/187252t1322374905138_o.gif" alt="CSS z-index 属性参与规则 2 的例子, position 为 auto 的节点不参与层级树比较, 但仍参与 DOM 兄弟节点间的层级比较, IE6 和 IE7" />
</p>
<h3 id="article-summary">总结</h3>
<p>
浏览器节点显示层级是一个费力的活, 今天你觉得 A 区块会永远置顶, 但明天因为需求变动, 突然出现 B 元素需要置顶. 一层一层往上堆砌, 某天回头一看, 发现很多区块交错在一起, 而且他们的值一个比一个大, 根本搞不清头绪. 我觉得在操刀干活之前, 最好先将 position, z-index 和层级的关系搞搞清楚, 以免后患无穷.
</p>
<p>
另外, 非情非得已, 切勿用 JavaScript 计算 z-index, 并将某个节点的 z-index 设置成所有节点中层级最高.
</p>
<p>
因为篇幅太长, 本文仅从节点属性角度进行讨论, 没有涉及 select 和 iframe 等特殊页面节点考虑, 如果有机会下次再为大家分享.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=2218" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/css-menu-list-design/" rel="bookmark">CSS 菜单列表设计</a><!-- (8.7)--></li>
		<li><a href="http://www.neoease.com/css-design-out-of-the-box/" rel="bookmark">CSS: 突破框框的设计</a><!-- (5.7)--></li>
		<li><a href="http://www.neoease.com/css-background-position/" rel="bookmark">CSS 中背景图片定位</a><!-- (4.2)--></li>
		<li><a href="http://www.neoease.com/w3c-css-background-position-value/" rel="bookmark">CSS 属性 background-position 的 W3C 定义</a><!-- (4)--></li>
		<li><a href="http://www.neoease.com/merge-icons-into-single-one/" rel="bookmark">为什么将多个图标放在一个图片里?</a><!-- (3.3)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/css-z-index-property-and-layering-tree/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>通过 IP 区分不同国家的用户</title>
		<link>http://www.neoease.com/get-user-country-by-ip-address/</link>
		<comments>http://www.neoease.com/get-user-country-by-ip-address/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 11:55:58 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WP Trick]]></category>
		<category><![CDATA[Geo IP]]></category>
		<category><![CDATA[IP]]></category>
		<category><![CDATA[Location]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=2163</guid>
		<description><![CDATA[为什么要在网站上通过 IP 区分来自不同国家用户? 这是为了让不同的国度和习惯的访客看到不一样的信息, 避免误导用户产生多余的交互行为或者因为页面的不合理而感到不爽. 上图是最近我在顶部放置的广告 banner. 上方红色的是某网站圣诞节促销活动的 Affiliate 广告, 中国大陆之外地区的访客可以看到; 下方蓝色的是最新发布的与我同行 Apply App 应用广告, 只有中国大陆地区的访客可以看到. 区分访客国家有什么用? 这里是几个我利用该功能的例子. 区分网站功能 这个博客有翻译文章的功能, 这是为了方便海外访客阅读文章, 但对中国人显得十分多余. 所以我通过 IP 判断国家, 对中国大陆地区屏蔽翻译功能. 区分展示广告 我在网站侧边栏放有广告, 很多中文广告对海外流量来说毫无意义, 所以我进行区分展示. 中国大陆地区在侧边栏最下方看到的是拿福能的广告, 而其他地区看到的是 Google 的广告. hostucan 是我的一个广告主, 有英文网站, 也有中文网站, 所以我可以向他提供区分展示服务, 免得浪费流量. 屏蔽布点服务 海外有很多好的服务平台, 在网站上布点即可采集数据和分享文章. 但很不幸, 因为某些原因, 他们在国内展示效果并不好, 不但没有起到应有效果, 还让页面加载时间变长. 可以对大陆访客屏蔽这些布点. 在 PHP 通过 IP 区分国家 如何用 [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
为什么要<strong>在网站上通过 IP 区分来自不同国家用</strong>户? 这是为了让不同的国度和习惯的访客看到不一样的信息, 避免误导用户产生多余的交互行为或者因为页面的不合理而感到不爽.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1145/1104/187252t1320417588878_o.jpg" alt="通过 IP 判断用户所在地区, 区分显示广告" />
</p>
<p>
上图是最近我在顶部放置的广告 banner. 上方红色的是某网站<a href="http://www.anrdoezrs.net/click-5524845-10577840" rel="nofollow external">圣诞节促销活动</a>的 Affiliate 广告, 中国大陆之外地区的访客可以看到; 下方蓝色的是最新发布的<a href="http://15tongxing.com/d" rel="nofollow external">与我同行 Apply App 应用</a>广告, 只有中国大陆地区的访客可以看到.
</p>
<p><span id="more-2163"></span></p>
<h3>区分访客国家有什么用?</h3>
<p>
这里是几个我利用该功能的例子.
</p>
<p>
<strong>区分网站功能</strong><br />
这个博客有翻译文章的功能, 这是为了方便海外访客阅读文章, 但对中国人显得十分多余. 所以我通过 IP 判断国家, 对中国大陆地区屏蔽翻译功能.
</p>
<p>
<strong>区分展示广告</strong><br />
我在网站侧边栏放有广告, 很多中文广告对海外流量来说毫无意义, 所以我进行区分展示. 中国大陆地区在侧边栏最下方看到的是<a href="http://www.neoease.com/nuffnang-challenge-1000/" rel="nofollow">拿福能</a>的广告, 而其他地区看到的是 Google 的广告. <a href="http://www.hostucan.cn/us/group/wordpress-hosting" rel="nofollow">hostucan</a> 是我的一个广告主, 有英文网站, 也有中文网站, 所以我可以向他提供区分展示服务, 免得浪费流量.
</p>
<p>
<strong>屏蔽布点服务</strong><br />
海外有很多好的服务平台, 在网站上布点即可采集数据和分享文章. 但很不幸, 因为某些原因, 他们在国内展示效果并不好, 不但没有起到应有效果, 还让页面加载时间变长. 可以对大陆访客屏蔽这些布点.
</p>
<h3>在 PHP 通过 IP 区分国家</h3>
<p>
如何用 PHP 通过 IP 区分国家和地区呢? <a href="http://www.maxmind.com/">Maxmind.com</a> 提供一套 GeoIP 的解决方案, 只需要简单几步即可在 PHP 中通过 IP 判断访客的国家.
</p>
<p>
<strong>1. 下载数据库和 PHP 库文件</strong>
</p>
<ul>
<li>下载 <a href="http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz">GeoID.dat.gz</a>, 解压为 GeoIP.dat 文件.</li>
<li>下载 <a href="http://www.maxmind.com/download/geoip/api/php/geoip.inc">geoip.inc</a>.</li>
</ul>
<p>
<strong>2. 通过 PHP 代码获取国家信息</strong><br />
以下是一段示范代码, 演示如何获取国家代号和国家名称.
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 引入 PHP 库文件</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;geoip.inc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 打开本地数据库, 数据保存在 GeoIP 文件中.</span>
<span style="color: #000088;">$geoData</span> <span style="color: #339933;">=</span> geoip_open<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GeoIP.dat'</span><span style="color: #339933;">,</span> GEOIP_STANDARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 获取国家 IP</span>
<span style="color: #000088;">$countryCode</span> <span style="color: #339933;">=</span> geoip_country_code_by_addr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoData</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 获取国家名称</span>
<span style="color: #000088;">$countryName</span> <span style="color: #339933;">=</span> geoip_country_name_by_addr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoData</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 关闭本地数据库</span>
geoip_close<span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>在 WordPress 中通过 IP 区分国家</h3>
<p>
既然 PHP 上使用没问题, WordPress 肯定也是 Okay 的. 看看我是怎么使用的.
</p>
<p>
<strong>1. 放置数据库文件</strong><br />
将 GeoIP.dat 解压到 WordPress 根目录中. (你可以在这个目录找到 wp-config.php 或者 wp-config-sample.php 文件)
</p>
<p>
<strong>2. 编写调用接口</strong><br />
在主题目录中新建文件夹 include, 将 geoip.inc 放置在新建文件夹中. 并在该文件夹新建文件 geoip.php 文件内容如下.
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'geoip.inc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$countryCode</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$geoData</span> <span style="color: #339933;">=</span> geoip_open<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GeoIP.dat'</span><span style="color: #339933;">,</span> GEOIP_STANDARD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$countryCode</span> <span style="color: #339933;">=</span> geoip_country_code_by_addr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoData</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
geoip_close<span style="color: #009900;">&#40;</span><span style="color: #000088;">$geoData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>
这里只取国家代号作为判别依据. 并且国家代号是全局变量, 以避免页面多处判断需要反复访问 GeoIP.dat 获取信息, 减少程序开销.
</p>
<p>
<strong>2. 调用接口, 获取国家代号</strong><br />
3. 打开 header.php 文件, 在文件顶部加入代码如下.
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include/geoip.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>
<strong>4. 使用国家代号</strong><br />
在主题中调用代码, 例子如下.
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$countryCode</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$countryCode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'CN'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 中国大陆地区执行的代码</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$countryCode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'US'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 美国地区执行的代码</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 中国大陆和美国以外地区执行的代码</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>总结</h3>
<p>
通过 IP 判断访客来源十分精准的, 现在一些外贸网站都是通过这个方法向用户进行展示区分, 比如美国地区的用户默认看到美国能买到的商品和美国物流信息. 但不是百分之百的准确, 比如某人常年翻墙, 那他可能一直看不到本国的信息. 至于是否需要区分处理, 网站主要有所考虑.
</p>
<p>
可能有人会问, 加这么个东西, 性能如何? 会不会要求强大的服务器? 我测试过, 正常的服务器上几乎不影响页面加载性能, 可以看看这个博客的速度. 如果不放心, 自己测一下.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=2163" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/wordpress-thumb-trick/" rel="bookmark">巧用 WordPress 缩略图</a><!-- (3.9)--></li>
		<li><a href="http://www.neoease.com/update-feedsky-subscribe-number-everyday/" rel="bookmark">每日同步 Feedsky 订阅数</a><!-- (3.2)--></li>
		<li><a href="http://www.neoease.com/wp-mobile-themes/" rel="bookmark">手机主题调度插件 WordPress Mobile Themes</a><!-- (2.8)--></li>
		<li><a href="http://www.neoease.com/spliting-blogroll-to-2-columns-03/" rel="bookmark">将 Blogroll 分为两栏 (3)</a><!-- (2.8)--></li>
		<li><a href="http://www.neoease.com/wp-easyarchives/" rel="bookmark">文章存档插件 WP-EasyArchives</a><!-- (2.7)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/get-user-country-by-ip-address/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>stopPropagation, preventDefault 和 return false 的区别</title>
		<link>http://www.neoease.com/stoppropagation-and-preventdefault-and-return-false/</link>
		<comments>http://www.neoease.com/stoppropagation-and-preventdefault-and-return-false/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 01:49:18 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Difference]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[Trick]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=2126</guid>
		<description><![CDATA[因为有父, 子节点同在, 因为有监听事件和浏览器默认动作之分. 使用 JavaScript 时为了达到预期效果经常需要阻止事件和动作执行. 一般我们会用到三种方法, 分别是 stopPropagation(), preventDefault() 和 return false. 它们之间有什么区别, 该何时使用呢? 将在本文中进行讲解. 术语 监听事件, 在在节点上能被监听的页面操作. 如: select 节点的 change 事件, a 节点的 click 事件. 浏览器默认动作, 指特定页面元素上带有的功能. 如: 点击 a 链接节点的跳转动作, 表单提交动作. stopPropagation() 因为事件可以在各层级的节点中传递, 不管是冒泡还是捕获, 有时我们希望事件在特定节点执行完之后不再传递, 可以使用事件对象的 stopPropagation() 方法. 假设页面上存在一个浮动弹出层, 显示在最前面, 当点击弹出层以外页面区域时, 隐藏弹出层. 为了做到这样的效果, 我们会监听 documentElement 的 click 事件, 一旦事件被触发即隐藏弹出层. 但是... 这显然存在问题. [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
因为有父, 子节点同在, 因为有监听事件和浏览器默认动作之分. 使用 JavaScript 时为了达到预期效果经常需要阻止事件和动作执行. 一般我们会用到三种方法, 分别是 <code>stopPropagation()</code>, <code>preventDefault()</code> 和 <code>return false</code>. 它们之间有什么区别, 该何时使用呢? 将在本文中进行讲解.
</p>
<p><span id="more-2126"></span></p>
<h3>术语</h3>
<p>
<strong>监听事件</strong>, 在在节点上能被监听的页面操作. 如: select 节点的 change 事件, a 节点的 click 事件.<br />
<strong>浏览器默认动作</strong>, 指特定页面元素上带有的功能. 如: 点击 a 链接节点的跳转动作, 表单提交动作.
</p>
<h3>stopPropagation()</h3>
<p>
因为事件可以在各层级的节点中传递, 不管是冒泡还是捕获, 有时我们希望事件在特定节点执行完之后不再传递, 可以使用事件对象的 <code>stopPropagation()</code> 方法.
</p>
<p>
假设页面上存在一个浮动弹出层, 显示在最前面, 当点击弹出层以外页面区域时, 隐藏弹出层. 为了做到这样的效果, 我们会监听 <code>documentElement</code> 的 click 事件, 一旦事件被触发即隐藏弹出层. 但是...
</p>
<p>
这显然存在问题. 当用户点击弹出层时, 我们不希望它隐藏掉. 但因为事件的冒泡传递, <code>documentElement</code> 的 click 事件也会被触发. 这个时候, 我们可以监听弹出层的 click 事件, 并使用 <code>stopPropagation()</code> 方法阻止冒泡. 请参考下面的代码.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// 在弹出对话框上点击时, 不进行任何页面操作, 并阻止冒泡</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dialog'</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>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	ev.<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>
&nbsp;
<span style="color: #006600; font-style: italic;">// 在 documentElement 节点上监听到点击事件时, 隐藏对话框</span>
document.<span style="color: #660066;">documentElement</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>ev<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;">'dialog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
<code>stopPropagation()</code> 相当好用, 可是 IE8 及以前版本都不支持. IE 的事件对象包含特有的属性 <code>cancelBubble</code>, 只要将它赋值为 false 即可阻止事件继续. 如:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// 在弹出对话框上点击时, 不进行任何页面操作, 并阻止冒泡</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dialog'</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>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	ev.<span style="color: #660066;">cancelBubble</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: #339933;">;</span></pre></div></div>

<h3>preventDefault()</h3>
<p>
一个带事件监听的链接代码如下:
</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;http://w3c.org&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;alert('JavaScript Click Event');&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>
点击该链接, 显示对话框后跳转页面. 由此可知, 除了执行监听事件还会触发浏览器默认动作; 执行监听事件在前, 触发浏览器默认动作在后.
</p>
<p>
这里有个经典示例, 我们希望点击链接在新窗口打开页面, 但不希望当前页面跳转. 这个时候可以使用 <code>preventDefault()</code> 阻止后面将要执行的浏览器默认动作.
</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;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;link&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://w3c.org&quot;</span>&gt;</span>W3C 首页链接<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
// 在新窗口, 打开页面
document.getElementById('link').onclick = function(ev) {
	// 阻止浏览器默认动作 (页面跳转)
	ev.preventDefault();
	// 在新窗口打开页面
	window.open(this.href);
};
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<h3>return false</h3>
<p>
退出执行, <code>return false</code> 之后的所有触发事件和动作都不会被执行. 有时候 <code>return false</code> 可以用来替代 <code>stopPropagation()</code> 和 <code>preventDefault()</code>, 比如我们上面新窗口打开链接的例子, 如:
</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;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;link&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://w3c.org&quot;</span>&gt;</span>W3C 首页链接<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
// 在新窗口, 打开页面
document.getElementById('link').onclick = function(ev) {
	// 在新窗口打开页面
	window.open(this.href);
	// 退出执行 (在监听事件之后执行的浏览器默认动作将不会被执行)
	return false;
};
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>
有人认为 <code>return false</code> = <code>stopPropagation()</code> + <code>preventDefault()</code>, 其实是错的. <code>return false</code> 不但阻止事件, 还可以返回对象, 跳出循环等... 方便地一刀切而不够灵活, 滥用易出错.
</p>
<h3>后话</h3>
<p>
因为习惯用 <code>return false</code> 来阻止事件, 最近在搞手机 Web 的时候遇到一些问题, 很是难堪. 写下此文当作笔记, 提醒自己.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=2126" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/javascript-go-top/" rel="bookmark">用 JavaScript 实现变速回到顶部</a><!-- (12.1)--></li>
		<li><a href="http://www.neoease.com/get-cursor-position-with-javascript/" rel="bookmark">通过 JavaScript 获取页面上的鼠标位置</a><!-- (9.6)--></li>
		<li><a href="http://www.neoease.com/wordpress-at-reply/" rel="bookmark">WordPress 评论的 @ 回复</a><!-- (8.4)--></li>
		<li><a href="http://www.neoease.com/javascript-onload-function/" rel="bookmark">JavaScript 初始化装载方法</a><!-- (8.2)--></li>
		<li><a href="http://www.neoease.com/search-tip-of-google-cn-music/" rel="bookmark">谷歌音乐搜索栏的提示功能</a><!-- (7.4)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/stoppropagation-and-preventdefault-and-return-false/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>HTML5 画布 - 线条, 矩形, 圆形和文字</title>
		<link>http://www.neoease.com/html5-canvas-line-rectangle-circle-text/</link>
		<comments>http://www.neoease.com/html5-canvas-line-rectangle-circle-text/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 09:00:11 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Circle]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Line]]></category>
		<category><![CDATA[Rectangle]]></category>
		<category><![CDATA[Text]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1940</guid>
		<description><![CDATA[上节已经介绍了如何取得画布及其上下文对象, 我们将开始使用 HTML5 Canvas 的绘图 API 来进行图形绘制, 本文会提及线条, 矩形, 圆形和文本这几个基本图形. 本节主要介绍相关的图形绘制方法, 不会有难以理解的内容, 在需要注意的地方会着墨较多. 相关章节 HTML5 画布 - 参考资料, 画布元素和上下文 HTML5 画布 - 线条, 矩形, 圆形和文字 线条 玩过 VC++ MFC 的同学应该很容易理解画线的这段代码, 通过下面三句代码可以在页面上画出一条从 (40, 40) 到 (340, 40) 的直线. context.moveTo&#40;40, 40&#41;; // 将笔移动到 (40, 40) context.lineTo&#40;340, 40&#41;; // 用笔画线到 (340, 40) context.stroke&#40;&#41;; // 让线条描绘出来 路径 线条除了绘制直线, 还可以勾出路径. [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
上节已经介绍了如何取得画布及其上下文对象, 我们将开始使用 <strong>HTML5 Canvas</strong> 的绘图 API 来进行<strong>图形绘制</strong>, 本文会提及<strong>线条</strong>, <strong>矩形</strong>, <strong>圆形</strong>和<strong>文本</strong>这几个基本图形. 本节主要介绍相关的图形绘制方法, 不会有难以理解的内容, 在需要注意的地方会着墨较多.
</p>
<p><span id="more-1940"></span></p>
<h3>相关章节</h3>
<ol>
<li><a href="http://www.neoease.com/html5-canvas-book-element-context/">HTML5 画布 - 参考资料, 画布元素和上下文</a></li>
<li><strong>HTML5 画布 - 线条, 矩形, 圆形和文字</strong></li>
</ol>
<h3>线条</h3>
<p>
玩过 VC++ MFC 的同学应该很容易理解画线的这段代码, 通过下面三句代码可以在页面上画出一条从 (40, 40) 到 (340, 40) 的直线.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 将笔移动到 (40, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条描绘出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926208862_o.gif" alt="画布线条" />
</p>
<h3>路径</h3>
<p>
线条除了绘制直线, 还可以勾出路径. 路径是什么? 类似 Photoshop 的线圈工具. 在画布中可以使用 beginPath 和 closePath 两个方法搭配构建路径, 我们可以看一下以下几个例子.
</p>
<p>
<b>1. 绘制三根线条.</b>
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 将笔移动到 (40, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 100)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (40, 100)</span>
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条显示出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926209731_o.gif" alt="" />
</p>
<p>
<b>2. 绘制第一根线条前, 标记一个路径的开始.</b> 由效果图看出, 第一条线没有显示出来, 相当于 beginPath 之前的代码没有起作用.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 将笔移动到 (40, 40)</span>
context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 开始路径</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 100)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (40, 100)</span>
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条显示出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926210593_o.gif" alt="" />
</p>
<p>
<b>3. 在绘制线条之前标记路径结束.</b> 路径将进行闭合, 如效果图所示.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">beginPath</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>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 100)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (40, 100)</span>
context.<span style="color: #660066;">closePath</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>
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条显示出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926211456_o.gif" alt="" />
</p>
<p>
除了通过 stroke 方法沿路径勾画线条之外, 我们还可以使用 fill 方法对路径圈定区域进行颜色填充, 填充前将自动闭合路径.
</p>
<p>
<b>4. 填充路径圈定区域.</b>
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">beginPath</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>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 100)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (40, 100)</span>
context.<span style="color: #660066;">closePath</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>
context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条显示出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926212257_o.gif" alt="" />
</p>
<p>
<b>5. 如果路径没有闭合, 填充前将自动闭合路径.</b>
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">beginPath</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>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 100)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (40, 100)</span>
context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条显示出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926212257_o.gif" alt="" />
</p>
<p>
<b>6. 如果路径上的点在一根线条上.</b> 无法圈定区域, 将显示空白.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">beginPath</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>
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 40)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 100)</span>
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">340</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 用笔画线到 (340, 150)</span>
context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 让线条显示出来</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926213110_o.gif" alt="" />
</p>
<h3>矩形</h3>
<p>
绘画矩形有用于勾画矩形和填充矩形使用的两个方法.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 只勾画出矩形的外框</span>
context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 画出矩形并使用颜色填充矩形区域</span></pre></div></div>

<p>
其效果和关系可参考下图.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0728/187252t1311842103156_o.gif" alt="画布矩形" />
</p>
<h3>圆形</h3>
<p>
圆形与矩形不同, 没有勾画和填充的方法. 可通过 arc 圈出来的是圆形路径, 再通过前面提及的 stroke 和 fill 方法进行勾画和填充.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">arc</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> radius<span style="color: #339933;">,</span> startAngle<span style="color: #339933;">,</span> endAngle<span style="color: #339933;">,</span> anticlockwise<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
这一共有 6 个参数. x 是圆心的横坐标, y 是圆心的纵坐标, radius 是半径, startAngle 是开始画圆的角度, endAngle 是结束画圆的角度, anticlockwise 是画圆方向.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926649397_o.gif" alt="画布 arc 属性" />
</p>
<p>
startAngle 和 endAngle 的角度使用弧度计算, 整圆弧度 2π, 弧度和角度换算方式如下.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> radians <span style="color: #339933;">=</span> degrees <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">180</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// radians 是弧度, degrees 是角度</span></pre></div></div>

<p>
anticlockwise 是可选项, 默认值是 false, 使用逆时针.
</p>
<p>
这里我们画个半圆看看效果吧.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">arc</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">230</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">*</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #009966; font-style: italic;">/ 4, Math.PI * 3 /</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 从 -1/4π 到 3/4π, 以 (230, 90) 为圆心, 半径 60px 画圆.</span>
context.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311926207890_o.gif" alt="画布圆形" />
</p>
<h3>文本</h3>
<p>
除了绘制图形, 还可以往画布上插入文本, 但该文本不是真实的文字, 而是栏栅化的图形. 绘制文本有两种方法, 分别是用于描边的 strokeText 和用于填字的 fillText 方法. 这里我们往页面上写点什么吧.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello, World!&quot;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
此处 text 是文本的内容, x 是画布左边到文字左边的距离, y 是画布上边到文字下边的距离, 如下图.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311928896597_o.gif" alt="画布 fillText 方法" />
</p>
<p>
是不是觉得文字有点小? 因为在默认情况下, 使用 10px sans-serif 为文本属性. 除了修改文字大小, 我们还想换换文字的字体, 形态等外观, 这些都可以通过修改 font 属性来修改文本属性.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello, World!&quot;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;italic 60px serif&quot;</span><span style="color: #339933;">;</span>
context.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
font 属性与 CSS 中的 font 属性值可以通用.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0729/187252t1311928895700_o.gif" alt="画布文字属性" />
</p>
<h3>结束语</h3>
<p>
本文介绍了一些 HTML5 Canvas 支持的基础图形, 下节我们将会了解关于上下文属性等, <a rel="nofollow external" href="http://www.amazon.cn/mn/detailApp/ref=as_li_ss_tl?_encoding=UTF8&#038;tag=wuzhao&#038;linkCode=as2&#038;asin=1430232919&#038;camp=536&#038;creative=3132&#038;creativeASIN=1430232919">《Foundation HTML5 Canvas: For Games and Entertainment》</a>第三章中还没提及的一些内容.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1940" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/html5-canvas-book-element-context/" rel="bookmark">HTML5 画布 - 参考资料, 画布元素和上下文</a><!-- (19.7)--></li>
		<li><a href="http://www.neoease.com/google-devfest-2010-beijing/" rel="bookmark">Google DevFest 互联网技术交流会</a><!-- (4.7)--></li>
		<li><a href="http://www.neoease.com/spliting-blogroll-to-2-columns-01/" rel="bookmark">将 Blogroll 分为两栏 (1)</a><!-- (1)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/html5-canvas-line-rectangle-circle-text/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>HTML5 画布 - 参考资料, 画布元素和上下文</title>
		<link>http://www.neoease.com/html5-canvas-book-element-context/</link>
		<comments>http://www.neoease.com/html5-canvas-book-element-context/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 09:38:02 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Context]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1934</guid>
		<description><![CDATA[最近在看一本关于 HTML5 Canvas 的书, 叫《Foundation HTML5 Canvas: For Games and Entertainment》, 书名比较长, 作者说得也比较累赘, 但是章节和内容都很清晰. 后面我会写一系列文章, 关于 HTML5 画布, 与此书章节同步, 可能插放一些别的内容. 相关章节 HTML5 画布 - 参考资料, 画布元素和上下文 HTML5 画布 - 线条, 矩形, 圆形和文字 参考资料 这本书适合有 JavaScript / jQuery 基础的同学看. 现在没有中文译本, 国内可以通过亚马逊购买. 亚马逊那价格, 还需要 1-4 个月发货, 谁受得了, 所以我搞了个 PDF, 在淘宝店打印了一本, 加运费 20 块左右, 一天到货. 书中第一章介绍 HTML5, 第二章介绍 [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
最近在看一本关于 <strong>HTML5 Canvas</strong> 的书, 叫<a rel="nofollow external" href="http://www.amazon.cn/mn/detailApp/ref=as_li_ss_tl?_encoding=UTF8&#038;tag=wuzhao&#038;linkCode=as2&#038;asin=1430232919&#038;camp=536&#038;creative=3132&#038;creativeASIN=1430232919">《Foundation HTML5 Canvas: For Games and Entertainment》</a>, 书名比较长, 作者说得也比较累赘, 但是章节和内容都很清晰. 后面我会写一系列文章, 关于 <strong>HTML5 画布</strong>, 与此书章节同步, 可能插放一些别的内容.
</p>
<p><span id="more-1934"></span></p>
<h3>相关章节</h3>
<ol>
<li><strong>HTML5 画布 - 参考资料, 画布元素和上下文</strong></li>
<li><a href="http://www.neoease.com/html5-canvas-line-rectangle-circle-text/">HTML5 画布 - 线条, 矩形, 圆形和文字</a></li>
</ol>
<h3>参考资料</h3>
<p>
这本书适合有 JavaScript / jQuery 基础的同学看. 现在没有中文译本, 国内可以<a rel="nofollow external" href="http://www.amazon.cn/mn/detailApp/ref=as_li_ss_tl?_encoding=UTF8&#038;tag=wuzhao&#038;linkCode=as2&#038;asin=1430232919&#038;camp=536&#038;creative=3132&#038;creativeASIN=1430232919">通过亚马逊购买</a>. 亚马逊那价格, 还需要 1-4 个月发货, 谁受得了, 所以我搞了个 PDF, 在<a rel="nofollow external" href="http://s.click.taobao.com/t_1?i=oBCpRbc%2FiXg6wA%3D%3D&#038;p=mm_10675567_0_0&#038;n=11">淘宝店打印了一本</a>, 加运费 20 块左右, 一天到货.
</p>
<p>
书中第一章介绍 HTML5, 第二章介绍 JavaScript, 不得不吐槽一下. 凑字数的章节写了 50 多页, 占全书 1/5, 实在有点不厚道. 我们从第三章 (Learning the Basics of Canvas) 开始, 进入 HTML5 画布基础.
</p>
<h3>画布元素</h3>
<p>
目前大部分新型浏览器实现了 HTML5 Canvas 原生的绘图 API, 但只支持 2D 渲染环境. 这里我们创建一个空白的画布元素, 代码如下.
</p>

<div class="wp_syntax"><div class="code"><pre class="html5" style="font-family:monospace;">&lt;canvas width=&quot;500&quot; height=&quot;500&quot;&gt;
	这是提示信息
&lt;/canvas&gt;</pre></div></div>

<p>
画布元素默认宽 300px, 高 150px, <del datetime="2011-07-29T09:12:25+00:00">除非你通过 CSS 或者像上面的例子一样定义它的尺寸.</del> 除非你像上面的例子一样定义它的尺寸. 画布就像是一张图片一样, 如果通过 CSS 修改其大小, 画布将被拉伸至指定宽高, 会导致失真.
</p>
<p>
毫不意外, IE9 以下的 IE 浏览器不支持画布元素. 在这些浏览器上, 我们有两个选择.
</p>
<ol>
<li>当浏览器不支持画布时显示提示, 可能是一段提醒用户升级浏览器的文案. 画布的 innerHTML 就是画布的提示信息.</li>
<li>使用 <a rel="external" href="http://code.google.com/p/explorercanvas/">ExplorerCanvas 脚本</a>兼容老版本的 IE 浏览器. 不过有弊端, 脚本文件比较大 (99KB), 而且需要在 onload 执行画布相关脚本才能生效.</li>
</ol>
<h3>坐标系统</h3>
<p>
HTML5 Canvas 使用笛卡尔坐标系统, 坐标原点 (0, 0) 在屏幕左上方, x 坐标向右, y 坐标向下, 如图所示.
</p>
<p>
<img class="sided" src='http://photo.tuhigh.com/pics/1131/0728/187252t1311839141503_o.gif' alt="笛卡尔坐标系统" />
</p>
<h3>上下文对象</h3>
<p>
了解画布元素和坐标系统后, 接下来需要知道如何操作画布. 操作图形需使用画布的上下文对象, 可以通过以下方法获取上下文.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
下面是一个例子, 我们使用画布画出一个矩形.
</p>

<div class="wp_syntax"><div class="code"><pre class="html5" style="font-family:monospace;">&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;HTML5 Canvas Sample&lt;/title&gt;
&nbsp;
&lt;!--[if IE]&gt;&lt;script src=&quot;excanvas.js&quot;&gt;&lt;/script&gt;&lt;![endif]--&gt;
&lt;script&gt;
//&lt;![CDATA[
function drawRect() {
	var canvas = document.getElementById(&quot;canvas&quot;);
	var context = canvas.getContext(&quot;2d&quot;);
&nbsp;
	context.strokeRect(50, 50, 120, 80);
}
//]]&gt;
&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body onload=&quot;drawRect();&quot;&gt;
&lt;canvas id=&quot;canvas&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;
	这是反馈信息
&lt;/canvas&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>
绘图效果如下.
</p>
<p>
<img class="sided" src="http://photo.tuhigh.com/pics/1131/0728/187252t1311844872965_o.gif" alt="" />
</p>
<h3>结束语</h3>
<p>
本文算是一个入门, 介绍了一本学习材料, 并讲述如何获取画布及其上下文对象. 下一节我们会讲一些关于图形绘制的内容.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1934" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/html5-canvas-line-rectangle-circle-text/" rel="bookmark">HTML5 画布 - 线条, 矩形, 圆形和文字</a><!-- (14.5)--></li>
		<li><a href="http://www.neoease.com/google-devfest-2010-beijing/" rel="bookmark">Google DevFest 互联网技术交流会</a><!-- (5)--></li>
		<li><a href="http://www.neoease.com/javascript-namespace/" rel="bookmark">JavaScript 的命名空间</a><!-- (2.2)--></li>
		<li><a href="http://www.neoease.com/spliting-blogroll-to-2-columns-01/" rel="bookmark">将 Blogroll 分为两栏 (1)</a><!-- (1.2)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/html5-canvas-book-element-context/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>ActionScript 3 调度个性化事件</title>
		<link>http://www.neoease.com/dispatching-custom-events-by-dispatchevent-method/</link>
		<comments>http://www.neoease.com/dispatching-custom-events-by-dispatchevent-method/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 14:52:40 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Dispatch]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[Method]]></category>
		<category><![CDATA[Translation]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1725</guid>
		<description><![CDATA[第三章涵盖了基本事件的使用, 但是有一个话题没有被覆盖到 (出现在后面的章节中), 那就是使用 dispatchEvent() 方法发送一个特殊的, 设置自定义的事件. 这个方法被包含在 EventDispatcher 类之中. dispatchEvent() 方法接收一个 Event 对象作为有且仅有的参数. 以下代码将调度一个名为 "my event" 的自定义事件: dispatchEvent&#40;new Event&#40;&#34;my event&#34;&#41;&#41;; 你也可以使用 AS3 提供的任何事件常量: dispatchEvent&#40;new Event&#40;Event.INIT&#41;&#41;; 下面定义的类被实例化后一秒钟会调度一个事件: package &#123; &#160; import flash.utils.Timer; import flash.events.TimerEvent; import flash.events.EventDispatcher; import flash.events.Event; &#160; public class DispatchEventExample extends EventDispatcher &#123; &#160; private var _timer:Timer; &#160; public function DispatchEventExample&#40;&#41; &#123; _timer [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
第三章涵盖了基本事件的使用, 但是有一个话题没有被覆盖到 (出现在后面的章节中), 那就是使用 <code>dispatchEvent()</code> 方法发送一个特殊的, 设置自定义的事件. 这个方法被包含在 <code>EventDispatcher</code> 类之中.
</p>
<p><span id="more-1725"></span></p>
<p>
<code>dispatchEvent()</code> 方法接收一个 <code>Event</code> 对象作为有且仅有的参数. 以下代码将调度一个名为 "my event" 的自定义事件:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;my event&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>
你也可以使用 AS3 提供的任何事件常量:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">INIT</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>
下面定义的类被实例化后一秒钟会调度一个事件:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Timer</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TimerEvent</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">EventDispatcher</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> DispatchEventExample <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">EventDispatcher</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _timer<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Timer</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> DispatchEventExample<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_timer = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Timer</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			_timer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">TimerEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TIMER</span><span style="color: #000066; font-weight: bold;">,</span> onTimer<span style="color: #000066; font-weight: bold;">,</span>
									<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			_timer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onTimer<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">TimerEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;one second elapsed&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
可以使用 <code>addEventListener()</code> 方法来监听这个事件. 跟之前讨论过的调度事件方式类似, 你可以监听一个事件常量 (例如 <code>Event.ENTER_FRAME</code>, 或者你自定义的事件类), 或者一个简单的字符串. 这里有一个例子, 该类会监听一个名为 "one second elapsed" 的事件:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _dispatchExample<span style="color: #000066; font-weight: bold;">:</span>DispatchEventExample<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_dispatchExample = <span style="color: #0033ff; font-weight: bold;">new</span> DispatchEventExample<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			_dispatchExample<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;one second elapsed&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
									  onOneSecond<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onOneSecond<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;_dispatchExample has dispatched an event&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>关于 EventDispatcher 和 DisplayObjectContainer 注意事项</h3>
<p>
显示对象容器是 <code>EventDispatcher</code> 的子类:<br />
<code>Object -> EventDispatcher -> DisplayObject -> InteractiveObject -> DisplayObjectContainer </code><br />
这意味任何的 <code>Sprite</code>, <code>MovieClip</code> 等继承显示对象容器的对象都可以使用 <code>dispatchEvent()</code> 方法.
</p>
<h3>应用实例</h3>
<p>
我们已经添加了一个名为 <code>OpenSlider</code> 的类到我们的包集合中. 它是一个健壮易懂, 可换肤的滑动条, 效果如下:
</p>
<p><object type="application/x-shockwave-flash" data="http://www.learningactionscript3.com/wp-content/uploads/openSlider.swf" height="400" width="550"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="movie" value="http://www.learningactionscript3.com/wp-content/uploads/openSlider.swf" /><param name="wmode" value="window" /><param name="quality" value="high" /><param name="bgcolor" value="#FFFFFF" /><param name="menu" value="true" /><param name="base" value="http://www.learningactionscript3.com/" /><param name="SeamlessTabbing" value="false" /></object></p>
<p>
<a rel="nofollow external" href="http://www.learningactionscript3.com/download/17/">下载 OpenSlider</a> (35.1 KB, 2,042 hits)
</p>
<p>
<code>OpenSlider</code> 类只需舞台上的两个显示对象作为初始化参数, 剩余部分会在幕后处理:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> scribbleSlider<span style="color: #000066; font-weight: bold;">:</span>OpenSlider = <span style="color: #0033ff; font-weight: bold;">new</span> OpenSlider<span style="color: #000000;">&#40;</span>scribbleBtn<span style="color: #000066; font-weight: bold;">,</span> scribbleLine<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>
当滑块被拖至新的位置, <code>OpenSlider</code> 会调度一个定义为 <code>CHANGE</code> 事件. 你可以通过下面的方式监听该事件:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">scribbleSlider<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CHANGE</span><span style="color: #000066; font-weight: bold;">,</span> onSliderChange<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>
一旦接收到事件, 你可以通过 <code>percent</code> 属性的 get 方法获到新的数值.
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> onSliderChange<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span>percent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
另一种方式, 如果你希望数值不在 0-1 区间内, 你可以使用 <code>setRange()</code> 方法进行重设, 如下:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// set range</span>
scribbleSlider<span style="color: #000066; font-weight: bold;">.</span>setRange<span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// read value</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>scribbleSlider<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>
与在本文的讨论内容相符, <code>OpenSlider</code> 使用了 <code>dispatchEvent()</code> 方法. 不需要太深入地了解整个类, 这里提供部分代码以展示 <code>dispatchEvent()</code> 方法是如何被利用的:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onBtnDown<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span>onBtnUpOutside<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">startDrag</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span>_rect<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>onChange<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_LEAVE</span><span style="color: #000066; font-weight: bold;">,</span>onBtnUp<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onBtnUp<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stopDrag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>onChange<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_LEAVE</span><span style="color: #000066; font-weight: bold;">,</span>onBtnUp<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span>onBtnUpOutside<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onBtnUpOutside<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">!</span>=<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stopDrag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>onChange<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_LEAVE</span><span style="color: #000066; font-weight: bold;">,</span>onBtnUp<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span>onBtnUpOutside<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onChange<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>_scrollMode == <span style="color: #990000;">&quot;horizontal&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_percent = <span style="color: #000000;">&#40;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>_rect<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>_rect<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		_percent = <span style="color: #000000;">&#40;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>_rect<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bottom</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>_btn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>_rect<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CHANGE</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
简而言之, 当 <code>_btn</code> 对象正在被拖动时, 会触发一个定义为 <code>ENTER_FRAME</code> 的事件, 将重新计算 <code>_percent</code> 变量, 并不间断地调度 <code>CHANGE</code> 事件. 当 <code>_btn</code> 对象停止被拖动, <code>ENTER_FRAME</code> 事件将被移除. 大家可以下载上面提供的 Demo 来查看剩余的代码.
</p>
<p>
新的事件模式是 AS3 与 ActionScript 早前的版本之间的最大的区别.
</p>
<h3>译者的话</h3>
<p>
本文翻译自 <a rel="external" href="http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/">Dispatching Custom Events</a>. 原文作者编著了<a rel="nofollow external" href="http://www.amazon.cn/mn/detailApp/ref=sr_1_46?_encoding=UTF8&#038;s=books&#038;qid=1292424315&#038;asin=144939017X&#038;sr=8-46&#038;tag=wuzhao">《Learning ActionScript 3.0》</a>(<a rel="nofollow external" href="http://www.amazon.cn/mn/detailApp/ref=sr_1_28?_encoding=UTF8&#038;s=books&#038;qid=1292424351&#038;asin=B001MS8LWS&#038;sr=8-28&#038;tag=wuzhao">影印版</a>) , 他的书和博客对初学者来说都是相当不错的学习材料.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1725" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/actionscript-keywords-get-and-set/" rel="bookmark">ActionScript 的 get 和 set 关键字</a><!-- (12.8)--></li>
		<li><a href="http://www.neoease.com/actionscript-development-environment/" rel="bookmark">ActionScript 开发环境搭建</a><!-- (10.9)--></li>
		<li><a href="http://www.neoease.com/stoppropagation-and-preventdefault-and-return-false/" rel="bookmark">stopPropagation, preventDefault 和 return false 的区别</a><!-- (3.9)--></li>
		<li><a href="http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/" rel="bookmark">Lazy Load, 延迟加载图片的 jQuery 插件</a><!-- (3.8)--></li>
		<li><a href="http://www.neoease.com/show-whats-coming-next-on-your-blog/" rel="bookmark">在 WordPress 博客中预告文章</a><!-- (3.5)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/dispatching-custom-events-by-dispatchevent-method/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>ActionScript 的 get 和 set 关键字</title>
		<link>http://www.neoease.com/actionscript-keywords-get-and-set/</link>
		<comments>http://www.neoease.com/actionscript-keywords-get-and-set/#comments</comments>
		<pubDate>Tue, 14 Dec 2010 13:56:16 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[Keyword]]></category>
		<category><![CDATA[set]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1718</guid>
		<description><![CDATA[ActionScript 提供 getter 和 setter 以存取类的属性. 在 OOP 编程的角度, 是出于类的封装性考虑, 类的继承 (protected) 和私有化 (private) 的属性不能直接暴露在外, 外部只能通过存取方法访问和修改. 假设现在有一个去理发的女生, 年龄只有她自己知道, 发型师可以获知她的发质, 知道并可以改变她的发型. 那么 ActionScript 类可以定义如下: public class Lady &#123; &#160; // 年龄 private var _age:int; &#160; // 发质 protected var _hairQuality:String; public function get hairQuality&#40;&#41;:String &#123; return this._hairQuality; &#125; &#160; // 发型 private var _hairStyle:String; public function [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
ActionScript 提供 getter 和 setter 以存取类的属性. 在 OOP 编程的角度, 是出于类的封装性考虑, 类的继承 (protected) 和私有化 (private) 的属性不能直接暴露在外, 外部只能通过存取方法访问和修改.
</p>
<p><span id="more-1718"></span></p>
<p>
假设现在有一个去理发的女生, 年龄只有她自己知道, 发型师可以获知她的发质, 知道并可以改变她的发型. 那么 ActionScript 类可以定义如下:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Lady <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// 年龄</span>
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _age<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// 发质</span>
	<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _hairQuality<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> hairQuality<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_hairQuality<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// 发型</span>
	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _hairStyle<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> hairStyle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_hairStyle<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> hairStyle<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_hairStyle = <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Lady<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_age = <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_hairQuality = <span style="color: #990000;">&quot;damaged&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_hairStyle = <span style="color: #990000;">&quot;fashion&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
由代码可以看出, 通过存取方法可以选择性地开放外部对属性的操作. 存取方法的写法也与一般的方法写法不同点在于 get 和 set 关键字. 一旦写为存取方法, 其调用方式将与常规方法不同, 将通过点运算符进行操作, 如:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> lady<span style="color: #000066; font-weight: bold;">:</span>Lady = <span style="color: #0033ff; font-weight: bold;">new</span> Lady<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>lady<span style="color: #000066; font-weight: bold;">.</span>hairStyle == <span style="color: #990000;">&quot;gliding&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	lady<span style="color: #000066; font-weight: bold;">.</span>hairStyle = <span style="color: #990000;">&quot;fashion&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
另外, 使用存取方法还可以覆盖从超类继承来的属性, 这是常规类属性不能做到的, 可以对从超类继承的 getter 和 setter 方法使用 override 属性.
</p>
<p>
ActionScript 存取方法的思想和 C# 中 "域 (field)" 的概念几乎一样, 倡导开发者不使用 public 声明属性, 而通过 getter 和 setter 对外提供服务. 而 C# 要求同一个属性的 getter 和 setter 写在一起, 可以减少开发者遗漏和犯错.
</p>
<p>
或许因为我很长一段时间在敲 Java, 这种类似原型 (prototype) 的调用方式让我感到难受. 如果用 Java 对上面的 Lady 再写一遍, 会是这样的...
</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Lady <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 年龄</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">int</span> _age<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Lady<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>._age = <span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>._hairQuality = <span style="color: #0000ff;">&quot;damaged&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>._hairStyle = <span style="color: #0000ff;">&quot;fashion&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 发质</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399; font-weight: bold;">String</span> _hairQuality<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399; font-weight: bold;">String</span> getHairQuality <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>._hairQuality<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 发型</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">String</span> _hairStyle<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> getHairStyle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>._hairStyle<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setHairStyle<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>._hairStyle = value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Java 恨不得将所有属性设为继承和私有, 而通过对外的方法提供信息. 在外部调用时, 其调用方法与常规方法无异, 如果不是 JavaBean, 没有任何特殊要求. 好处是什么? 属性是属性, 方法是方法, 没有存取方法什么的这种特殊类型, 所以代码可以更加清晰易懂.
</p>
<p>
我是比较习惯 Java 的写法, 将会尝试将这种写法用在 ActionScript 中. 更多关于 ActionScript 的 getter 和 setter 基础可以参考: <a rel="nofollow external" href="http://help.adobe.com/zh_CN/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f30.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcb">Adobe 关于 get 和 set 存取器方法的说明</a></p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1718" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/dispatching-custom-events-by-dispatchevent-method/" rel="bookmark">ActionScript 3 调度个性化事件</a><!-- (12.4)--></li>
		<li><a href="http://www.neoease.com/actionscript-development-environment/" rel="bookmark">ActionScript 开发环境搭建</a><!-- (10.6)--></li>
		<li><a href="http://www.neoease.com/notepad-opens-actionscript-file-with-haskell-syntax-highlighting/" rel="bookmark">Notepad++ 对 AS 文件语法高亮</a><!-- (7.2)--></li>
		<li><a href="http://www.neoease.com/javascript-with-statement/" rel="bookmark">关于 JavaScript 的 with 语句</a><!-- (2.3)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/actionscript-keywords-get-and-set/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Notepad++ 对 AS 文件语法高亮</title>
		<link>http://www.neoease.com/notepad-opens-actionscript-file-with-haskell-syntax-highlighting/</link>
		<comments>http://www.neoease.com/notepad-opens-actionscript-file-with-haskell-syntax-highlighting/#comments</comments>
		<pubDate>Tue, 14 Dec 2010 04:58:16 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[Highlight]]></category>
		<category><![CDATA[Notepad++]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1714</guid>
		<description><![CDATA[Notepad++ 有 ActionScript 的语法高亮功能, 但它似乎不能被程序探查到. 打开的每个 AS 文件, 都要自行套用语法高亮, 特别麻烦. 当你打开一批文件, 那就需要逐个文件进行设定语言为 Flash actionscript. 可以通过以下步骤解决探查不到 AS 语法的问题. 找到并打开 langs.xml 文件. 你可以在 Notepad++ 的安装目录中找到这个文件, 默认路径是: C:\Program Files\Notepad++ 找到 haskell 语句. &#60; Language name="haskell" ext="hs lhs as las" commentLine="--" &#62; 从 ext 属性中删除 "as", 并保存文件. 使用 Notepad++ 重新打开 AS 文件, 它们将使用正确的语法高亮. 本文翻译自: Notepad++ opens ActionScript file with [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
Notepad++ 有 ActionScript 的语法高亮功能, 但它似乎不能被程序探查到. 打开的每个 AS 文件, 都要自行套用语法高亮, 特别麻烦. 当你打开一批文件, 那就需要逐个文件进行设定语言为 <code>Flash actionscript</code>.
</p>
<p><span id="more-1714"></span></p>
<p>
可以通过以下步骤解决探查不到 AS 语法的问题.
</p>
<ol>
<li>
找到并打开 <code>langs.xml</code> 文件.<br />
你可以在 Notepad++ 的安装目录中找到这个文件, 默认路径是:<br />
<code>C:\Program Files\Notepad++</code>
</li>
<li>
找到 haskell 语句.<br />
<code>&lt; Language name="haskell" ext="hs lhs as las" commentLine="--" &gt;</code>
</li>
<li>
从 ext 属性中删除 "<code>as</code>", 并保存文件.
</li>
</ol>
<p>
使用 Notepad++ 重新打开 AS 文件, 它们将使用正确的语法高亮.
</p>
<p>
本文翻译自: <a rel="nofollow external" href="http://www.matthijskamstra.nl/blog/index.php/2009/05/27/notepad-opens-actionscript-file-with-haskell-syntax-highlighting/">Notepad++ opens ActionScript file with Haskell syntax highlighting</a></p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1714" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/actionscript-keywords-get-and-set/" rel="bookmark">ActionScript 的 get 和 set 关键字</a><!-- (5.9)--></li>
		<li><a href="http://www.neoease.com/dispatching-custom-events-by-dispatchevent-method/" rel="bookmark">ActionScript 3 调度个性化事件</a><!-- (3.3)--></li>
		<li><a href="http://www.neoease.com/actionscript-development-environment/" rel="bookmark">ActionScript 开发环境搭建</a><!-- (2.4)--></li>
		<li><a href="http://www.neoease.com/json-editor-online/" rel="bookmark">线上 JSON 编辑工具</a><!-- (1)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/notepad-opens-actionscript-file-with-haskell-syntax-highlighting/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>ActionScript 开发环境搭建</title>
		<link>http://www.neoease.com/actionscript-development-environment/</link>
		<comments>http://www.neoease.com/actionscript-development-environment/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 14:46:39 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Environment]]></category>
		<category><![CDATA[Hello World]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1615</guid>
		<description><![CDATA[虽然我一直不喜欢 Flash, 但作为前端, 我还是忍不住要玩一下传说中的 AS3. 今天在师弟和网友的帮助下, 终于将开发环境搭建了起来, 写出那让人怀念的 Hello World. 记录一下搭建步骤, 没啥技术含量, 纯学习笔记. 1. 安装 Flex SDK 选择 Adobe Flex SDK 下载, 并解压到任何你愿意放置程序的目录中. (因为文件大, 要下载很久, 所以我先写这个步骤.) 下载列表 2. 安装 JRE/JDK 下载并安装 JRE 或者 JDK, 要求 1.6 或以上版本. 下载列表 3. 安装 Flash player Debug 选择 Projector content debugger 下载, 并解压到任何你愿意放置程序的目录中. 下载列表 4. 安装 FlashDevelop 下载并安装 FlashDevelop, [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
虽然我一直不喜欢 Flash, 但作为前端, 我还是忍不住要玩一下传说中的 <strong>AS3</strong>. 今天在师弟和网友的帮助下, 终于将<strong>开发环境</strong>搭建了起来, 写出那让人怀念的 <strong>Hello World</strong>.
</p>
<p>
记录一下<strong>搭建步骤</strong>, 没啥技术含量, 纯学习笔记.
</p>
<p><span id="more-1615"></span></p>
<h3>1. 安装 Flex SDK</h3>
<p>
选择 <strong>Adobe Flex SDK</strong> 下载, 并解压到任何你愿意放置程序的目录中. (因为文件大, 要下载很久, 所以我先写这个步骤.)<br />
<a rel="external" href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">下载列表</a>
</p>
<h3>2. 安装 JRE/JDK</h3>
<p>
下载并安装 <strong>JRE</strong> 或者 <strong>JDK</strong>, 要求 1.6 或以上版本.<br />
<a rel="nofollow external" href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">下载列表</a>
</p>
<h3>3. 安装 Flash player Debug</h3>
<p>
选择 <strong>Projector content debugger</strong> 下载, 并解压到任何你愿意放置程序的目录中.<br />
<a rel="nofollow external" href="http://www.adobe.com/support/flashplayer/downloads.html">下载列表</a>
</p>
<h3>4. 安装 FlashDevelop</h3>
<p>
下载并安装 <strong>FlashDevelop</strong>, 程序依赖 .NET Framework 2.0.<br />
<a rel="external" href="http://www.flashdevelop.org/community/viewforum.php?f=11">下载列表</a>
</p>
<h3>5. 配置 FlashDevelop</h3>
<p>
5.1 进入 <strong>Tools -> Programe Settings</strong>, 在 <strong>AS3Context</strong> 下将 <strong>Flex SDK Location</strong> 设置为第 1 步中的程序存放目录.
</p>
<p>
5.2 在 <strong>FlashViewer</strong> 下将 <strong>External player Path</strong> 设置为第 3 步中的程序存放目录.
</p>
<h3>6. 新建项目</h3>
<p>
选择 <strong>Project -> New Project...</strong>, 选择 <strong>AS3 Project</strong>, 选项填写如下:<br />
<strong>Name:</strong> HelloWorld<br />
<strong>Location:</strong> (任何你愿意放置该项目的目录)<br />
<strong>Pagckage:</strong> (留空)<br />
<strong>Create directory for project:</strong> (选中)
</p>
<h3>7. 编写代码</h3>
<p>
点开 <strong>src</strong> 目录, 双击打开 <strong>Main.as</strong> 文件, 修改代码如下:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hello World!'</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>8. 修改项目选项</h3>
<p>
选择 <strong>Project -> Properties...</strong>, 将 <strong>Test Movie</strong> 修改为 <strong>Play in external player</strong>, 否则看不到 trace 输出.
</p>
<h3>9. 编译运行</h3>
<p>
请勇敢地按下 <strong>Ctrl + Enter</strong>.
</p>
<h3>学习笔记</h3>
<p>
使用 FlashDevelop 是因为相对于 FlashBuilder 这比较轻量级, 而且不用钱. 至于好不好用, 用一段时间在作评价. 虽然之前完全没有接触过, 也不打算看什么书了, 但这次学 AS 我是很认真的. 以后我会将相关的学习比较用文章形式写出来, 我相信学习过程中写出来的东西最真实.
</p>
<p>
本文参考: <a href="http://www.qilei.org/200810/flashdevelop-build-up/">AS3-学习笔记-flashdevelop平台-搭建-配置</a></p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1615" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/dispatching-custom-events-by-dispatchevent-method/" rel="bookmark">ActionScript 3 调度个性化事件</a><!-- (10.8)--></li>
		<li><a href="http://www.neoease.com/actionscript-keywords-get-and-set/" rel="bookmark">ActionScript 的 get 和 set 关键字</a><!-- (5.8)--></li>
		<li><a href="http://www.neoease.com/seo-links/" rel="bookmark">关于网站的链接优化</a><!-- (4.1)--></li>
		<li><a href="http://www.neoease.com/notepad-opens-actionscript-file-with-haskell-syntax-highlighting/" rel="bookmark">Notepad++ 对 AS 文件语法高亮</a><!-- (2.9)--></li>
		<li><a href="http://www.neoease.com/update-feedsky-subscribe-number-everyday/" rel="bookmark">每日同步 Feedsky 订阅数</a><!-- (2.9)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/actionscript-development-environment/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>960 网格系统即将过时</title>
		<link>http://www.neoease.com/960-grid-system-is-getting-old/</link>
		<comments>http://www.neoease.com/960-grid-system-is-getting-old/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 03:50:31 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[960]]></category>
		<category><![CDATA[978]]></category>
		<category><![CDATA[Grid]]></category>
		<category><![CDATA[Page]]></category>
		<category><![CDATA[Translation]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1543</guid>
		<description><![CDATA[960px 网格系统 (960.gs), 也称 960 栅格布局, 数年来作为网页设计人员的最爱, 被用来搭建网站和设计网页布局. 当显示屏和分辨率变得更大, 我发现 960 网格系统已经不能完全满足我的需求. 20px 的槽让内容区域实际只有 940px, 对现代的 Web 设计来说太窄小了. 在设计 Themify 主题的时候, 我发现一种新的网格布局, 相当适合用来设计当今的 Web 布局. 关于 960px 网格系统 960 网格系统的构造如下: 页面总宽度 960px 12 栏布局, 每栏 60px 每栏两边保留 10px 的外边距, 相当于 20px 的槽 内容区域总宽度是 940px 960 布局无疑是非常好的网格系统, 因为相当灵活. 它帮助网页设计者快速地构造以下栏栅数目的布局原型: 9 x 3, 3 x 3 x [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
960px 网格系统 (<a rel="external" href="http://960.gs/">960.gs</a>), 也称 960 栅格布局, 数年来作为网页设计人员的最爱, 被用来搭建网站和设计网页布局. 当显示屏和分辨率变得更大, 我发现 960 网格系统已经不能完全满足我的需求. 20px 的槽让内容区域实际只有 940px, 对现代的 Web 设计来说太窄小了. 在设计 Themify 主题的时候, 我发现一种新的网格布局, 相当适合用来设计当今的 Web 布局.
</p>
<p><span id="more-1543"></span></p>
<h3>关于 960px 网格系统</h3>
<p>
960 网格系统的构造如下:<br />
页面总宽度 960px<br />
12 栏布局, 每栏 60px<br />
每栏两边保留 10px 的外边距, 相当于 20px 的槽<br />
内容区域总宽度是 940px
</p>
<p>
960 布局无疑是非常好的网格系统, 因为相当灵活. 它帮助网页设计者快速地构造以下栏栅数目的布局原型: 9 x 3, 3 x 3 x 3, 4 x 4 x 4 x 4, 10 x 2 等. 960 网格系统可能是现在最受欢迎的网格系统, 并已经在很多网站和设计模板上使用.
</p>
<h3>存在的问题</h3>
<p>
问题在于 960 网格系统槽空间和内容宽度. 除去外边距, 实际的内容区域只有 940px. 以往能很好被使用是因为大部分设计者使用 12px 的字号. 而现在大多设计者喜欢将字体大小设为 13px, 甚至更大. 既然文字变大, 你不认为我们同样需要加大内容区域吗?
</p>
<p>
<img class="sided" src="http://www.webdesignerwall.com/wp-content/uploads/2010/09/960gs.gif" alt="960 网格系统" />
</p>
<h3>978 网格</h3>
<p>
我 (文章作者) 在 Themify 主题中使用 978px 宽度, 分 12 栏, 每栏 54px, 槽宽 30px. 我发现这种结构相当好用, 而且在 1024 x 768 分辨率下表现极佳. 我将槽的宽度增加到 30px, 所以你内容有更多的呼吸的空间. 效果如下:
</p>
<p>
<img class="sided" src="http://www.webdesignerwall.com/wp-content/uploads/2010/09/978-grid.gif" alt="978 网格系统" />
</p>
<p>
原文链接: <a rel="external" href="http://www.webdesignerwall.com/trends/960-grid-system-is-getting-old/">960 Grid System is Getting Old</a>
</p>
<h3>译者的看法</h3>
<p>
我会翻译这个文章是因为本文的作者思考问题的角度与我不一样. 我写过一个类似的文章: <a href="http://www.neoease.com/page-width-and-column-grid/">关于页面宽度的选择, 950px 布局与 960px 布局的比较</a>.
</p>
<p>
我认为作者的想法是对的, 但是不能接受. 我的观点是: 管他 950, 960 还是 978, 最终是为了设计出能充分利用空间的, 灵活且美观的网站, 适合就好. 网格系统对大型的网站很有用, 它可以兼顾多种页面布局的需要, 以减少设计和维护成本. 但对几个页面的小网站来说, 网格系统更可能令网站变得复杂.
</p>
<p>
一个破小博客搞什么网格, 扯淡嘛.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1543" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/page-width-and-column-grid/" rel="bookmark">关于页面的宽度: 950 还是 960?</a><!-- (9.6)--></li>
		<li><a href="http://www.neoease.com/the-basics-of-css3/" rel="bookmark">CSS3 基础知识</a><!-- (4.9)--></li>
		<li><a href="http://www.neoease.com/css3-dropdown-menu/" rel="bookmark">CSS3 下拉菜单</a><!-- (3.8)--></li>
		<li><a href="http://www.neoease.com/css-menu-list-design/" rel="bookmark">CSS 菜单列表设计</a><!-- (3.4)--></li>
		<li><a href="http://www.neoease.com/json-editor-online/" rel="bookmark">线上 JSON 编辑工具</a><!-- (2.2)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/960-grid-system-is-getting-old/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>随机排序广告</title>
		<link>http://www.neoease.com/random-advertisements/</link>
		<comments>http://www.neoease.com/random-advertisements/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 08:15:03 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Advertisement]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1364</guid>
		<description><![CDATA[博客流行在侧边栏放置 4 到 6 个 125x125 的广告, 但一般的摆放顺序存在问题. 如果广告位置被固定, 各个位置的天然关注度肯定是不一样的. 投放广告的人都很关注他的广告会放在哪个位置, 因为这可能影响点击次数, 甚至是否在第一屏显示. 就这个问题, 其实很容易解决, 只要随机显示广告即可. 代码如何实现? 在这我推荐两种随机显示广告的处理办法. 在后端处理 在后端排序好再输出页面. 将广告节点用数组存放, 对数组进行随机排序, 再输出经过排序的数组. 参考代码 (PHP) 如下: // 用数组存放广告列表 $ads = array&#40;'&#60;a href=&#34;#&#34;&#62;&#60;img src=&#34;ad-125x125.png&#34; alt=&#34;广告 1&#34; width=&#34;125&#34; height=&#34;125&#34; /&#62;&#60;/a&#62;' ,'&#60;a href=&#34;#&#34;&#62;&#60;img src=&#34;ad-125x125.png&#34; alt=&#34;广告 2&#34; width=&#34;125&#34; height=&#34;125&#34; /&#62;&#60;/a&#62;' ,'&#60;a href=&#34;#&#34;&#62;&#60;img src=&#34;ad-125x125.png&#34; alt=&#34;广告 3&#34; width=&#34;125&#34; height=&#34;125&#34; /&#62;&#60;/a&#62;' ,'&#60;a [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
博客流行在侧边栏放置 4 到 6 个 125x125 的<strong>广告</strong>, 但一般的<strong>摆放顺序</strong>存在问题. 如果广告位置被固定, 各个位置的天然关注度肯定是不一样的.
</p>
<p>
投放广告的人都很关注他的广告会放在哪个位置, 因为这可能影响点击次数, 甚至是否在第一屏显示. 就这个问题, 其实很容易解决, 只要<strong>随机</strong>显示广告即可.
</p>
<p>
代码如何实现? 在这我推荐两种随机显示广告的处理办法.
</p>
<p><span id="more-1364"></span></p>
<h3>在后端处理</h3>
<p>
在后端排序好再输出页面. 将广告节点用数组存放, 对数组进行随机排序, 再输出经过排序的数组. 参考代码 (PHP) 如下:
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 用数组存放广告列表</span>
<span style="color: #000088;">$ads</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 1&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
			<span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 2&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
			<span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 3&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
			<span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 4&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 对数组进行随机排序</span>
<span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ads</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 输出经过排序的数组</span>
<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ads</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ad</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$ad</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span></pre></div></div>

<p>
我们来扩展一下, 如果我是站长, 预留了 4 个广告位, 但现在只有 3 个在投放; 我想在空置的广告位放置一个 "虚位以待" 的广告招租链接, 并显示在最后, 该如何处理呢? 在排序完成之后再插放广告招租链接就可以了.
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 用数组存放广告列表</span>
<span style="color: #000088;">$ads</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 1&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
			<span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 2&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
			<span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 3&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 对数组进行随机排序</span>
<span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ads</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 输出经过排序的数组</span>
<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ads</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ad</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$ad</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 添加广告招租链接</span>
<span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;sell-ad-125x125.png&quot; alt=&quot;虚位以待&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span></pre></div></div>

<p>
我就是用这个方法来输出 125x125 广告的, 因为它直观可靠, 处理方便. 但是如果你希望对页面做静态化, 建议选用 JS 随机排序的方法.
</p>
<h3>在前端处理</h3>
<p>
在后端按原有循序输出, 在页面通过 JavaScript 重新排序. 假设页面输出广告区域的 HTML 片段如下.
</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;ads&quot;&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 1&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 2&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 3&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 4&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;</pre></div></div>

<p>
我们可以通过 JS 来对广告进行重新排序. 参考代码如下:
</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;ads&quot; style=&quot;display:none;&quot;&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 1&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 2&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 3&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
	&lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;ad-125x125.png&quot; alt=&quot;广告 4&quot; width=&quot;125&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div id=&quot;random-ads&quot; style=&quot;display:none;&quot;&gt;
&lt;/div&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot;&gt;
//&lt;![CDATA[
&nbsp;
var source = document.getElementById('ads');
var target = document.getElementById('random-ads');
var ads = source.getElementsByTagName('a');
&nbsp;
// 下标数组
var arr = new Array();
for(var i=0; i&lt;ads.length; i++) {
	arr[i] = i;
}
&nbsp;
// 随机排序
function randomSort(a, b){
	var tmp = parseInt((Math.random() + 0.5), 10);
	return tmp ? a-b : b-a;
}
&nbsp;
// 将老的广告区的节点随机插放到新的广告区
arr.sort(randomSort);
for(var i=0; i&lt;arr.length; i++) {
	target.appendChild(ads[arr[i]].cloneNode(true));
}
&nbsp;
// 显示新的广告区和移除老的广告区
source.parentNode.removeChild(source);
target.style.display = 'block';
&nbsp;
//]]&gt;
&lt;/script&gt;</pre></div></div>

<p>
如果有如同方法 1 那样的扩展需求, 将空广告位显示在最后, 且显示广告招租链接, 该如何处理? 这个当作课后习题吧...</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1364" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/update-feedsky-subscribe-number-everyday/" rel="bookmark">每日同步 Feedsky 订阅数</a><!-- (4.5)--></li>
		<li><a href="http://www.neoease.com/show-diff-between-crawlers-and-guests/" rel="bookmark">向访客和爬虫显示不同的内容</a><!-- (4.3)--></li>
		<li><a href="http://www.neoease.com/spliting-blogroll-to-2-columns-03/" rel="bookmark">将 Blogroll 分为两栏 (3)</a><!-- (4.2)--></li>
		<li><a href="http://www.neoease.com/output-html-in-javascript/" rel="bookmark">用 JavaScript 输出页面代码</a><!-- (3.3)--></li>
		<li><a href="http://www.neoease.com/jquery-slideup-and-slidedown/" rel="bookmark">jQuery 的 slideUp 和 slideDown 动画</a><!-- (3.3)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/random-advertisements/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>谷歌音乐搜索栏的提示功能</title>
		<link>http://www.neoease.com/search-tip-of-google-cn-music/</link>
		<comments>http://www.neoease.com/search-tip-of-google-cn-music/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 14:35:51 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Search]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1355</guid>
		<description><![CDATA[我很喜欢在 google.cn/music 上找 mp3, 因为谷歌音乐在版权方面, 用户体验和搜索功能都做得很好. 但是它的搜索栏提示功能一直存在一个小问题, bug 存在半年以上了吧... 问题描述 在加载页面的时候, 将光标快速定位到搜索栏上, 待页面加载完成, 搜索栏进行初始化后会显示搜索提示. 此时输入的任何内容将成为搜索提示的一部分而不是搜索关键字. 截图如下: 导致原因 搜索栏的 JavaScript 初始化执行在 onload 的时候. 因为页面图片请求多, 完全加载需要 3 秒钟左右, 并且搜索栏的 tabindex 被设为 1, 搜索优先的用户很容易就能遇到. 以下是我根据自己的理解反编译出来的 JS 代码, 页面在 onload 的时候将会执行 m.hint.initHint 方法为搜索框添加提示功能. /** * 为搜索框添加提示功能 * @param searchTip 提示信息 * @param searchBoxId 搜索输入框 ID * @param hideBoxId 关键字隐藏框 [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
我很喜欢在 google.cn/music 上找 mp3, 因为<strong>谷歌音乐</strong>在版权方面, 用户体验和搜索功能都做得很好. 但是它的<strong>搜索栏提示功能</strong>一直存在一个小问题, bug 存在半年以上了吧...
</p>
<h3>问题描述</h3>
<p>
在加载页面的时候, 将光标快速定位到搜索栏上, 待页面加载完成, 搜索栏进行初始化后会显示搜索提示. 此时输入的任何内容将成为搜索提示的一部分而不是搜索关键字. 截图如下:
</p>
<p>
<img class="sided" src="http://farm5.static.flickr.com/4076/4760612976_6c990697d7.jpg" alt="谷歌音乐搜索栏的 bug" />
</p>
<p><span id="more-1355"></span></p>
<h3>导致原因</h3>
<p>
搜索栏的 JavaScript 初始化执行在 onload 的时候. 因为页面图片请求多, 完全加载需要 3 秒钟左右, 并且搜索栏的 tabindex 被设为 1, 搜索优先的用户很容易就能遇到.
</p>
<p>
以下是我根据自己的理解反编译出来的 JS 代码, 页面在 onload 的时候将会执行 m.hint.initHint 方法为搜索框添加提示功能.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * 为搜索框添加提示功能
 * @param searchTip		提示信息
 * @param searchBoxId	搜索输入框 ID
 * @param hideBoxId		关键字隐藏框 ID
 */</span>
m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">initHint</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>searchTip<span style="color: #339933;">,</span> searchBoxId<span style="color: #339933;">,</span> hideBoxId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> searchBox <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>searchBoxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> hideBox <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;">if</span><span style="color: #009900;">&#40;</span>searchBox<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>hideBoxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			hideBox <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>hideBoxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		l.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;blur&quot;</span><span style="color: #339933;">,</span> l.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">onInputBlur</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> searchBox<span style="color: #339933;">,</span> hideBox<span style="color: #009900;">&#41;</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>
		l.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;focus&quot;</span><span style="color: #339933;">,</span> l.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">onInputFocus</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> searchBox<span style="color: #339933;">,</span> hideBox<span style="color: #009900;">&#41;</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>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>hideBox<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			l.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;change&quot;</span><span style="color: #339933;">,</span> bind<span style="color: #009900;">&#40;</span>m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">onInputChange</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> searchBox<span style="color: #339933;">,</span> hideBox<span style="color: #009900;">&#41;</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>
			hideBox.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">getInputValue</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// 在这里将搜索提示赋给临时变量</span>
		m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">Gh</span><span style="color: #009900;">&#91;</span>searchBox<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> searchTip<span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// 如果搜索框存在, 则为搜索框加上临时变量和灰色字的 class</span>
		m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">onInputBlur</span><span style="color: #009900;">&#40;</span>searchBox<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>
&nbsp;
m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">onInputBlur</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #339933;">,</span> hideBox<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">fi</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	hideBox <span style="color: #339933;">&amp;&amp;</span> m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">onInputChange</span><span style="color: #009900;">&#40;</span>searchBox<span style="color: #339933;">,</span> hideBox<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">fi</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>searchBox<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>searchBox<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> searchTip <span style="color: #339933;">=</span> m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">Gh</span><span style="color: #009900;">&#91;</span>searchBox<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>searchTip <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>searchBox.<span style="color: #660066;">value</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">||</span> searchBox.<span style="color: #660066;">value</span><span style="color: #339933;">==</span>searchTip<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			searchBox.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span>m.<span style="color: #660066;">hint</span>.<span style="color: #660066;">IS_HINT</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			searchBox.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; hint&quot;</span><span style="color: #339933;">;</span>
			searchBox.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> searchTip<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></div></div>

<h3>解决办法</h3>
<p>
缩短搜索框 DOM 节点加载和 JavaScript 初始化之间的时间. 可以在搜索框加载完成后立刻执行 JS, 反正 Google 自己的产品也不需要 SEO. 当然, 最好在 DOM ready 的时候执行.
</p>
<h3>扩展知识</h3>
<p>
记得我写过一篇文章, 介绍如何在 <a href="http://www.neoease.com/wordpress-searchbox-tip/">WordPress 搜索框添加文字提示</a>. 我的处理办法简单粗暴, 仅以框内关键字来判断是关键字还是提示信息. (所以我的提示信息很长)
</p>
<p>
在分析这个案例的过程中, 我发现 Google 的处理办法很好, 可以借鉴和使用. 它通过隐藏输入框来放置真正的搜索信息, 所以可以实现完全关键字和提示文案的区分.
</p>
<h3>后话</h3>
<p>
这个小问题已经困扰我很久了 (几乎每次遇到), 今天终于忍不住看了一下代码, 希望 Google 的工程师能够看到本文快速解决掉. (修改建议我都给出来了, 还不改也说不过去吧)</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1355" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/wordpress-searchbox-tip/" rel="bookmark">WordPress 搜索框添加文字提示</a><!-- (11.9)--></li>
		<li><a href="http://www.neoease.com/wordpress-searchbox-tip-2/" rel="bookmark">WordPress 搜索框添加文字提示 (续)</a><!-- (10.8)--></li>
		<li><a href="http://www.neoease.com/javascript-go-top/" rel="bookmark">用 JavaScript 实现变速回到顶部</a><!-- (8.8)--></li>
		<li><a href="http://www.neoease.com/wordpress-at-reply/" rel="bookmark">WordPress 评论的 @ 回复</a><!-- (7.7)--></li>
		<li><a href="http://www.neoease.com/stoppropagation-and-preventdefault-and-return-false/" rel="bookmark">stopPropagation, preventDefault 和 return false 的区别</a><!-- (7.1)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/search-tip-of-google-cn-music/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>JavaScript 的 parseInt 取整</title>
		<link>http://www.neoease.com/javascript-get-integer-via-parseint/</link>
		<comments>http://www.neoease.com/javascript-get-integer-via-parseint/#comments</comments>
		<pubDate>Fri, 21 May 2010 13:38:53 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[parseInt]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1270</guid>
		<description><![CDATA[JavaScript 是弱类型语言, 为了保证数值的有效性, 在处理数值的时候, 我们可以对数值字符串进行强行转换. 如 parseInt 取整和 parseFloat 取浮点数. Java 也有 Integer.parseInt() 方法, 但是 JavaScript 的 parseInt 处理方式与 Java 等强整型语言不太一样, 所以经常有人因为对这个方法的使用不当而获得异常返回. 下面是一段 Java 代码, 用于将字符串 020 转为整型. public class Test &#123; public static void main&#40;String args&#91;&#93;&#41; throws Exception &#123; String str = &#34;020&#34;; System.out.println&#40;Integer.parseInt&#40;str&#41;&#41;; &#125; &#125; 输出结果是 20 下面是一段 JavaScript 代码, 同样是将字符串 020 转为整型. [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
JavaScript 是弱类型语言, 为了保证数值的有效性, 在处理数值的时候, 我们可以对数值字符串进行强行转换. 如 <strong>parseInt 取整</strong>和 parseFloat 取浮点数.
</p>
<p>
Java 也有 Integer.parseInt() 方法, 但是 JavaScript 的 parseInt 处理方式与 Java 等强整型语言不太一样, 所以经常有人因为对这个方法的使用不当而获得异常返回.
</p>
<p><span id="more-1270"></span></p>
<p>
下面是一段 Java 代码, 用于将字符串 020 转为整型.
</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;020&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>str<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;">&#125;</span></pre></div></div>

<p>
输出结果是 20
</p>
<p>
下面是一段 JavaScript 代码, 同样是将字符串 020 转为整型.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;020&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>num<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
输出结果是 16
</p>
<h3>为什么呢?</h3>
<p>
无论是 Java 还是 JavaScript, parseInt 方法都有两个参数, 第一个参数就是要转换的对象, 第二个参数是进制基数, 可以是 2, 8, 10, 16, 默认以 10 进制处理. 但在 JavaScript 中, 0 开始的数被认为使用 8 进制处理, 0x 的数被认为是用 16 进制来处理. 所以上面的 JavaScript 代码计算错误了.
</p>
<h3>影响大吗?</h3>
<p>
大! 很大! 因为这个经常用于计算价钱, 一旦价格错误, 对用户来说, 这是误导, 而一个好的网站不应该出现这种误导用户的. 下面的 DEMO 中, 没有指定进制. 大家可以在数量框中输入一个以 0 开头的数字, 在点击计算按钮, 算出来的数值将比预想中的小一些, 或者小很多 (如: 8 进制中没有 019 这样的数值, 所以数值变成了 1, 9 被忽略掉).<br />
<a rel="external" href="http://www.neoease.com/tutorials/javascript-parseint-radix/without-radix.html">没有为 parseInt 函数指定进制的 DEMO</a>
</p>
<h3>如何修改?</h3>
<p>
前面说过, 有两个参数, 第二个参数可以指定计算使用的进制.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">parseInt<span style="color: #009900;">&#40;</span>num<span style="color: #339933;">,</span> radix<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
所以我们可以将前面有问题的那段 JavaScript 代码改写为下面的代码.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;020&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>num<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
这样处理的话, 我们再重写一些前面的 DEMO, 如下:<br />
<a rel="external" href="http://www.neoease.com/tutorials/javascript-parseint-radix/with-radix.html">为 parseInt 函数指定进制为 10 的 DEMO</a>
</p>
<p>记住了, 在 JavaScript 上使用 parseInt 方法时要带上进制参数.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1270" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/javascript-with-statement/" rel="bookmark">关于 JavaScript 的 with 语句</a><!-- (11.7)--></li>
		<li><a href="http://www.neoease.com/wordpress-javascript-l10n/" rel="bookmark">WordPress 的 JavaScript 本地化</a><!-- (9.9)--></li>
		<li><a href="http://www.neoease.com/javascript-namespace/" rel="bookmark">JavaScript 的命名空间</a><!-- (9.2)--></li>
		<li><a href="http://www.neoease.com/string-is-empty/" rel="bookmark">判断 Java 中的空字符串</a><!-- (8.7)--></li>
		<li><a href="http://www.neoease.com/get-cursor-position-with-javascript/" rel="bookmark">通过 JavaScript 获取页面上的鼠标位置</a><!-- (7.5)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/javascript-get-integer-via-parseint/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>关于 JavaScript 的 with 语句</title>
		<link>http://www.neoease.com/javascript-with-statement/</link>
		<comments>http://www.neoease.com/javascript-with-statement/#comments</comments>
		<pubDate>Mon, 17 May 2010 09:58:32 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Keyword]]></category>
		<category><![CDATA[Statement]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1263</guid>
		<description><![CDATA[JavaScript 有个 with 关键字, with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象. 用过 Java 和 .NET 的同学对包或命名空间的概念应该不会陌生, 正因为有这个概念, 使代码的简洁易读得到了保证. 不知 JavaScript 设计之初是如何定位 with 语句的, 个人觉得它们之间有一定的相似度. 如: apple.banana.candy.dog.egg.fog.god.huh.index = 0; doSomething&#40;apple.banana.candy.dog.egg.fog.god.huh.index&#41;; 利用 with 语句, 可以写为以下代码. with&#40;apple.banana.candy.dog.egg.fog.god.huh&#41; &#123; c = 0; doSomething&#40;index&#41;; &#125; 看起来很美妙, 却存在致命的缺陷. 下面我们来进行一些小测试吧. 1. 在 with 语句内部通过内部变量修改数值 var root = &#123; branch: &#123; node: 1 &#125; &#125;; &#160; [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
<strong>JavaScript</strong> 有个 <strong>with</strong> 关键字, with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象.
</p>
<p>
用过 Java 和 .NET 的同学对包或命名空间的概念应该不会陌生, 正因为有这个概念, 使代码的简洁易读得到了保证. 不知 JavaScript 设计之初是如何定位 with 语句的, 个人觉得它们之间有一定的相似度. 如:
</p>
<p><span id="more-1263"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">apple.<span style="color: #660066;">banana</span>.<span style="color: #660066;">candy</span>.<span style="color: #660066;">dog</span>.<span style="color: #660066;">egg</span>.<span style="color: #660066;">fog</span>.<span style="color: #660066;">god</span>.<span style="color: #660066;">huh</span>.<span style="color: #660066;">index</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
doSomething<span style="color: #009900;">&#40;</span>apple.<span style="color: #660066;">banana</span>.<span style="color: #660066;">candy</span>.<span style="color: #660066;">dog</span>.<span style="color: #660066;">egg</span>.<span style="color: #660066;">fog</span>.<span style="color: #660066;">god</span>.<span style="color: #660066;">huh</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
利用 with 语句, 可以写为以下代码.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>apple.<span style="color: #660066;">banana</span>.<span style="color: #660066;">candy</span>.<span style="color: #660066;">dog</span>.<span style="color: #660066;">egg</span>.<span style="color: #660066;">fog</span>.<span style="color: #660066;">god</span>.<span style="color: #660066;">huh</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	c <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	doSomething<span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
看起来很美妙, 却存在致命的缺陷. 下面我们来进行一些小测试吧.
</p>
<p>
1. 在 with 语句内部通过内部变量修改数值
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	branch<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		node<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	node <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// 显示 0, 正确!</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// 显示 0, 正确!</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
2. 在 with 语句内部通过对象节点修改数值
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	branch<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		node<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	root.<span style="color: #660066;">branch</span>.<span style="color: #660066;">node</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// 显示 0, 正确!</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// 显示 0, 正确!</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
经过测试 1 和测试 2, 乍看没什么问题, 但是... 请看测试 3.
</p>
<p>
3. 在 with 语句内部通过对象父节点修改数值
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	branch<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		node<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	root.<span style="color: #660066;">branch</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		node<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// 显示 1, 错误!</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// 显示 0, 正确!</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
由上面的测试 3 可知, with 语句内部的节点父节点修改后, 不会同步到节点本身. 也就是说, 不能保证内外数值的一致性. 这是可能成为项目里面隐藏性很高的 bug.<br />
那我们该怎么办呢? 接受那很长的一串逐级访问, 还是另有他法?
</p>
<p>
方法是有的. 我们可以通过别名引用父节点的方式来调用节点对象, 如:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	branch<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		node<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> quote <span style="color: #339933;">=</span> root.<span style="color: #660066;">branch</span><span style="color: #339933;">;</span>
quote.<span style="color: #660066;">node</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// 显示 0, 正确!</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">branch</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
我相信很少人会用 with 语句, 也不会有很多人知道这个关键字, 但我觉得这是个有问题的语句, 压根就不应该使用, 所以写个小文记录一下.</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1263" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/wordpress-javascript-l10n/" rel="bookmark">WordPress 的 JavaScript 本地化</a><!-- (8.5)--></li>
		<li><a href="http://www.neoease.com/javascript-get-integer-via-parseint/" rel="bookmark">JavaScript 的 parseInt 取整</a><!-- (7)--></li>
		<li><a href="http://www.neoease.com/javascript-namespace/" rel="bookmark">JavaScript 的命名空间</a><!-- (5.9)--></li>
		<li><a href="http://www.neoease.com/javascript-magnifier-get-multiple-and-viewport-size/" rel="bookmark">JavaScript 放大镜 - 放大倍率和视窗尺寸</a><!-- (4.5)--></li>
		<li><a href="http://www.neoease.com/get-cursor-position-with-javascript/" rel="bookmark">通过 JavaScript 获取页面上的鼠标位置</a><!-- (4.5)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/javascript-with-statement/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>JavaScript 放大镜 - 放大倍率和视窗尺寸</title>
		<link>http://www.neoease.com/javascript-magnifier-get-multiple-and-viewport-size/</link>
		<comments>http://www.neoease.com/javascript-magnifier-get-multiple-and-viewport-size/#comments</comments>
		<pubDate>Sun, 16 May 2010 16:48:01 +0000</pubDate>
		<dc:creator>mg12</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Glass]]></category>
		<category><![CDATA[Magnifier]]></category>
		<category><![CDATA[Multiple]]></category>
		<category><![CDATA[Thumb]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Viewport]]></category>
		<category><![CDATA[Zoom]]></category>

		<guid isPermaLink="false">http://www.neoease.com/?p=1254</guid>
		<description><![CDATA[对JavaScript 放大镜来说, 计算倍率必不可少. 一个完整的放大镜结构里, 与倍率扯上关系的一共有 4 个对象, 原图, 缩略图, 镜片和视窗. 镜片在缩略图上的覆盖位置, 其实就是视窗对原图的部分展示, 所以它们之间有如下关系. 倍率 = 原图/缩略图 = 视窗/镜片 倍率一般大于等于 1, 因为原图一般都不会小于缩略图. 一旦倍率小于 1, 则将倍率设为 1. 因为缩略图和原图的尺寸是不可变的, 所以我们通过它们来获得倍率. /** * 获取放大镜放大倍数 * @param thumb 缩略图对象 * @param glass 镜片对象 * @return 放大镜放大倍数 */ function getMultiple&#40;thumb, glass&#41; &#123; var multiple = &#123; horizontal:0, vertical:0 &#125;; &#160; var [...]<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></description>
			<content:encoded><![CDATA[<p>
对<strong>JavaScript 放大镜</strong>来说, <strong>计算倍率</strong>必不可少.<br />
一个完整的放大镜结构里, 与倍率扯上关系的一共有 4 个对象, 原图, 缩略图, 镜片和视窗. 镜片在缩略图上的覆盖位置, 其实就是视窗对原图的部分展示, 所以它们之间有如下关系.<br />
<em>倍率 = 原图/缩略图 = 视窗/镜片</em>
</p>
<p>
<img class="sided" src="http://farm2.static.flickr.com/1193/4612139638_4893289779_o.jpg" alt="javaScript,magnifier,multiple,viewport,glass,thumb" />
</p>
<p>
倍率一般大于等于 1, 因为原图一般都不会小于缩略图. 一旦倍率小于 1, 则将倍率设为 1.<br />
因为缩略图和原图的尺寸是不可变的, 所以我们通过它们来获得倍率.
</p>
<p><span id="more-1254"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * 获取放大镜放大倍数
 * @param thumb		缩略图对象
 * @param glass		镜片对象
 * @return 			放大镜放大倍数
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> getMultiple<span style="color: #009900;">&#40;</span>thumb<span style="color: #339933;">,</span> glass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> multiple <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		horizontal<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
		vertical<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> thumbSize <span style="color: #339933;">=</span> getSize<span style="color: #009900;">&#40;</span>thumb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> imageSize <span style="color: #339933;">=</span> getImageSize<span style="color: #009900;">&#40;</span>image<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	multiple.<span style="color: #660066;">horizontal</span> <span style="color: #339933;">=</span> imageSize.<span style="color: #660066;">width</span> <span style="color: #339933;">/</span> thumbSize.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>multiple.<span style="color: #660066;">horizontal</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		multiple.<span style="color: #660066;">horizontal</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	multiple.<span style="color: #660066;">vertical</span> <span style="color: #339933;">=</span> imageSize.<span style="color: #660066;">height</span> <span style="color: #339933;">/</span> thumbSize.<span style="color: #660066;">height</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>multiple.<span style="color: #660066;">vertical</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		multiple.<span style="color: #660066;">vertical</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> multiple<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
原图和缩略图是固定的. 而为了统一视觉感官和防止破页, 我们一般也会指定视窗的尺寸, 所以镜片的尺寸是根据其他几个对象计算出来的.<br />
<em>镜片 = 缩略图x视窗/原图 = 缩略图/倍率</em>
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * 加载镜片的样式
 * @param viewportSize	视窗尺寸
 * @param multiple		倍率
 * @param glass			镜片对象
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> loadGlassStyle<span style="color: #009900;">&#40;</span>viewportSize<span style="color: #339933;">,</span> multiple<span style="color: #339933;">,</span> glass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	glass.<span style="color: #660066;">style</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> round<span style="color: #009900;">&#40;</span>viewportSize.<span style="color: #660066;">width</span> <span style="color: #339933;">/</span> multiple.<span style="color: #660066;">horizontal</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
	glass.<span style="color: #660066;">style</span>.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> round<span style="color: #009900;">&#40;</span>viewportSize.<span style="color: #660066;">height</span> <span style="color: #339933;">/</span> multiple.<span style="color: #660066;">vertical</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
如果定义的视窗的宽或高比原图还要小, 那该如何显示?<br />
需要改变视窗的尺寸. 假设原图是 240x180, 视窗原定尺寸是 200x200. 视窗尺寸随之改变为 200x180. 此时镜片的尺寸仍须与视窗成正比.
</p>
<p>
<img class="sided" src="http://farm2.static.flickr.com/1170/4612139792_9f6fa0fb8a_o.jpg" alt="javaScript,magnifier,multiple,viewport,glass,thumb" />
</p>
<p>
如果倍率小于 1, 又该如何显示?<br />
将倍率设为 1, 镜片覆盖整个缩略图, 显示整个原图为视窗内容; 或者不作操作 (禁止放大镜效果).
</p>
<p>
<img class="sided" src="http://farm5.static.flickr.com/4018/4611527877_df58668fd5_o.jpg" alt="javaScript,magnifier,multiple,viewport,glass,thumb" />
</p>
<p>
下面的代码用于获取视窗的尺寸.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * 返回视窗尺寸
 * @param multiple		倍率
 * @param image			原图对象
 * @return				视窗尺寸
 */</span>
getViewportSize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>multiple<span style="color: #339933;">,</span> image<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> dimension <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		width<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
		height<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// 如果倍率小于 1 或者视窗比原图还宽, 宽度设为跟原图一致, 否则去设定宽度</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>multiple.<span style="color: #660066;">horizontal</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">||</span> config.<span style="color: #660066;">viewportSize</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> image.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		dimension.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> image.<span style="color: #660066;">width</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>
		dimension.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">viewportSize</span><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: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// 如果倍率小于 1 或者视窗比原图还高, 高度设为跟原图一致, 否则去设定高度</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>multiple.<span style="color: #660066;">vertical</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">||</span> config.<span style="color: #660066;">viewportSize</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> image.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		dimension.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> image.<span style="color: #660066;">height</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>
		dimension.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">viewportSize</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> dimension<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
上节<a href="http://www.neoease.com/javascript-magnifier-move-glass/">《JavaScript 放大镜- 移动镜片》</a>我们实现了镜片在缩略图上的移动效果, 并留下习题:"当镜片带边框时, 如何保证边框不影响镜片移动时的精确度?"<br />
为了防止镜片受边框影响偏移, 可以通过为镜片对象设定与边框宽度一样的负 margin 值来消除偏移.
</p>
<p>
本节没有 DEMO, 但对后面几讲来说十分重要, 请同学们搞清楚正常和异常情况下的比例换算.<br />
同样留个课后思考题, 本文的代码中出现了 round 方法, 这是一个四舍五入取整方法, 如果要你自己实现这个功能, 你会如何处理? (提示: 可参考计算机图形学关于线条走样的处理方法)</p>
 <img src="http://www.neoease.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1254" width="1" height="1" style="display: none;" /><ul>
		<li><a href="http://www.neoease.com/javascript-magnifier-move-glass/" rel="bookmark">JavaScript 放大镜 - 移动镜片</a><!-- (30.8)--></li>
		<li><a href="http://www.neoease.com/javascript-go-top/" rel="bookmark">用 JavaScript 实现变速回到顶部</a><!-- (8.5)--></li>
		<li><a href="http://www.neoease.com/get-cursor-position-with-javascript/" rel="bookmark">通过 JavaScript 获取页面上的鼠标位置</a><!-- (8)--></li>
		<li><a href="http://www.neoease.com/javascript-namespace/" rel="bookmark">JavaScript 的命名空间</a><!-- (6.5)--></li>
		<li><a href="http://www.neoease.com/wordpress-thumb-trick/" rel="bookmark">巧用 WordPress 缩略图</a><!-- (6.2)--></li>
	</ul>
<p><hr color="#E0E0E0" />
<div>
Copyright &copy; 2007-2010 NeoEase. All rights reserved.<br />
<a href="http://www.neoease.com" target="_blank">NeoEase.com</a> | <a target="_blank" href="http://twitter.com/mg12" rel="nofollow">Twitter</a> | <a target="_blank" href="http://weibo.com/mg12" rel="nofollow">新浪微博</a> | <a target="_blank" href="http://www.douban.com/people/mg12" rel="nofollow">豆瓣</a> | <a target="_blank" href="http://www.zhihu.com/people/mg12" rel="nofollow">知乎</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neoease.com/javascript-magnifier-get-multiple-and-viewport-size/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

