The IE6-IE9 in the tbody innerHTML can not assign a value to the solution, ie6-ie9tbody

Source: Internet
Author: User

The IE6-IE9 in the tbody innerHTML can not assign a value to the solution, ie6-ie9tbody

The innerHTML of tbody in the IE6-IE9 cannot be assigned a value. The reproduction code is as follows:

Js Code

<! DOCTYPE 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.

var isupportTbodyInnerHTML = function () {var table = document.createElement('table')var tbody = document.createElement('tbody')table.appendChild(tbody)var boo = truetry{tbody.innerHTML = '<tr></tr>'} catch(e) {boo = false}return boo}()alert(isupportTbodyInnerHTML)

Click to see if the browser you are browsing this blog supports

Click Me

<Script type = "text/javascript"> // <! [CDATA [var isupportTbodyInnerHTML = function () {var table = document. createElement ('table') var tbody = document. createElement ('tbody') var tr = document. createElement ('tr ') var td = document. createElement ('td ') var txt = document. createTextNode ('A') td. appendChild (txt) tr. appendChild (td) tbody. appendChild (tr) table. appendChild (tbody) var boo = true try {tbody. innerHTML = '<tr> <td> B </td> </tr>'} catch (e) {boo = false} return boo} (); tbodyInnerHTML. onclick = function () {if (isupportTbodyInnerHTML) {alert ('your browser supports tbody innerHTML assignment ')} else {alert ('your browser is the IE6-9 kernel, tbody innerHTML assignment ') }}//]> </script>

If you want to set innerHTML for the tbody in the IE6-IE9, you can use the following alternative method:

Js Code

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.

Js Code

function setTBodyInnerHTML(tbody, html) { var div = document.createElement('div') div.innerHTML = '<table>' + html + '</table>' tbody.parentNode.replaceChild(div.firstChild.firstChild, tbody) }

After intercepting an html code containing a table on the Internet, what can be done if innerHTML cannot be used to add the code to the block? The table contains thead and tbody.

<Tr>
<Th scope = "col" axis = "length" class = "icon" style = "width: 5%">
</Th>

Not matched. th should be td.
 
InnerHTML assignment in JavaScript

Question: In the OK () function defined in the test1.htm file, you should change this function to the following:
Function OK ()
{
Document. getElementById ("load" 2.16.innerhtml#parent.left.doc ument. form1.c. value;
}
As you can see, innerHTML is an attribute rather than a method, so you should assign a value to innerHTML instead of calling it as a method.
In addition, you should not place the passed values in the <td> MARK In the table, in test1.htm
<Td id = "load"> load the code entered on the left here </td>
It should also be modified to the following statement:
<Td> <div id = "load"> load the code entered on the left </div> </td>
To implement the functions you need.

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.