Created
December 14, 2011 12:42
-
-
Save froop/1476430 to your computer and use it in GitHub Desktop.
[JavaScript] IEだとtbodyやtrのinnerHTMLが読み取り専用なのを回避
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html><body> | |
| <table><tbody id="tbody1"></tbody></table> | |
| <script> | |
| var tbody1 = document.getElementById("tbody1"); | |
| replaceTableRows( | |
| "<table><tr><td>01</td><td>02</td></tr></table>", tbody1); | |
| replaceTableRows( | |
| "<table><tr><td>03</td><td>04</td></tr></table>", tbody1); | |
| function replaceTableRows(newTableHTML, targetTbody) { | |
| var dummyTag = document.createElement("div"), newTbody; | |
| dummyTag.innerHTML = newTableHTML; | |
| newTbody = dummyTag.getElementsByTagName("tbody")[0]; | |
| while (targetTbody.childNodes.length > 0) { | |
| targetTbody.removeChild(targetTbody.childNodes[0]); | |
| } | |
| if (newTbody) { | |
| while (newTbody.childNodes.length > 0) { | |
| targetTbody.appendChild(newTbody.childNodes[0]); | |
| } | |
| } else { | |
| var trTag = document.createElement("tr"), | |
| tdTag = document.createElement("td"); | |
| tdTag.innerHTML = newTableHTML; | |
| trTag.appendChild(tdTag); | |
| targetTbody.appendChild(trTag); | |
| tdTag.colSpan = "99"; | |
| } | |
| } | |
| </script> | |
| </body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment