table.innerHTMLへの代入
IE7 beta3を試しています。既存のJavaScriptを動かしてみて、まずinnerHTMLへの代入で引っかかりました。
普通のdivとかならinnerHTMLに代入しても問題ないんですけど、table、tbody、trのinnerHTMLへの代入が「未知の実行時エラー」とか言って怒られます。
いちいちremoveChild、あるいはdeleteRowするのは面倒なのでinnerHTMLにヌル文字列を代入する方が楽なんですけど、多分テーブルの構造が変わってしまうから許したくないんだろうなーとも思います。
念のため書いておくとIE6やFirefox1.5、Opera9.0では上のような現象は起きません。IE7 beta3 のバグであってほしい。
以下、テストするためのHTMLです。(要prototype.js)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>innerHTML test for IE7</title> <script language="javascript" type="text/javascript" charset="UTF-8" src="prototype.js" ></script> </head> <body> <div> <div id="replacable"> IE7でもDIVとかにならinnerTextで代入できる </div> <textarea id="replace_text" cols="100" rows="5"></textarea> <div> <input type="text" id="replace_field_id" value="replacable"/> <input type="button" id="replace_button" value="replace"/> </div> <script language="JavaScript" type="text/javascript"><!-- Event.observe($("replace_button"), "click", function(event) { $( $("replace_field_id").value ).innerHTML = $("replace_text").value; }, false); --></script> </div> <div style="margin-top:30px;"> IE7ではtable関係のinnerHTMLに代入できない <table id="products" border="1" cellspacing="0" cellpadding="0"> <thead id="products_thead"><tr id="row_0"> <th id="th_0" width="30">ID</th> <th id="th_1" width="50">カテゴリ</th> <th id="th_2" width="400">名前</th> <th id="th_3" width="100">ASINコード</th> <th id="th_4" width="80">価格</th> </tr></thead> <tbody id="products_tbody_0"> <tr id="row_1"> <td id="td_0">1</td> <td id="td_1">書籍</td> <td id="td_2">RailsによるアジャイルWebアプリケーション開発</td> <td id="td_3">4274066401</td> <td id="td_4">3990</td> </tr> </tbody> </table> <div> <textarea id="table_replace_html" cols="100" rows="5"></textarea> <br/>products, <br/>products_thead, row_0, th_0 ・・・ th_4, <br/>products_tbody_0, row_1, td_0 ・・・ td_4 <br/> <input type="text" id="table_replace_field_id" value="products"/> <input type="button" id="table_replace_button" value="replace table node"/> </div> <script language="JavaScript" type="text/javascript"><!-- Event.observe($("table_replace_button"), "click", function(event) { $( $("table_replace_field_id").value ).innerHTML = $("table_replace_html").value; }, false); --></script> </div> </body> </html>