<?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>七分地 &#187; iframe自适应</title>
	<atom:link href="http://www.qifendi.com/html/ytag/iframe%e8%87%aa%e9%80%82%e5%ba%94/feed" rel="self" type="application/rss+xml" />
	<link>http://www.qifendi.com</link>
	<description>soncy的七分责任田，种点瓜果蔬菜和杂草</description>
	<lastBuildDate>Sun, 22 Aug 2010 13:25:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>iframe的高度自适应</title>
		<link>http://www.qifendi.com/html/y2009/iframe-height-adaptive.html</link>
		<comments>http://www.qifendi.com/html/y2009/iframe-height-adaptive.html#comments</comments>
		<pubDate>Mon, 18 May 2009 11:11:54 +0000</pubDate>
		<dc:creator>soncy</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[iframe自适应]]></category>

		<guid isPermaLink="false">http://www.eyike.com/?p=60</guid>
		<description><![CDATA[iframe不像普通的div或者span等元素默认高度是auto，在iframe嵌套一个页面时，如果不指定iframe的高度，那么肯定是得不到想要的结果的，但是如果被嵌套的页面（以下称子页面）的内容是不固定的呢？这个时候如果再写死iframe的高度就不靠谱了，当子页面的内容变化，比如变多，子页面的高[......]<p class='read-more'><a href='http://www.qifendi.com/html/y2009/iframe-height-adaptive.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>iframe不像普通的div或者span等元素默认高度是auto，在iframe嵌套一个页面时，如果不指定iframe的高度，那么肯定是得不到想要的结果的，但是如果被嵌套的页面（以下称子页面）的内容是不固定的呢？这个时候如果再写死iframe的高度就不靠谱了，当子页面的内容变化，比如变多，子页面的高度就会增加，在嵌套页面（以下称父页面）中就只会看到一部分，如果子页面内容变少，高度变低，那么就会看到一大片空白，很不爽。</p>
<p>这个时候就有必要考虑iframe的高度自适应了，原理很简单，就是得到子页面的高度，再把高度赋给iframe。首先在父页面定义一个函数，用于指定iframe的高度设置。</p>

<div class="wp_codebox"><table><tr id="p604"><td class="line_numbers"><pre>0
1
2
3
4
5
</pre></td><td class="code" id="p60code4"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> setIfrmHeight<span style="color: #009900;">&#40;</span>_iframid<span style="color: #339933;">,</span>_height<span style="color: #339933;">,</span>_visibility<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>_visibility<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span>_iframid<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> _visibility<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	$<span style="color: #009900;">&#40;</span>_iframid<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> _height <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这个函数接收三个参数，分别为iframe的id，要设置的高度和是否显示。<br />
接下来在子页面调用这个函数就可以了，在子页面需要取得页面高度和当页面被哪个iframe调用，页面高度可以用document.body.offsetHeight得到，iframe的id可以通过iframe的src传进去，如</p>

<div class="wp_codebox"><table><tr id="p605"><td class="line_numbers"><pre>0
</pre></td><td class="code" id="p60code5"><pre class="html" style="font-family:monospace;">&lt;iframe id=&quot;piframe&quot; src=&quot;aa.html?frmid=piframe&quot; height=&quot;0&quot; width=&quot;100%&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;</pre></td></tr></table></div>

<p>在子页面中，就可以这样调用了</p>

<div class="wp_codebox"><table><tr id="p606"><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p60code6"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> QueryString<span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span>  
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> uri <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">search</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> re <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">+</span>val<span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=([^&amp;?]*)&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;ig&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>uri.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span><span style="color: #009900;">&#40;</span>uri.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>val.<span style="color: #660066;">length</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> _bodyHeight <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">offsetHeight</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> _iframeId <span style="color: #339933;">=</span> QueryString<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;frmid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> _visibility <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>_bodyHeight <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	_visibility <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
window.<span style="color: #660066;">parent</span>.<span style="color: #660066;">setIfrmHeight</span><span style="color: #009900;">&#40;</span>_iframeId<span style="color: #339933;">,</span>_bodyHeight<span style="color: #339933;">,</span>_visibility<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>其中QueryString()函数是负责取得url中的对应字段的。<br />
使用这种方法的时候有两个注意事项，一是子页面和父页面必须在同一个域下，二是子页面必须有DOCTYPE的声明。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.qifendi.com/html/y2009/iframe-height-adaptive.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: qifendi.com @ 2010-09-08 08:03:33 by W3 Total Cache -->