如何更新 MooTools Request.HTML 调用中的多个元素

发布于 2024-11-02 12:39:36 字数 732 浏览 0 评论 0原文

使用 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>

我希望代码循环遍历的子元素,获取 id,将该 id 与页面中的元素相匹配,然后将页面的元素 insideHTML 替换为该元素来自响应的innerHTML。这似乎并不难,但我似乎无法让它发挥作用。任何帮助将不胜感激。谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-11-09 12:39:36

您可以像这样遍历 Request.HTML 返回的元素:

http://jsfiddle.net/dimitar/NF2jz /1139/

new Request.HTML({
    url: '/echo/html/',
    data: {
        html: data,
        delay: 1
    },
    method: 'post',
    onComplete: function() {
        // first, only get elements that have an id and are divs
        // this may be slow as it looks for ALL els returned, depends on your
        // fragment 
        var filtered = this.response.elements.filter(function(el) {
            return el.get("id") && el.get("tag") == "div";
        });

        // This wil be even faster, working with your response fragment 
        // where your els are children of the first node:
        // get div els where id is set via Slick
        var filtered = this.response.tree[0].getElements("div[id]");

        filtered.each(function(el) {
            // get the id
            var id = el.get("id");
            // remove the id from the element so dom selector works correctly
            el.set("id", null);

            // look for dom element that matches
            var target = document.id(id);

            // if found, update html
            if (target)
                target.set("html", el.get("html"));

        });
    }
}).send();

正如 mootools-core 的 Tim Weink 喜欢指出的那样,我有一个讨厌的习惯,即使用公共 API 之外的未记录的 mootools 功能,例如访问 this.response直接在我应该使用命名参数的地方。记住这一点并查看文档,了解哪个参数将与 this.response.elementsthis.response.tree 匹配 - 在不可能中mootools 更改其 API 并使其不可用的事件。

you can walk the returned elements form Request.HTML like so:

http://jsfiddle.net/dimitar/NF2jz/1139/

new Request.HTML({
    url: '/echo/html/',
    data: {
        html: data,
        delay: 1
    },
    method: 'post',
    onComplete: function() {
        // first, only get elements that have an id and are divs
        // this may be slow as it looks for ALL els returned, depends on your
        // fragment 
        var filtered = this.response.elements.filter(function(el) {
            return el.get("id") && el.get("tag") == "div";
        });

        // This wil be even faster, working with your response fragment 
        // where your els are children of the first node:
        // get div els where id is set via Slick
        var filtered = this.response.tree[0].getElements("div[id]");

        filtered.each(function(el) {
            // get the id
            var id = el.get("id");
            // remove the id from the element so dom selector works correctly
            el.set("id", null);

            // look for dom element that matches
            var target = document.id(id);

            // if found, update html
            if (target)
                target.set("html", el.get("html"));

        });
    }
}).send();

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 match this.response.elements or this.response.tree - in the unlikely event that mootools change their API and make that unavailable.

安穩 2024-11-09 12:39:36
onSuccess:function(responseJSON, responseText)
{
    var data_length = responseJSON.data.length;
    for(var i=0;i<data_length;i++){
         $(responseJSON.data[i].id).set('html',responseJSON.data[i].html);
    }
}

来自服务器的 JSON 响应

{data: [
        {id:"header",html:"<h1>My title</h1>"},
        {id:"content",html:"<h2>Content</h2><p>This is my content</p>"},
        ....  
       ]  
}

您可以执行一些捷径,但这显示了逻辑。

onSuccess:function(responseJSON, responseText)
{
    var data_length = responseJSON.data.length;
    for(var i=0;i<data_length;i++){
         $(responseJSON.data[i].id).set('html',responseJSON.data[i].html);
    }
}

The JSON reposnse from server

{data: [
        {id:"header",html:"<h1>My title</h1>"},
        {id:"content",html:"<h2>Content</h2><p>This is my content</p>"},
        ....  
       ]  
}

There are some short cutts you can do, but this shows the logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文