这是我的代码:
const http = new XMLHttpRequest();
const url = 'http://page/';
http.open('get', url, false);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function ()
{
if(http.readyState === 4)
{
if(http.status === 200 || http.status == 0)
{
let str = (http.responseText);
results = document.getElementById('test').value;
alert(results)
}
}
}
http.send();
我如何将 str 排序为 html,以便我可以使用 document.getElementById 获取值?
我已经尝试过
var doc = document.implementation.createHTMLDocument("example");
doc.documentElement.innerHTML = http2.responseText;
let test1 = doc.body.querySelector("#test").value;
它有效,但它给了我一个空值,当我查看响应预览时,我发现值栏是空的,选项就像工厂默认状态一样。当我查看页面的 html 代码时,发现了这一行中的值
<script language=javascript>Transfer_meaning('test','\12345678');</script>
如果responseText是纯文本HTML,你可以这样做
document.getElementById('test').innerHTML = str;
如果你使用jquery:
如果回复是这样的:
let str = "<body><p>Paragraph Here</p><input id='test' value='foo'></body>";
替换此行:
results = document.getElementById('test').value;
用这一行:
let results = $('input#test', "<div>"+str+"</div>").val();