The perfect solution of the tbody innerHTML in IE6/IE7/IE8/IE9--Experience exchange

Source: Internet
Author: User
Tags object model

The tbody innerHTML in IE6/IE7/IE8/IE9 cannot be assigned a value and the following code is reproduced

Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8"/>
Tbody innerHTML cannot be duplicated in &LT;TITLE&GT;IE6-IE9 bug</title>
<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>

Two buttons, the first to get the Tbody innerHTML, and the second to set the Tbody innerHTML.

When obtained, all browsers pop up the TR string, but the ie6-9 is not supported when set and the error is shown

You can use the feature to determine whether the browser supports TBODY innerHTML setting values

Copy Code code 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)

For IE6-IE9, if you want to set tbody innerHTML, you can use the following workaround

Copy Code code 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 include a table, then delete all the elements in the tbody, and finally give Tbody the first element of the first element of the Div, that is, div>table>tr.

There is, of course, a more streamlined version, which uses the ReplaceChild method directly to replace

Copy Code code as follows:

function settbodyinnerhtml (tbody, HTML) {
var div = document.createelement (' div ')
div.innerhtml = ' <table> ' + html + ' </table> '
Tbody.parentNode.replaceChild (Div.firstChild.firstChild, tbody)
}

The tHead of Col, Colgroup, frameset, HTML, head, style, table, TFOOT, innerHTML, title, and TR are read-only (IE6-IE9) from the MSDN records.

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 table" S dynamically. However, to change the content of a particular cell, your 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.