Skip to content

Instantly share code, notes, and snippets.

@froop
Created December 14, 2011 12:42
Show Gist options
  • Select an option

  • Save froop/1476430 to your computer and use it in GitHub Desktop.

Select an option

Save froop/1476430 to your computer and use it in GitHub Desktop.
[JavaScript] IEだとtbodyやtrのinnerHTMLが読み取り専用なのを回避
<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