<?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; textarea</title>
	<atom:link href="http://www.qifendi.com/html/ytag/textarea/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>textarea的scrollHeight</title>
		<link>http://www.qifendi.com/html/y2009/scrollheight-the-textarea.html</link>
		<comments>http://www.qifendi.com/html/y2009/scrollheight-the-textarea.html#comments</comments>
		<pubDate>Sun, 31 May 2009 08:36:39 +0000</pubDate>
		<dc:creator>soncy</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[scrollHeight]]></category>
		<category><![CDATA[textarea]]></category>

		<guid isPermaLink="false">http://www.eyike.com/?p=282</guid>
		<description><![CDATA[在写昨天那篇日志《<a href="http://www.eyike.com/html/y2009/javascript-delete-element-node.html" target="_blank">javascript删除元素节点</a>》时，用自己修改的runcode插入了一段代码，用的是[runcode][runcode]的方式，按照我插件的实现方式，[runcode]标签没有参数时，他的高度应该是根据内容自适应的才是，用的方法是取得&#60;textarea&#62;的scroll[......]<p class='read-more'><a href='http://www.qifendi.com/html/y2009/scrollheight-the-textarea.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>在写昨天那篇日志《<a href="http://www.eyike.com/html/y2009/javascript-delete-element-node.html" target="_blank">javascript删除元素节点</a>》时，用自己修改的runcode插入了一段代码，用的是<span class="codeBg">[runcode][runcode]</span>的方式，按照我插件的实现方式，<span class="codeBg">[runcode]</span>标签没有参数时，他的高度应该是根据内容自适应的才是，用的方法是取得<span class="codeBg">&lt;textarea&gt;</span>的scrollHeight然后赋值给他的height，但是在firefox下却没有得到我想要的表现，发现他的高度始终是100px，除非<span class="codeBg">&lt;textarea&gt;</span>中的内容高度超过了100px，scrollHeight的值才会改变，试着运行下面的代码：</p>
<div class="runcode">
<p><textarea name="runcode" style="overflow-y:visible;width:600px;font-size:12px" class="runcode_text" id="runcode_QHSlXo">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;EN&quot; lang=&quot;EN&quot;&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
#xx{height:80px;font-size:12px;width:300px;}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;textarea id=&quot;xx&quot;&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var textareaObj = new (function(){
	this.textarea = document.getElementById(&quot;xx&quot;);
	this.xx = function(){
		this.textarea.style.height = this.textarea.scrollHeight + &quot;px&quot;;
	}
	this.checkHeight = function(){
        alert(&quot;高度是&quot;+this.textarea.scrollHeight + &quot;px&quot;);
	}
})();
window.onload = function(){
	textareaObj.xx();
}
&lt;/script&gt;
&lt;/textarea&gt;
&lt;button onclick=&quot;textareaObj.checkHeight()&quot;&gt;点击计算高度&lt;/button&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var textareaObj = new (function(){
	this.textarea = document.getElementById(&quot;xx&quot;);
	this.xx = function(){
		this.textarea.style.height = this.textarea.scrollHeight + &quot;px&quot;;
	}
	this.checkHeight = function(){
                          alert(&quot;高度是&quot;+this.textarea.scrollHeight + &quot;px&quot;);
	}
})();
window.onload = function(){
	textareaObj.xx();
}
&lt;/script&gt;
&lt;/html&gt;
</textarea></p>
<script type="text/javascript">function changeTsize(){document.getElementById("runcode_QHSlXo").style.height = document.getElementById("runcode_QHSlXo").scrollHeight + "px";}window.setTimeout(changeTsize,0);</script><p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_QHSlXo');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_QHSlXo');"/> <input type="button" value="另存代码" class="runcode_button" onclick="saveCode('runcode_QHSlXo','runcode_QHSlXo');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<div class="runcode">
<p><textarea name="runcode" style="overflow-y:visible;width:600px;font-size:12px" class="runcode_text" id="runcode_a0TM0H">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;EN&quot; lang=&quot;EN&quot;&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
#xx{height:80px;font-size:12px;width:300px;}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;textarea id=&quot;xx&quot;&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/textarea&gt;
&lt;button onclick=&quot;textareaObj.checkHeight()&quot;&gt;点击计算高度&lt;/button&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var textareaObj = new (function(){
	this.textarea = document.getElementById(&quot;xx&quot;);
	this.xx = function(){
		this.textarea.style.height = this.textarea.scrollHeight + &quot;px&quot;;
	}
	this.checkHeight = function(){
                          alert(&quot;高度是&quot;+this.textarea.scrollHeight + &quot;px&quot;);
	}
})();
window.onload = function(){
	textareaObj.xx();
}
&lt;/script&gt;
&lt;/html&gt;</textarea></p>
<script type="text/javascript">function changeTsize(){document.getElementById("runcode_a0TM0H").style.height = document.getElementById("runcode_a0TM0H").scrollHeight + "px";}window.setTimeout(changeTsize,0);</script><p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_a0TM0H');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_a0TM0H');"/> <input type="button" value="另存代码" class="runcode_button" onclick="saveCode('runcode_a0TM0H','runcode_a0TM0H');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<p>第一段代码中<span class="codeBg">&lt;textarea&gt;</span>中的代码是</p>

<div class="wp_codebox"><table><tr id="p2822"><td class="code" id="p282code2"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> textareaObj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">textarea</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;xx&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">xx</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">textarea</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">textarea</span>.<span style="color: #660066;">scrollHeight</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checkHeight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;高度是&quot;</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">textarea</span>.<span style="color: #660066;">scrollHeight</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	textareaObj.<span style="color: #660066;">xx</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;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>这段内容高度超过了80px，所以firefox下得到的高度是210px，但是第二段代码中，<span class="codeBg">&lt;textarea&gt;</span>中的代码只有<span class="codeBg">&lt;div&gt;&lt;/div&gt;</span>，但得到的高度却是80px，正好是在css中定义的，<span class="codeBg">&lt;textarea&gt;</span>的高度，只要把css中定义的高度去掉，就能得到文本的高度了，您可以修改代码然后运行试试。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.qifendi.com/html/y2009/scrollheight-the-textarea.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: qifendi.com @ 2010-09-10 20:15:55 by W3 Total Cache -->