以下代码从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/