The innerHTML of tbody in the IE6-IE9 cannot be assigned a value. The reproduction code is as follows:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> innerHTML of tbody in IE6-IE9 cannot copy bug </title>
</Head>
<Body style = "height: 3000px">
<Table>
<Tbody>
<Tr> <td> aaa </td> </tr>
</Tbody>
</Table>
<P>
<Button id = "btn1"> GET </button> <button id = "btn2"> SET </button>
</P>
<Script>
Var tbody = document. getElementsByTagName ('tbody') [0]
Function setTbody (){
Tbody. innerHTML = '<tr> <td> bbb </td> </tr>'
}
Function getTbody (){
Alert (tbody. innerHTML)
}
Btn1.onclick = function (){
GetTbody ()
}
Btn2.onclick = function (){
SetTbody ()
}
</Script>
</Body>
</Html>
Two buttons: the first to get the innerHTML of the tbody and the second to set the innerHTML of the tbody.
When getting, all browsers pop up the tr string, but the IE6-9 is not supported at the time of the setting, and an error is reported,
You can use the feature to determine whether the browser supports tbody innerHTML settings.
Copy codeThe Code is as follows:
Var isupportTbodyInnerHTML = function (){
Var table = document. createElement ('table ')
Var tbody = document. createElement ('tbody ')
Table. appendChild (tbody)
Var boo = true
Try {
Tbody. innerHTML = '<tr> </tr>'
} Catch (e ){
Boo = false
}
Return boo
}()
Alert (isupportTbodyInnerHTML)
If you want to set innerHTML for the tbody in the IE6-IE9, you can use the following alternative method:
Copy codeThe Code is as follows:
Function setTBodyInnerHTML (tbody, html ){
Var div = document. createElement ('div ')
Div. innerHTML = '<table>' + html + '</table>'
While (tbody. firstChild ){
Tbody. removeChild (tbody. firstChild)
}
Tbody. appendChild (div. firstChild. firstChild)
}
Use a div to contain a table, delete all the elements in the tbody, and add the first element of the div to the tbody, that is, div> table> tr.
Of course, there is also a simpler version, which is directly replaced by the replaceChild method.
Copy codeThe Code is as follows:
Function setTBodyInnerHTML (tbody, html ){
Var div = document. createElement ('div ')
Div. innerHTML = '<table>' + html + '</table>'
Tbody. parentNode. replaceChild (div. firstChild. firstChild, tbody)
}
From the MSDN record, col, colGroup, frameset, html, head, style, table, tfoot, tHead, title, and tr are read-only (IE6-IE9 ).
The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.
You can change the value of the title element using the document. title property.
To change the contents of the table, tFoot, tHead, and tr elements, use the table object model described in Building Tables Dynamically. however, to change the content of a particle cell, you can use innerHTML.