Home  >  Article  >  Backend Development  >  XMLHttpRequest中responseText怎么获取指定<div>的内容,而不是整个HTML的内容

XMLHttpRequest中responseText怎么获取指定<div>的内容,而不是整个HTML的内容

WBOY
WBOYOriginal
2016-06-23 14:14:431891browse

http_request = new XMLHttpRequest;
....
document.getElementById(reobj).innerHTML = http_request.responseText;

现在responseText返回的是整个HTML,怎么才能让其返回我指定标签中的内容?

注: 什么模板,框架都没有用,只是在index.php中加载了一个xmlajax.js文件。上面代码在xmlajax.js中,获取的是index.php的整个HTML代码。如何获取指定标签的内容而不是整个HTML代码!!!



回复讨论(解决方案)

你只返回需要的内容就是了

就是只返回需要的内容,关键是怎么让responseText只返回需要的内容?

你请求的的url是index.php吗?在服务端返回你需要的内容就是了。

在index.php用ajax访问index.php? 你访问的文件有什么内容都会全部输出

在index.php用ajax访问index.php? 你访问的文件有什么内容都会全部输出
就是因为全部输出了,我需要的不是全部输出,只要输出部分。用正则能不能解决?能的话,请大神们给写个代码,在整个HTML中匹配

......
中的内容并返回。。

preg_match("/

]+id='_ajax'[^>]*>(.*)/is",$str,$arr);
print_r($arr[1]);

你不是给自己找麻烦吗?
document.getElementById(reobj).innerHTML = http_request.responseText;
document.getElementById(reobj).innerHTML = document.getElementById(reobj).getElementById('_ajax').innerHTML

你不是给自己找麻烦吗?
document.getElementById(reobj).innerHTML = http_request.responseText;
document.getElementById(reobj).innerHTML = document.getElementById(reobj).getElementById('_ajax').innerHTML

不好意识啊,我没说清楚。reobj就是_ajax

路过~~~~~~~~~~~· 了解了

这个的话你怎么不用responseXML啊,让index.php生成一个xml文件,然后用javascrip的DOM读取所需节点的内容!

正则匹配

index.php

这个请求的文件 你可控么? 如果可控 在里面控制返回的内容就可以了

路过,学习中。。。

现在遇到相同的问题

现在遇到同样的问题。。求救

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn