TestCode:
< Table ID = "Test" > </ Table > < Script > VaR Otable = Document. getelementbyid ( " Test " ); Otable. innerhtml = " <Tr> <TD> innerhtml </TD> </tr> " ; </ Script >
The above Code does not work in the IE6-9 and an error is reported directly:
Ie9:Invalid target element for this operation.
IE6-8:Unknown runtime error
Find the IE document (http://msdn.microsoft.com/en-us/library/ms533897 (vs.85). aspx) and find such a paragraph:
The Innerhtml Property is read-only on Col , Colgroup , Frameset , Html , Head , Style , Table , Tbody , Tfoot , Thead , Title , And Tr Objects.
So we can only use other solutions. My solutions:
VaR Otable = Document. getelementbyid ("test" ); // Otable. innerhtml = "<tr> <TD> innerhtml </TD> </tr> "; Settableinnerhtml (otable, "<tr> <TD> innerhtml </TD> </tr>" ); Function Settableinnerhtml (table, HTML ){ If (Navigator & navigator. useragent. Match (/MSIE/ I )){ VaR Temp = table. ownerdocument. createelement ('div'); Temp. innerhtml = '<Table> <tbody>' + HTML + '</tbody> </table>' ; If (Table. tbodies. Length = 0 ){ VaR Tbody = Document. createelement ("tbody" ); Table. appendchild (tbody);} table. replaceChild (temp. firstchild. firstchild, table. tbodies [ 0 ]);} Else {Table. innerhtml = HTML ;}}
Here, we only process the table and use a similar solution for other unsupported elements.
In addition, table in ie10 supports innerhtml.