<?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>Bolo的博客 &#187; W3C</title>
	<atom:link href="http://blog.imbolo.com/tag/w3c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.imbolo.com</link>
	<description>一个设计爱好者杂七杂八的博客</description>
	<lastBuildDate>Wed, 23 Nov 2011 16:19:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>再谈CSS圆角</title>
		<link>http://blog.imbolo.com/creating-rounded-corners-with-css/</link>
		<comments>http://blog.imbolo.com/creating-rounded-corners-with-css/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 13:35:03 +0000</pubDate>
		<dc:creator>bolo</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[W3C]]></category>

		<guid isPermaLink="false">http://blog.imbolo.com/?p=1658</guid>
		<description><![CDATA[在网页设计越来越精美的今天，圆角的应用已经越来越广泛这篇文章将分别讲述圆角在CSS2和CSS3里的创建方法。]]></description>
			<content:encoded><![CDATA[<p><img src="http://i1001.photobucket.com/albums/af131/bolo1988/rounded-corners1.jpg" alt="CSS圆角" class="alignright" />在网页设计越来越精美的今天，圆角的应用已经越来越广泛这篇文章将分别讲述圆角在CSS2和CSS3里的创建方法。</p>
<p>从易用性来说，CSS3不需要额外的标记和图片，比CSS2要先进得多。但当应用在网站上时，采用CSS2能使页面在主流浏览器上的效果更加一致。下面将详细阐述创建圆角的各种方法和其对浏览器的兼容性。</p>
<p><strong style="color:#FF0000;">注：本文以第一个配图的效果作为演示。</strong></p>
<h3>用CSS2创建圆角的方法</h3>
<h4>1.固定宽度的圆角</h4>
<p>这个是最简单的方法最多只需要使用两个图片（顶部和底部），并且也不需要添加额外的标记。在这里，我使用以下两个图片做背景：<img src="http://i1001.photobucket.com/albums/af131/bolo1988/rounded-top.jpg" alt="rounded-top" /><img src="http://i1001.photobucket.com/albums/af131/bolo1988/rounded-bottom.jpg" alt="rounded-bottom" /></p>
<p><span id="more-1658"></span></p>
<h5>HTML:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>This is a heading<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
Lorem ipsum dolor sit amet,
 consectetur adipiscing elit. Sed
vehicula ligula eu diam tincidunt
fermentum. Curabitur facilisis
enim non libero cursus eu varius
enim suscipit. Ut venenatis
vehicula lorem ut hendrerit. Ut
adipiscing augue sed ante volutpat
eget ornare erat facilisis. In hac
habitasse platea dictumst.
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<h5>CSS:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#EE2E24</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;img/rounded-top.jpg&quot;</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.box</span> h2 <span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;img/rounded-top.jpg&quot;</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.box</span> p <span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h5>优点：</h5>
<p>用最少的标记和图片实现了圆角效果（CSS好点的童鞋只要一个图片就可以搞定了）。</p>
<h5>缺点：</h5>
<p>不够灵活，每次更改框架的宽度都要更新背景图片。</p>
<h4>2.灵活的圆角</h4>
<p>为了能让圆角更加灵活，我们可以把容器里的每个圆角都做成一个独立的元素，通过CSS强行定位到父容器的四个角上。在这个方法里，我们只需要用到右边这个图片。<img src="http://i1001.photobucket.com/albums/af131/bolo1988/rounded-corner-sprite.jpg" alt="rounded-corner-sprite" class="alignright" /></p>
<h5>HTML:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;crnr tl&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;crnr tr&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>This is a heading<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
Lorem ipsum dolor sit amet,
 consectetur adipiscing elit. Sed
vehicula ligula eu diam tincidunt
fermentum. Curabitur facilisis
enim non libero cursus eu varius
enim suscipit. Ut venenatis
vehicula lorem ut hendrerit. Ut
adipiscing augue sed ante volutpat
eget ornare erat facilisis. In hac
habitasse platea dictumst.
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;crnr bl&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;crnr br&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<h5>CSS:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
        <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.crnr</span> <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">&quot;img/crnr-sprite.jpg&quot;</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
       <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.tl</span> <span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #00AA00;">;</span>
     <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.tr</span> <span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-25px</span> <span style="color: #cc66cc;">0</span> <span style="color: #00AA00;">;</span>
     <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.bl</span> <span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">-25px</span> <span style="color: #00AA00;">;</span>
     <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.br</span> <span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-25px</span> <span style="color: #933;">-25px</span> <span style="color: #00AA00;">;</span>
     <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h5>优点：</h5>
<p>能自动适应父容器的大小，浏览器兼容性非常强。</p>
<h5>缺点：</h5>
<p>为了实现圆角，添加了一些没有意义的标记。</p>
<h3>用CSS3创建圆角的方法</h3>
<h4>1.Border radius（边界半径）</h4>
<p>W3C建议已经建议为boder增加几个属性选项，其中之一就是border-radius，目前这个属性已经被Firefox和Safari 3(Chrome)支持，比较遗憾的是IE不支持此属性。</p>
<h5>HTML:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>This is a heading<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
Lorem ipsum dolor sit amet,
 consectetur adipiscing elit. Sed
vehicula ligula eu diam tincidunt
fermentum. Curabitur facilisis
enim non libero cursus eu varius
enim suscipit. Ut venenatis
vehicula lorem ut hendrerit. Ut
adipiscing augue sed ante volutpat
eget ornare erat facilisis. In hac
habitasse platea dictumst.
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<h5>CSS:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#EE2E24</span><span style="color: #00AA00;">;</span>
        -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
	-webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
        border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h5>优点：</h5>
<p>这种方法不需要添加任何多余的标记，而且可以非常方便睇修改圆角的半径，是最易上手的方法。</p>
<h5>缺点：</h5>
<p>虽然符合W3C的标准，但缺乏浏览器支持。</p>
<h4>2.多个背景图片</h4>
<p>CSS3对background属性做出了一些重大改进，在CSS3标准里，我们可以把多个背景图片应用到同一个元素里。我们利用这个特性为容器加上圆角效果。HTML代码与上例保持一致，在CSS中需要先把四个圆角的图片分别做好</p>
<h5>CSS:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">top-left.gif</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">,</span>
        <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">top-right.jpg</span><span style="color: #00AA00;">&#41;</span>  <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">,</span>
        <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">bottom-left.gif</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">bottom</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">,</span>
        <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">bottom-right.gif</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">bottom</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">repeat-y</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h5>优点：</h5>
<p>没有多余的标记，可以分别设置每个圆角的半径。</p>
<h5>缺点：</h5>
<p>使用了W3C未明确定义的方法，浏览器兼容性很差，目前只有Safari和Chrome对此方法支持良好。</p>
<h4>3.Border Image(图形边界)</h4>
<p><img src="http://i1001.photobucket.com/albums/af131/bolo1988/border-image.jpg" alt="border-image" class="alignright"/>使用图片作为border将是未来使用最广泛的CSS属性之一，这个属性是对border-style的升级。此属性允许您切片成的规则的9个不同部分的图像，然后在浏览器便会作为border的基本形状来进行渲染。如右图，这种技术称为九切片缩放，可以避免圆角出现失真。我们不需要手动切开图片，CSS能帮我们实现切片功能。</p>
<h5>CSS:</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
        -webkit-border-image <span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span> <span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">box.jpg</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;">20%</span> <span style="color: #933;">20%</span> <span style="color: #933;">20%</span> <span style="color: #933;">20%</span> round round<span style="color: #00AA00;">;</span>
        -moz-border-image <span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span> <span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">box.jpg</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;">20%</span> <span style="color: #933;">20%</span> <span style="color: #933;">20%</span> <span style="color: #933;">20%</span> round round<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h5>优点：</h5>
<p>不需要添加多余的标记，使用非常简单，而且可以自由改变每个圆角的半径。</p>
<h5>缺点：</h5>
<p>W3C并为确定这个属性的使用方法，目前只可以用浏览器的私有属性实现，浏览器兼容性较差。目前支持这个方法的游览器有Firefox，Chrome，Safari，Opera。</p>
<h3>后记：</h3>
<p>网页圆角的实现方法是目前争议较大的一个话题。还有一个通过由若干个不同宽度的Div组合做出圆角的方法，该方法对浏览器的兼容性是最强的，由于篇幅所限，没有提及。虽然本文提及的方法都有明显的缺点，但在浏览器更新越来越快的今天，我们可以预见旧式浏览器将很快被淘汰，权当是对未来CSS写法的预习吧。</p>
<p>原文：<a href="http://www.inspiredcss.com/tutorials/creating-rounded-corners-with-css2-and-css3" rel="external">Creating rounded corners with CSS2 &#038; CSS3</a>。有删改。</p>
<p  class="related_post_title">Related Posts</p><ul class="related_post"><li><a href="http://blog.imbolo.com/css-turn-off-webkit-browsers-input-highlight-and-textarea-resize/" title="CSS 屏蔽 Webkit 浏览器 input 高亮和 textarea 缩放">CSS 屏蔽 Webkit 浏览器 input 高亮和 textarea 缩放</a></li><li><a href="http://blog.imbolo.com/jquery-selectors/" title="jQuery选择器详解（一）">jQuery选择器详解（一）</a></li><li><a href="http://blog.imbolo.com/customizing-the-new-wordpress-3-0-custom-menus-adding-different-icons-to-each-link/" title="为 WordPress 3.0 的自定义菜单链接添加栏目图标">为 WordPress 3.0 的自定义菜单链接添加栏目图标</a></li><li><a href="http://blog.imbolo.com/center-multiple-divs-with-css/" title="CSS 多个子框架居中">CSS 多个子框架居中</a></li><li><a href="http://blog.imbolo.com/the-css3-flexible-box-model-2/" title="CSS3 灵活的盒子模型(Flexible Box Module) – 2">CSS3 灵活的盒子模型(Flexible Box Module) – 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.imbolo.com/creating-rounded-corners-with-css/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>能通过W3C的新窗口打开链接方法</title>
		<link>http://blog.imbolo.com/open-a-new-window-valid-w3c/</link>
		<comments>http://blog.imbolo.com/open-a-new-window-valid-w3c/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 06:07:19 +0000</pubDate>
		<dc:creator>bolo</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[W3C]]></category>

		<guid isPermaLink="false">http://digau.cn/?p=453</guid>
		<description><![CDATA[我们要在新窗口中打开链接通常的做法是在链接后面加target="_blank"，我们采用过渡型的DOCTYPE(xh tml1-transitional. dtd)时没有问题，但是当我们使用严格的DOCTYPE(xhtml1-strict.dtd)时，这个方法将通不过W3C的校验]]></description>
			<content:encoded><![CDATA[<p>我们要在新窗口中打开链接通常的做法是在链接后面加target=&#8221;_blank&#8221;，我们采用过渡型的DOCTYPE(xh tml1-transitional. dtd)时没有问题，但是当我们使用严格的DOCTYPE(xhtml1-strict.dtd)时，这个方法将通不过W3C的校验，会出现如下错误提示：<br />
<strong><em>&#8220;there is no attribute target for this element(in this HTML version)&#8221;</em></strong><br />
原来在HTML4.01/XHTML1.0/XHTML1.1严格DOCTYPE下，target=&#8221;_blank&#8221;、target=&#8221;_self&#8221;等等语法都是无效的，我们只能通过JavaScript来变通实现。</p>
<p>有朋友问为什么不允许使用target=&#8221;_blank&#8221;？这个属性很方便啊。呵呵，不知道W3C的专家们是怎么想的，据我所知，主要是“易用性、友好性”的问题，因为老外觉得不经过用户同意，没有明确提示就打开一个新窗口是不礼貌的。先不管这个取消是否合理，我们来看看解决办法。<br />
rel属性</p>
<p>HTML4.0增加了一个新属性：rel，这个属性用来说明链接和包含此链接页面的关系，以及链接打开的目标。rel有许多的属性值，比如 next、previous,、chapter、section等等。我们要使用的就是rel=&#8221;externa l&#8221;属性。原来这样写的代码：<br />
<span id="more-453"></span></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;</span>a href=<span style="color:#996600;">&quot;document.html&quot;</span> target=<span style="color:#996600;">&quot;_blank&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span> 打开一个新窗口<span style="color:#006600; font-weight:bold;">&lt;/</span>a<span style="color:#006600; font-weight:bold;">&gt;</span></pre></div></div>

<p>现在要写成这样：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;</span>a href=<span style="color:#996600;">&quot;document.html&quot;</span> rel=<span style="color:#996600;">&quot;external&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>打开一个新窗口<span style="color:#006600; font-weight:bold;">&lt;/</span>a<span style="color:#006600; font-weight:bold;">&gt;</span></pre></div></div>

<p>这是符合strict标准的方法。当然还必须配合一个javascript才有效。<br />
javascript</p>
<p>完整的代码JS如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    function externallinks<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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>document.<span style="color: #006633;">getElementsByTagName</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>    
     var anchors <span style="color: #339933;">=</span> document.<span style="color: #006633;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
     <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>var i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>anchors .<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
       var anchor <span style="color: #339933;">=</span> anchors<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>    
       <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>anchor.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>    
           anchor.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rel&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;external&quot;</span><span style="color: #009900;">&#41;</span>    
         anchor.<span style="color: #006633;">target</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;_blank&quot;</span><span style="color: #339933;">;</span>    
     <span style="color: #009900;">&#125;</span>    
    <span style="color: #009900;">&#125;</span>    
    window.<span style="color: #006633;">onload</span> <span style="color: #339933;">=</span> externallinks<span style="color: #339933;">;</span></pre></div></div>

<p>你可以把它保存成一个.js文件(比如external.js)，然后通过外部联接方法调用：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;external.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>其实这个方法依然用了blank属性，只不过用了偷龙转凤的方法而已。 </anchors></pre>
<p  class="related_post_title">Related Posts</p><ul class="related_post"><li><a href="http://blog.imbolo.com/creating-rounded-corners-with-css/" title="再谈CSS圆角">再谈CSS圆角</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.imbolo.com/open-a-new-window-valid-w3c/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

