我已将 XMLHttpRequest()
的响应转储到网站的变量中,例如 yahoo.com。我需要如何通过此变量的 getElementById
或 getElementsByName
获取 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() );
或者,您可以将转储附加到文档中并从那里读取它。
关于javascript - 变量中的横向 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36992173/