这是我的代码:
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>
答案 0 :(得分:0)
如果responseText是纯文本HTML,则可以这样做
document.getElementById('test').innerHTML = str;
答案 1 :(得分:0)
@mina nageh curl
看起来如何?
请这样做:
http.responseText
并向我们展示您在调试器控制台(浏览器上的 F12 )中得到了什么
如果http.onreadystatechange = function ()
{
if (http.readyState === 4)
{
if (http.status === 200 || http.status == 0)
{
let str = (http.responseText);
console.log( 'str length = ', str.length )
let QuotesElements = http.responseText.match(/['"](.*?)['"]/g)
console.log ( QuotesElements )
/*---
results = document.getElementById('test').value;
console.log(results)
---*/
}
}
}
是HTML代码的一小部分:
http.responseText
答案 2 :(得分:0)
如果您使用的是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();