Created
February 4, 2014 18:17
-
-
Save niloc132/8809252 to your computer and use it in GitHub Desktop.
In non-IE browsers, changing the innerHTML of an element detaches its children, but leaves them intact. In IE, it also clears the innerhtml property of all children, detaching all descendants.
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
public class HowDoIInnerHtml implements EntryPoint { | |
@Override | |
public void onModuleLoad() { | |
Element body = Document.get().getBody(); | |
DivElement newDiv = Document.get().createDivElement(); | |
newDiv.setClassName("foo"); | |
newDiv.setInnerText("Text in the div"); | |
SpanElement newSpan = Document.get().createSpanElement(); | |
newDiv.appendChild(newSpan); | |
body.appendChild(newDiv); | |
//page shows "Text in the div" | |
Window.alert("newDiv.innerHTML? " + newDiv.getInnerHTML() + | |
"\nspan is in div? " + (newSpan.getParentElement() == newDiv)); | |
//alert shows "Text in the div <span></span>", true | |
body.setInnerHTML(""); | |
Window.alert("newDiv.innerHTML? " + newDiv.getInnerHTML() + | |
"\nspan is in div? " + (newSpan.getParentElement() == newDiv)); | |
//alert shows "", false ??? | |
body.appendChild(newDiv); | |
//page shows blank | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment