문제

Good morning, I am geting html of another page by AJAX:

var xml = new XMLHttpRequest()
xml.onreadystatechange = function () {
    if (xml.readyState == 4) {
        // here I need to work with data
        // xml.responseText
    }
}
xml.open("GET", url, false);
xml.send(null)

How I can apply querySelectorAll() to html content of another page?

도움이 되었습니까?

해결책

You could create new document and put responseText into it. Then you can use querySelectorAll(). Here is your onreadystatechange function:

function () {
    if (xml.readyState == 4) {
        var container = document.implementation.createHTMLDocument().documentElement;
        container.innerHTML = xml.responseText;
        var nodeList = container.querySelectorAll('selector');
    }
}

다른 팁

If your response is valid XML, you can try xml.responseXml.

If that's not the case, you might consider using a DocumentFragment:

var doc = document.createDocumentFragment();
var div = document.createElement("div");
div.innerHTML = xml.responseText;
doc.appendChild(div);

var nodes = doc.querySelectorAll("...");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top