<?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>Silentash-默尘 &#187; 置换元素</title>
	<atom:link href="http://blog.silentash.com/tag/%e7%bd%ae%e6%8d%a2%e5%85%83%e7%b4%a0/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.silentash.com</link>
	<description>尘埃默默，风雨洗礼</description>
	<lastBuildDate>Tue, 24 Aug 2010 16:31:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>置换元素在IE浏览器中的疑难杂症</title>
		<link>http://blog.silentash.com/2010/01/problem-of-replaced-element-in-ie/?fuckgfw</link>
		<comments>http://blog.silentash.com/2010/01/problem-of-replaced-element-in-ie/?fuckgfw#comments</comments>
		<pubDate>Sat, 30 Jan 2010 04:01:42 +0000</pubDate>
		<dc:creator>nunumick</dc:creator>
				<category><![CDATA[Browser Bug]]></category>
		<category><![CDATA[XHTML/CSS]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[display]]></category>
		<category><![CDATA[hasLayout]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[replaced element]]></category>
		<category><![CDATA[置换元素]]></category>

		<guid isPermaLink="false">http://blog.silentash.com/?p=490</guid>
		<description><![CDATA[<a href="http://reference.sitepoint.com/css/replacedelements" target="_blank">置换元素</a>在 IE 浏览器中有不少问题，比方说<a href="http://blog.silentash.com/2008/06/css-replaced-element/" target="_blank">置换元素的行高</a>就是其中难题之一，当然还有 form 元素 <a href="http://blog.silentash.com/2009/01/ie-margin-inherit-bug/" target="_blank">textarea 的边距继承 bug</a>，OK，今天再介绍几个，凑满一箩筐]]></description>
			<content:encoded><![CDATA[<p><a href="http://reference.sitepoint.com/css/replacedelements" target="_blank">置换元素</a>在 IE 浏览器中有不少问题，比方说<a href="http://blog.silentash.com/2008/06/css-replaced-element/" target="_blank">置换元素的行高</a>就是其中难题之一，当然还有 form 元素 <a href="http://blog.silentash.com/2009/01/ie-margin-inherit-bug/" target="_blank">textarea 的边距继承 bug</a>，OK，今天再介绍几个，凑满一箩筐。</p>
<h3>置换元素与A标签</h3>
<p><strong>场景：</strong><br />
   IE6，A标签内的图片（置换元素），默认隐藏，hover显示</p>
<p><strong>html：</strong></p>
<pre class="chili"><code class="html">   &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;&quot; /&gt;
   &lt;/a&gt;
</code></pre>
<p><strong>css:</strong></p>
<pre class="chili"><code class="css">   a img{display:none}/*使用visibility也行*/
   a:hover img{display:block;}
</code></pre>
<p><strong>现象：</strong><br />
ie6,hover后无法正常显示 a 标签内的图片</p>
<p><strong>解决方法：</strong></p>
<ol>
<li>hover 时触发 a 标签的 hasLayout
<pre class="chili"><code class="css">   a:hover{
      _zoom:1;
   }
</code></pre>
</li>
<li>使用 JS 脚本添加 class</li>
</ol>
<p><strong>Demo：</strong><a href="http://www.silentash.com/lab/201001/replaced_ele.html#hover" target="_blank" >demo1</a></p>
<h3>置换元素与position</h3>
<p>在前面的基础上给 img 加上容器，设置position，默认隐藏，hover 显示</p>
<p><strong>html:</strong></p>
<pre class="chili"><code class="html">   &lt;a href=&quot;#&quot;&gt;
      &lt;span&gt;
         &lt;img src=&quot;&quot; /&gt;
      &lt;/span&gt;
   &lt;/a&gt;
</code></pre>
<p><strong>css:</strong></p>
<pre class="chili"><code class="css">   a{position:relative}
   a span{position:absolute;display:none;}
   a:hover span{display:block;}
</code></pre>
<p><strong>现象：</strong><br />
ie6,hover后无法正常显示 a 标签内的图片，触发其 hasLayout 可以显示，但无法再次隐藏</p>
<p><strong>Demo：</strong><a href="http://www.silentash.com/lab/201001/replaced_ele.html#pos" target="_blank" >demo2</a></p>
<p><strong>解决方法：</strong></p>
<ol>
<li>使用visibility代替display</li>
<li>清除span元素的position</li>
<li>使用脚本</li>
</ol>
<p><strong>一个更极端的例子：</strong><a href="http://www.justarrangingbits.org/demo/2/" target="_blank" >The IE &#8216;non-disappearing content&#8217; bug</a></p>
<h3>图片与hasLayout</h3>
<p>同样的例子，如果 img 的容器 span 元素被触发 hasLayout 则会导致 a 链接在 IE6/7 浏览器下失效（图片区域）</p>
<p><strong>html:</strong></p>
<pre class="chili"><code class="html">   &lt;a href=&quot;#&quot;&gt;
      &lt;span&gt;
         &lt;img src=&quot;&quot; /&gt;
      &lt;/span&gt;
   &lt;/a&gt;
</code></pre>
<p><strong>css:</strong></p>
<pre class="chili"><code class="css">   a span{display:inline-block}/*float,absolute,zoom都行*/
</code></pre>
<p><strong>现象:</strong><br />
图片区域链接无效</p>
<p><strong>Demo：</strong><a href="http://www.silentash.com/lab/201001/replaced_ele.html#float" target="_blank" >demo3</a></p>
<p><strong>解决方法：</strong></p>
<ol>
<li>还原span容器的hasLayout</li>
<li>尽量不要采用此类布局</li>
</ol>
<hr /><h2>Comments</h2><ul><li><a href="http://blog.silentash.com/2010/01/problem-of-replaced-element-in-ie/?fuckgfw#comment-209">2010年01月31日</a>, <a href='http://www.rainoina.com' rel='external nofollow' class='url'>rainoxu</a> writes: <a href="#" rel="nofollow"> 
    
       
    
</a> 

这个之前我也遇到过，减少嵌套，合理嵌套是更好的解决方式。

p.s:要是能附上上面这些问题出现的原因，那就更好了：）</li></ul><hr /><small>Copyright &copy; 2008-2010<br /> This feed is for personal, non-commercial use only. <br /> </small>]]></content:encoded>
			<wfw:commentRss>http://blog.silentash.com/2010/01/problem-of-replaced-element-in-ie/?fuckgfw/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>也说置换元素引起的行高BUG</title>
		<link>http://blog.silentash.com/2008/06/css-replaced-element/?fuckgfw</link>
		<comments>http://blog.silentash.com/2008/06/css-replaced-element/?fuckgfw#comments</comments>
		<pubDate>Fri, 06 Jun 2008 01:27:49 +0000</pubDate>
		<dc:creator>nunumick</dc:creator>
				<category><![CDATA[Browser Bug]]></category>
		<category><![CDATA[XHTML/CSS]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[line-height]]></category>
		<category><![CDATA[non-replaced]]></category>
		<category><![CDATA[replaced]]></category>
		<category><![CDATA[置换元素]]></category>
		<category><![CDATA[行高]]></category>

		<guid isPermaLink="false">http://mick/project/Php/silentash/?p=55</guid>
		<description><![CDATA[首先说明此BUG在IE7已经被修复，不过鉴于目前IE6浏览器使用占有率还是非常之高，还是很有必要了解下这个“臭虫”！而且我写这篇文章的主要目的是为了阐述另一个结论……
]]></description>
			<content:encoded><![CDATA[<p>首先说明此BUG在IE7已经被修复，不过鉴于目前IE6浏览器使用占有率还是非常之高，还是很有必要了解下这个“臭虫”！而且我写这篇文章的主要目的是为了阐述另一个结论，请看。<br/><br/><b>BUG的起因： </b>只要行内有置换元素，就会引起这个BUG。这里我们首先要知道的是什么是置换元素，在我前面转贴的文章中已经给出了W3C官方对于置换元素的解释：<b> 置换元素（replaced element）主要是指img，input，textarea，select，object等这类默认就有CSS格式化外表范围的元素。</b><br/><br/><b>我们再来看看这个BUG在万恶的IE6中的表现：</b><br/>拿最普遍的表单输入做一个例子<br/>
<pre class="chili"><code class="html">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;head&gt;
    &lt;style&gt;
      /*为了让BUG更加直观，这里设置的行高要大一些*/
      body,p{margin:0;line-height:50px;font-size:12px;background-color:#f1f1f1;}
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;!-//随手编的表单--&gt;
    &lt;p style=&quot;padding-left:30px;border:1px solid #f00;&quot;&gt;
        这是一个多行的例子，行高50px，在IE7，FF,OPERA下面显示的是正常
        &lt;br /&gt;
        请注意看IE6以下浏览器的表现
        &lt;br /&gt;
        姓名:&lt;input type=&quot;text&quot; size=&quot;15&quot; /&gt;&lt;span&gt;这里我们放入的是input这个置换元素&lt;/span&gt;
        &lt;br /&gt;
        提示1：请输入真实姓名
        &lt;br /&gt;
        城市:&lt;select&gt;&lt;option&gt;杭州市&lt;/option&gt;&lt;/select&gt;
        &lt;br /&gt;
        提示2：请如实选择你的所在城市
    &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><br/><b>FF,IE7截图<图1></b><br/><img src="http://www.silentash.com/uploads/200806/lhbug1.gif" alt="" title=""/><br/><b>IE6截图<图2></b><br/><img src="http://www.silentash.com/uploads/200806/lhbugie6.gif" alt="" title=""/><br/><br/>对比下就知道，在IE6下，置换元素会引起行距错乱。下面一张是IE6中接连两行都有置换元素时的截图<br/><b><图3></b><br/><img src="http://www.silentash.com/uploads/200806/lhbug2ie6.gif" alt="" title=""/><br/>可以看到这两行已经没有上下间距了。<br/>这里打住，首先让我们提两个概念：line-height和font-size，行高和字体大小。line-height减去font-size称之为“间距（leading）”。间距的一半，称之为“半间距”。而这个“半间距”会被加在每行文字的上面和下面，于是行与行之间的空隙（有上一段下面的半间距 和 下一段上面的半间距相加而成）就出来了。在本例中，行高是50PX，字体大小是16PX，那行间距应该是50-12＝38PX；半间距就是19PX;<br/><img src="http://www.silentash.com/uploads/200806/ie7road65.gif" alt="" title=""/><br/>看了上面的解释，相信大家对行高和间距的定义都有了正确的理解，那么我们来看看是什么原因导致万恶的IE6出现了如此奇怪的BUG，其实到目前为止都没有一个官方的解释，网上的解释都是我们自己瞎猜的，有的说置换元素使上下相邻行间距减半，有的说带有置换元素的那行文字的上下半间距，和相邻的上下两行的下上半间距合并到了一起，使间距减半。但是我认为真正的原因并不是减半这么简单，<b> 综合<图2>和<图3>，可以很明显的看出置换元素并没有受到line-height的影响，就是说置换元素和其他行的间距应该为0，其实是没有间距的，而不是象网上所说的间距减半（如果间距减半那就说明自己还有间距）那样有自己的间距。</b>请再仔细分析这2张截图，应该不难得出和我一样的结论。<br/><br/><b>BUG修复的方法：</b>我决定自从我知道了真正的BUG原因之后，抛开什么减半和合并或者重叠的说法，我对如何去修复也终于是理解的更透彻了，既然置换元素img,input,select等对line-height不感冒，那我们计算出他们的行半间距，以”margin:半间矩PX 0；“的方式为这些固执的置换元素加上就好了，不过要搞清楚的是不能以“(行高-字体大小)/2”来计算半间矩，应该以&#8221;(行高-置换元素高度)/2“来计算。据说现在也只有这么一个不是办法的办法=.=!<br/><br/><b>我的结论：</b><font color="Red"> IE6下，line-height对带有置换元素的整行文字不起作用，应该是撤撤底底的无效，换句话说置换元素是没有行间距的。这就是我今天要阐述的结论。</font>因此在进行前端设计的时候要考虑到这一点，非要用line-height来设置行高的话，需要为ie6进行HACK，HACK的方法是：<br/>
<pre class="chili"><code class="css">*html input,img,select{margin:计算得到的半间距px 0;vertical-align:middle;}</code></pre>
<p><br/>vertical-align是为了让垂直位置更准确居中，象input,img,select这些都是置换内联元素，受到vertical-align的影响。</p>
<hr /><h2>Comments</h2><ul><li><a href="http://blog.silentash.com/2008/06/css-replaced-element/?fuckgfw#comment-18">2009年08月18日</a>, ade writes: 解释的真清楚,终于弄明白了,谢谢分享
</li><li><a href="http://blog.silentash.com/2008/06/css-replaced-element/?fuckgfw#comment-208">2010年01月30日</a>, <a href='http://blog.silentash.com/2010/01/problem-of-replaced-element-in-ie/' rel='external nofollow' class='url'>Silentash-默尘 &raquo; 置换元素在IE浏览器中的疑难杂症</a> writes: [...] IE 浏览器中有不少问题，比方说置换元素的行高就是其中难题之一，当然还有 form 元素 textarea 的边距继承 [...]</li></ul><hr /><small>Copyright &copy; 2008-2010<br /> This feed is for personal, non-commercial use only. <br /> </small>]]></content:encoded>
			<wfw:commentRss>http://blog.silentash.com/2008/06/css-replaced-element/?fuckgfw/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
