简体   繁体   English

如何使jQuery .load()将返回的数据附加到容器内而不是在容器上重写?

[英]how to make jQuery .load() to append returned data inside the container instead of rewrite on it?

I am using .load() function to load html content from a controller action and put in an existing container element, The problem is that this coming data from the load method remove everything in this container and then write the new data there, What I want is to keep any content inside this container and just append the coming data. 我正在使用.load()函数从控制器操作中加载html内容并放入现有的容器元素中,问题是来自load方法的此数据会删除此容器中的所有内容,然后将新数据写入那里想要的是将任何内容保留在此容器中,然后仅追加即将到来的数据。

here is my load: 这是我的负担:

$("#container").load("/Profile/Chat", { friendId: friendId });
$.get("/Profile/Chat", { friendId: friendId }, function(html){
    $("#container").append(html);
}, 'html');

load always replaces the content of the container. load始终替换容器中的内容。 You can get around this by loading the content into a different hidden container, then append it to the intended container: 您可以通过将内容加载到其他隐藏容器中,然后appendappend到预期的容器中来解决此问题:

$("#hiddenContainer").load("/Profile/Chat", { friendId: friendId }, function() {
      $("#container").append($("#hiddenContainer").html());
});

load , get , post etc are all just wrappers around the jQuery ajax method. loadgetpost等都只是jQuery ajax方法的包装。 When in doubt, ajax always gets you what you want. 如有疑问, ajax总是可以为您提供所需的东西。

$.ajax({
    url: "/Profile/Chat",
    data: { friendId: friendId },
    success: function(data){
        $("#container").append(data);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM