<?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; 文本</title>
	<atom:link href="http://julabs.me/blog/tags/%e6%96%87%e6%9c%ac/feed/" rel="self" type="application/rss+xml" />
	<link>http://julabs.me/blog</link>
	<description>想努力创造完美的东西，必须具备心灵的纯洁，同时富于宗教精神。</description>
	<lastBuildDate>Wed, 18 Jan 2012 06:34:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>《精通JavaScript》笔记——获取文本内容的通用函数</title>
		<link>http://julabs.me/blog/front/function-for-get-text-in-dom/</link>
		<comments>http://julabs.me/blog/front/function-for-get-text-in-dom/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 09:35:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[前端]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[文本]]></category>

		<guid isPermaLink="false">http://julabs.me/blog/?p=93</guid>
		<description><![CDATA[以前写过一篇博文《获取元素文本内容》，但功能过于简单，它只能获取元素自身所包含的文本内容，而对于它的子元素内的文本内容就无能为力了。今天在看《精通JavaScript》时看到了一个更好的方法，不仅能兼容各种浏览器，而且还可以提取所有子元素内的文本内容。代码如下： function getText(e){ var t=&#34;&#34;; // 如果传入的是元素，则继续遍历其子元素， // 否则假定它是一个数组 e=e.childNodes &#124;&#124; e; for(var j=0;j&#60;e.length;j++){ // 如果不是元素，追加其文本值 // 否则，递归遍历所有元素的子节点 t+=e[j].nodeType!=1? e[j].nodeValue:getText(e[j].childNodes); } // 返回匹配的文本 return t; } } 这样你就能拥有一个能获取任何元素文本内容的兼容方法，无论是在XHTML还是在XML文档中都能使用。]]></description>
			<content:encoded><![CDATA[<p>以前写过一篇博文<a href="/blog/front/get-text-value/" target="_blank">《获取元素文本内容》</a>，但功能过于简单，它只能获取元素自身所包含的文本内容，而对于它的子元素内的文本内容就无能为力了。今天在看《精通JavaScript》时看到了一个更好的方法，不仅能兼容各种浏览器，而且还可以提取所有子元素内的文本内容。代码如下：</p>
<pre><code class="javascript">function getText(e){
	var t=&quot;&quot;;

		// 如果传入的是元素，则继续遍历其子元素，
		// 否则假定它是一个数组
		e=e.childNodes || e;

		for(var j=0;j&lt;e.length;j++){
			// 如果不是元素，追加其文本值
			// 否则，递归遍历所有元素的子节点
			t+=e[j].nodeType!=1?
			e[j].nodeValue:getText(e[j].childNodes);
		}

		// 返回匹配的文本
		return t;
	}
}
</code>
</pre>
<p>这样你就能拥有一个能获取任何元素文本内容的兼容方法，无论是在<strong>XHTML</strong>还是在<strong>XML</strong>文档中都能使用。</p>
]]></content:encoded>
			<wfw:commentRss>http://julabs.me/blog/front/function-for-get-text-in-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>获取元素文本内容</title>
		<link>http://julabs.me/blog/front/get-text-value/</link>
		<comments>http://julabs.me/blog/front/get-text-value/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 07:41:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[前端]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[文本]]></category>

		<guid isPermaLink="false">http://julabs.me/blog/?p=13</guid>
		<description><![CDATA[要获得一个元素的文本内容，简单地可以用以下代码： elementContent = bodyElement.firstChild.nodeValue; 但如果一个元素中有多行文本，如: 第一行文本 第二行文本 第三行文本 第四行文本 第五行文本 用firstChild.nodeValue的方法只能取得第一行的文本内容，如果要想获得元素内所有的文本内容，在IE中就要用到innerText。火狐不支持innerText，但火狐有个与innerText相似的功能：textContent。一个简单通用代码如下： elementContent = bodyElement.innerText&#124;&#124;bodyElement.textContent; 这种方法在IE、火狐，谷歌浏览器以及Opera都能正常运行。]]></description>
			<content:encoded><![CDATA[<p>要获得一个元素的文本内容，简单地可以用以下代码：</p>
<pre><code class="javascript">elementContent = bodyElement.firstChild.nodeValue;</code></pre>
<p>但如果一个元素中有多行文本，如:</p>
<pre><code class="html">第一行文本
第二行文本
第三行文本
第四行文本
第五行文本</code></pre>
<p>用<code class="javascript">firstChild.nodeValue</code>的方法只能取得第一行的文本内容，如果要想获得元素内所有的文本内容，在<acronym title="Internet Explorer">IE</acronym>中就要用到<code class="javascript">innerText</code>。<a href="http://www.mozilla.com/firefox/" title="FireFox" target="_blank">火狐</a>不支持<code class="javascript">innerText</code>，但<a href="http://www.mozilla.com/firefox/" title="FireFox" target="_blank">火狐</a>有个与<code class="javascript">innerText</code>相似的功能：<code class="javascript">textContent</code>。一个简单通用代码如下：</p>
<pre><code class="javascript">elementContent = bodyElement.innerText||bodyElement.textContent;</code></pre>
<p>这种方法在<acronym title="Internet Explorer">IE</acronym>、<a href="http://www.mozilla.com/firefox/" title="FireFox" target="_blank">火狐</a>，<a href="http://www.google.com/chrome/" title="chrome" target="_blank">谷歌浏览器</a>以及<a href="http://www.opera.com/" title="Opera" target="_blank">Opera</a>都能正常运行。</p>
]]></content:encoded>
			<wfw:commentRss>http://julabs.me/blog/front/get-text-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

