【发布时间】:2011-08-22 11:00:21
【问题描述】:
以下只是我的 ajax 代码的一部分,目前我的变量“resp”正在从 test.php 返回整个响应。有什么方法可以在指定标签之间返回 responseText 吗? IE。只需取回 id="xyz" 的 test.php 的 div 中的 html 的 responseText?
var loc="test.php?myinput=apple";
ajax.onreadystatechange=function() {
if (ajax.readyState==4 && ajax.status == 200) {
var resp = ajax.responseText;
}
}
ajax.open("GET",loc,true);
ajax.send(null);
【问题讨论】:
-
您可以使用库来解析响应 HTML,然后通过 CSS 选择器找到 DIV。像这样:
$(resp).find('#xyz') -
如果您对 DIV 的源文本内容感兴趣,我建议您使用服务器端脚本来返回它而不是通过 JavaScript 搜索它。
标签: javascript html xml ajax