Solution to the problem that tbody innerHTML cannot be assigned a value in IE6-IE9

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.