如何更新 MooTools Request.HTML 调用中的多个元素
使用 MooTools Request.HTML,我的 Request 调用工作完美,通过使用以下内容更新我的页面元素:
onComplete: function() {
var response = this.response.text;
$('content').set("html", response);
}
但是,我希望能够有一个响应,其中包含由 ID 更新的多个元素,但我不能我的生活让它正常工作。例如,如果我的响应是:
<response>
<div id="header"><h1>My Title</h1></div>
<div id="content"><h2>Content</h2><p>This is my content</p></div>
<div id="footer"><p>Special footer for this page</p></div>
</response>
我希望代码循环遍历
Using MooTools Request.HTML, I have my Request call working perfectly, updating my page element by using something like:
onComplete: function() {
var response = this.response.text;
$('content').set("html", response);
}
However, I'd like to be able to have a response have multiple elements that are updated by ID and I can't for the life of me get this working correctly. For instance, if my response is:
<response>
<div id="header"><h1>My Title</h1></div>
<div id="content"><h2>Content</h2><p>This is my content</p></div>
<div id="footer"><p>Special footer for this page</p></div>
</response>
I would like the code to loop through the child elements of <response>, grab the id, match that id to an element in the page, and replace the element innerHTML of the page with the element innerHTML from the response. It didn't seem to be that hard but I just can't seem to get this working. Any help would be much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样遍历 Request.HTML 返回的元素:
http://jsfiddle.net/dimitar/NF2jz /1139/
正如 mootools-core 的 Tim Weink 喜欢指出的那样,我有一个讨厌的习惯,即使用公共 API 之外的未记录的 mootools 功能,例如访问
this.response
直接在我应该使用命名参数的地方。记住这一点并查看文档,了解哪个参数将与this.response.elements
或this.response.tree
匹配 - 在不可能中mootools 更改其 API 并使其不可用的事件。you can walk the returned elements form Request.HTML like so:
http://jsfiddle.net/dimitar/NF2jz/1139/
as Tim Weink from mootools-core likes to point out, I have a nasty habit of using undocumented mootools features that fall outside of the public API, such as accessing
this.response
directly where I should use the named arguments instead. Keep it in mind and look at the documentation on which argument will matchthis.response.elements
orthis.response.tree
- in the unlikely event that mootools change their API and make that unavailable.来自服务器的 JSON 响应
您可以执行一些捷径,但这显示了逻辑。
The JSON reposnse from server
There are some short cutts you can do, but this shows the logic.