在一个变量中横向 DOM

Transverse DOM in a Variable

我已将 XMLHttpRequest() 的响应转储到网站的变量中,例如 yahoo.com。我如何需要通过此变量的 getElementByIdgetElementsByName 获取 DOM 内容的值。

例如:

var dump; //contains html source code of a website e.g. yahoo.com
var data = dump.getElementsByTagName('span').value;
// it's throw error "Uncaught ReferenceError: getElementsByTagName is not defined"

现在,我需要从变量转储中获取 SPAN 标记值。

是否可以单独使用 javascript 或 jquery 来检索这些数据?

应该与 jquery 一起工作:

https://jsfiddle.net/q0r3fem6/

var dump = "<html><body><div><span value='xyz'>test</span></div><body></html>";
console.log( $(dump).find("span").attr("value") );
console.log( $(dump).find("span").text() );

或者您可以将转储附加到您的文档并从那里读取它。