<?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; table</title>
	<atom:link href="http://zhiqiang.org/blog/tag/table/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>数学、金融、计算机</description>
	<lastBuildDate>Wed, 23 May 2012 08:37:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>innerHTML只读问题 &amp; 两个动态修改Table的js函数</title>
		<link>http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html</link>
		<comments>http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html#comments</comments>
		<pubDate>Wed, 25 Jul 2007 04:06:58 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[innerHTML]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[网页技术]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/627.html</guid>
		<description><![CDATA[博客 » IT技术 » innerHTML，javascript，table，网页技术 » 最近做社会实践项目遇到的一个问题，需要动态修改一个表格。本来以为要想修改一行，直接设置新的 tr.innerHTML即可。后来发现在Firefox下可行，但在IE下通不过，查看了一下帮助，才发现innerHTML还没有一个通用标准，而在IE下innerHTML对于标签为COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR的元素是只读的。要想...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href="http://zhiqiang.org/blog/tag/innerhtml" rel="tag">innerHTML</a>，<a href="http://zhiqiang.org/blog/tag/javascript" rel="tag">javascript</a>，<a href="http://zhiqiang.org/blog/tag/table" rel="tag">table</a>，<a href="http://zhiqiang.org/blog/tag/%e7%bd%91%e9%a1%b5%e6%8a%80%e6%9c%af" rel="tag">网页技术</a> » </p><p>最近做社会实践项目遇到的一个问题，需要动态修改一个表格。本来以为要想修改一行，直接设置新的 tr.innerHTML即可。后来发现在Firefox下可行，但在IE下通不过，查看了一下帮助，才发现innerHTML还没有一个通用标准，而在IE下<strong>innerHTML</strong>对于标签为COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR的元素是<strong>只读</strong>的。要想在IE下动态修改表格，只能使用insertRow和insertCell;</p>
<p>下面这个函数往表格<strong>i</strong>里添加一行，此行的HTML代码为<strong>t。</strong></p>
<blockquote><pre><span style="color: #008000">// insert a row with HTML code t to table i; </span>
<span style="color: #008000">// t: the full HTML code of the row</span>
<span style="color: #008000">// i : the table element</span>
<span style="color: #008000">// Return the row added</span>
<span style="color: #0000ff">function</span> addRow(t, i) {
	<span style="color: #0000ff">if</span> (<span style="color: #0000ff">typeof</span>(i) == "<span style="color: #8b0000">string</span>")	i = <span style="color: #0000ff">document</span>.getElementById(i);

	<span style="color: #0000ff">var</span> f = t.indexOf('&gt;'), l = t.lastIndexOf('&lt;/'),
		tag = t.substr(l+2, t.<span style="color: #0000ff">length</span>-l-3), e;

	<span style="color: #0000ff">if</span> (tag == 'tr') e = i.insertRow(-1);
	<span style="color: #0000ff">else</span> {
		e = <span style="color: #0000ff">document</span>.createElement(tag);
		i.appendChild(e, i);
		e.innerHTML = t.substr(f+1, l-f-1);
	}

	s = t.substr(1, f-1).split(' ');
	<span style="color: #0000ff">for</span> (i=0; i&lt;s.<span style="color: #0000ff">length</span>; i++) {
		ss = s[i].split('=');
		<span style="color: #0000ff">if</span> (ss.<span style="color: #0000ff">length</span>==1) <span style="color: #0000ff">continue</span>;
		e.setAttribute(ss[0], strip(ss[1]));
	}

	ti = t.substr(f+1, l-f-1);
	<span style="color: #0000ff">if</span> (t.substr(l+2, t.<span style="color: #0000ff">length</span>-l-3) == 'tr') {
		<span style="color: #0000ff">var</span> tf;
		<span style="color: #0000ff">while</span>( (tf = ti.indexOf("<span style="color: #8b0000">&lt;td&gt;</span>")) != -1) {
			ti = ti.substr(tf+4, ti.<span style="color: #0000ff">length</span>-tf-4);
			<span style="color: #0000ff">var</span> te = e.insertCell(-1);
			tl = ti.indexOf("<span style="color: #8b0000">&lt;/td&gt;</span>");
			te.innerHTML = ti.substr(0, tl);
			ti = ti.substr(tl, ti.<span style="color: #0000ff">length</span>-tl);
		}
	} 

	<span style="color: #0000ff">return</span> e;

	<span style="color: #0000ff">function</span> strip(ss) {
		<span style="color: #0000ff">if</span> (ss[0]=='\''|| ss[0] == "<span style="color: #8b0000">\"</span>")
			<span style="color: #0000ff">return</span> ss.substr(1, ss.<span style="color: #0000ff">length</span>-2);
	}
}</pre>
</blockquote>
<p>下面这个函数将表格的<strong>i</strong>行改写为代码<strong>t：</strong> </p>
<blockquote><pre><span style="color: #008000">// modify row i with row of HTML code t</span>
<span style="color: #008000">// t: HTML code </span>
<span style="color: #008000">// i: the row element, get by table.rows[row_index]</span>
<span style="color: #008000">// return the row modified</span>
<span style="color: #0000ff">function</span> modifyRow(t, i) {
	<span style="color: #0000ff">var</span> f = t.indexOf('&gt;'), l = t.lastIndexOf('&lt;/'),
		tag = t.substr(l+2, t.<span style="color: #0000ff">length</span>-l-3), e;

	<span style="color: #0000ff">if</span> (tag == 'tr') e = i;
	<span style="color: #0000ff">else</span> {
		e = <span style="color: #0000ff">document</span>.createElement(tag);
		i.appendChild(e, i);
		e.innerHTML = t.substr(f+1, l-f-1);
	}

	s = t.substr(1, f-1).split(' ');
	<span style="color: #0000ff">for</span> (i=0; i&lt;s.<span style="color: #0000ff">length</span>; i++) {
		ss = s[i].split('=');
		<span style="color: #0000ff">if</span> (ss.<span style="color: #0000ff">length</span>==1) <span style="color: #0000ff">continue</span>;
		e.setAttribute(ss[0], strip(ss[1]));
	}

	<span style="color: #0000ff">var</span> ti = t.substr(f+1, l-f-1), ri = 0;
	<span style="color: #0000ff">if</span> (t.substr(l+2, t.<span style="color: #0000ff">length</span>-l-3) == 'tr') {
		<span style="color: #0000ff">var</span> tf;
		<span style="color: #0000ff">while</span> ( (tf = ti.indexOf("<span style="color: #8b0000">&lt;td&gt;</span>")) != -1 ) {
			ti = ti.substr(tf+4, ti.<span style="color: #0000ff">length</span>-tf-4);
			<span style="color: #0000ff">var</span> te;
			<span style="color: #0000ff">if</span> (e.cells.<span style="color: #0000ff">length</span> &lt;= ri)	te = e.insertCell(-1);
			<span style="color: #0000ff">else</span> te = e.cells[ri];
			ri++;
			tl = ti.indexOf("<span style="color: #8b0000">&lt;/td&gt;</span>");
			te.innerHTML = ti.substr(0, tl);
			ti = ti.substr(tl, ti.<span style="color: #0000ff">length</span>-tl);
		}
	} 

	<span style="color: #0000ff">return</span> e;

	<span style="color: #0000ff">function</span> strip(ss) {
		<span style="color: #0000ff">if</span> (ss[0]=='\''|| ss[0] == "<span style="color: #8b0000">\"</span>")
			<span style="color: #0000ff">return</span> ss.substr(1, ss.<span style="color: #0000ff">length</span>-2);
	}
}</pre>
</blockquote>
<p>[tags]javascript, innerHTML, table[/tags]</p>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html">innerHTML只读问题 &amp; 两个动态修改Table的js函数</a></li><li ><a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html">Dreamhost上的GZIP &#038; Cache</a></li><li ><a href="http://zhiqiang.org/blog/it/speedup-website-with-css-sprites.html">用CSS Sprites让网站再加速</a></li><li ><a href="http://zhiqiang.org/blog/it/speedup-blog-about-javascript-css.html">加速blog：关于javascript(css)</a></li><li ><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2007. | <a href="http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html#comments">3 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

