以下代码从 www.example.com 启动,获取 www.example.com/example.html 的完整 html 源代码并对其发出警报。
function process(){
url = "http://www.example.com/example.html"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
alert(xhr.responseText)
}
}
xhr.send();
}
process();
现在我想做的是从 html 源代码中获取电子邮件,如下所示:
<input type="hidden" id="email2" name="email2" value="example@example.com" />
通常,如果我在页面本身上,我只会做一个简单的操作:
document.getElementById('email2').value
但是在这种情况下,我如何简单地将电子邮件解析为包含所有 html 源代码的变量,即:xhr.responseText
?
最佳答案
你不能使用 getElementById.. 因为你拥有的只是一个字符串..但你可以解析它
alert(xhr.responseText.split('id="email2"')[1].split('value="')[1].split('"')[0])
关于javascript - 从通过 get ajax 请求收到的 html 源代码获取电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17848743/