Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串转换为html元素以运行javascript_Javascript_Html_Dom - Fatal编程技术网

将字符串转换为html元素以运行javascript

将字符串转换为html元素以运行javascript,javascript,html,dom,Javascript,Html,Dom,我正在尝试读取来自xhr请求的响应。我不想尝试使用正则表达式来解析信息。是否需要将响应字符串转换为html元素,以便使用document.getElementsById()读取所需信息?解决方案无法使用jquery 编辑:格式是来自xhr响应的常规html。我想从DIV那里得到信息 <div class="name">John Doe</div> johndoe 当然没问题,如果您的响应是HTML,只需创建一个DIV并将您的响应放入其中即可。不过,我不确定您提到的get

我正在尝试读取来自xhr请求的响应。我不想尝试使用正则表达式来解析信息。是否需要将响应字符串转换为html元素,以便使用document.getElementsById()读取所需信息?解决方案无法使用jquery

编辑:格式是来自xhr响应的常规html。我想从DIV那里得到信息

<div class="name">John Doe</div>
johndoe

当然没问题,如果您的响应是HTML,只需创建一个DIV并将您的响应放入其中即可。不过,我不确定您提到的getElementsByName,据我所知,这不是一个标准的DOM函数

var xhrResponse = "<span id='status'>SUCCESS</span>";

var clipboard = document.createElement("DIV");
clipboard.innerHTML = xhrResponse;
var searchingForSomething = clipboard.getElementById('status');
console.log(searchingForSomething.innerHTML); // => SUCCESS
var xhrResponse=“SUCCESS”;
var clipboard=document.createElement(“DIV”);
clipboard.innerHTML=xhrResponse;
var searchingForSomething=clipboard.getElementById('status');
console.log(searchingForSomething.innerHTML);//=>成功

您的临时DIV甚至不必出现在文档中,就可以正常工作。

但是,如果没有jQuery,按类获取元素是冗长的。你可以搜索整个树,最简单的代码是递归的,或者如果你只支持高端浏览器,你可以使用querySelector。你的响应不是很清楚,但听起来好像你想用它填充某个元素的内部html。响应是在普通html中。我正试图从像约翰·多伊这样的人身上获取信息