有没有办法让JSON.stringify一个HTML DOM对象? [JavaScript的]
问题描述:
我试图加载一个外部html文件并将其中的内容加以串化,但似乎无法这样做,而不会产生不希望的结果。有没有办法做到这一点?
I am trying to load in a external html file and stringify the contents within the body but can't seem to do so without it producing undesired results. Is there even a way to do this?
var xhr = new XMLHttpRequest();
function loadFile(){
xhr.open("GET", 'index.html');
xhr.responseType = "document";
xhr.send();
}
xhr.onload = function(data) {
myData = data;
myString = JSON.stringify(myData.srcElement.responseXML.body.children);
console.log(myString)
//logs: {"0":{},"length":1}
}
loadFile();
但我需要加载的是实际的div内容,例如:
But what I need it to load is the actual div contents, ex:
//log: '<div id ='mydiv'>...</div>
我做错了什么?
答
function buildAd(file){
xhr.open("GET", file);
xhr.responseType = "DOMString";
xhr.send();
}