作为 DOM 对象的 Ajax 响应
有没有办法从典型的 ajax 函数获取响应,以便可以用 getElements 对其进行剖析?我已经尝试过 query.responseText.getElementById
但它的效果和看起来一样糟糕。不过,您应该能够通过看到该片段来了解我想要实现的目标。我只需要从 ajax 响应中获取元素,就像获取普通 DOM 对象一样。
另外,请不要建议使用 jQuery。当我有很多脚本并且可以使用它的很多功能时,我会使用它,但在这种情况下,我只有一个简短的脚本和一个 70 倍大小的库,这似乎是一种浪费。
Is there a way to get a response from the typical ajax function so that it can be dissected with getElements? I've tried query.responseText.getElementById
but it works just as bad as it looks. You should be able to tell what I'm trying to achieve by seeing that snippet, though. I just need to get elements from an ajax response the same way as I would a normal DOM object.
Also, please do not suggest using jQuery. I use it when I have a lot of script and can use a lot of its functions, but in this case I only have a short script and a library 70x the size of it would seem like a waste.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解析 SVG 或 HTML 文档
doc< /strong> 将是一个有效的 html 文档。
Parsing an SVG or HTML document
doc will be a valid html document.
好吧,您可以在页面上隐藏一个
div
并将其 innerHTML 设置为您收到的 Ajax 响应。然后您可以调用div.getElementById()
,因为它只是另一个 DOM 对象。Well you could have a hidden
div
on your page and set it's innerHTML to the Ajax response you receive. You could then calldiv.getElementById()
, since it is then just another DOM object.参考这篇文章:在Ajax中解析XML响应
在本例中,我使用
responseXML
。您可以使用 getElementsByTagName 和其他 getElement*() 方法来获取数据。Refer to this article: Parsing XML response in Ajax
In this case I am using
responseXML
. You can make use of getElementsByTagName and other getElement*() methods to get your data.如果您的回复是文本,我见过 ppl 使用 ...xhr.responseText.spit('html>...body>...div id="yourTargetsParent">')[1].split ('/div>.../body>.../html>')[0]; //只是将字符串分开!
另一种方法是使用 iframe.contentWindow.document.body...(或某些浏览器的 contentDocument)...只需隐藏 iframe 你知道。
显然,如果你可以控制完全改变事情的目标(这篇文章可能不会在这里),但我也看到了一些通过目标使用脚本编写其主机 dom、localStorage、splits/joins 的方法, webSQLDatabases, ...用于字符串操作。
老实说,我曾经使用隐藏的 div(谢谢 sleepysamurai!),但我想我遇到了更多 getElementById/jQuery.load 类型的方式。 ..如果找到的话我会回来的..
If your response is TEXT, I've seen ppl use ...xhr.responseText.spit('html>...body>...div id="yourTargetsParent">')[1].split('/div>.../body>.../html>')[0]; //just split the string up however!
Another way is to use iframe.contentWindow.document.body... (or contentDocument for some browsers)... just hide the iframe ya know.
Obviously, if you have control over the target that totally changes things (and this post probably wouldn't be here), but I've also seen some mean work arounds with the target's use of scripting its host dom, localStorage, splits/joins, webSQLDatabases, ...for string manipulation.
Honestly, I used to use a hidden div(thank you asleepysamurai!), but I thought I came across a more getElementById/jQuery.load type way. ..I'll post back if I find it...
例如javaScript“append”需要解析html(.body.firstElementChild仅获取元素而没有主体)
For example javaScript "append" need parsed html (.body.firstElementChild get element only without body)