Converting String to DOM
My Ajax functions gives me HTML-开发者_JAVA百科Elements as String back, and this String I want to append in my Document as DOM Element.
Something like
parentNode.appendChild(responseText);
What will be the best way to do this.
parentNode.innerHTML += responseText;
you can use innerText to do it
There can be more possible cases. You should clarify a bit.
If you get a string that should be an object and it's not existing yet, then you should use this:
var tempObj = document.createElement("yourString");
then you can just usetempObj
to handle it.If you get a string that is the name or ID of an existing object, then use:
var tempObj = document.getElementByName("yourString");
or
var tempObj = document.getElementById("yourString");
You can use the DOM methods this library provides, for example the insert() or update() method:
$('parentId').insert(yourString);
or
$('parentId').update(yourString);
http://api.prototypejs.org/dom/element/insert/
http://api.prototypejs.org/dom/element/update/
Note that innerHTML is not standarized yet, so using prototype, you can be sure those methods are cross browser compatible.
Good luck!
The simple way for converting String to DOM and vice versa is presented on this link:
http://famulatus.com/ks/programming/java/item/335-convert-xml-dom-to-string-and-string-to-xml-dom.html
精彩评论