简体   繁体   English

使用JQuery ajax替换div,而不替换内容

[英]Replace div using JQuery ajax, not replace contents

Here's an example doc structure 这是一个示例文档结构

<div>
    <div id="1" class="to-replace"></div>
    <div id="2" class="to-replace"></div>
    <div id="3" class="to-replace"></div>
</div>

What I want to do is replace #1 , for example, with whatever I retrieve from the ajax call (I'm not using JSON because the request is extremely small and it didn't seem worth it. Plus, the site is mostly for my personal use). 我想做的就是替换#1 ,例如,将其替换为我从ajax调用中检索到的所有内容(我不使用JSON,因为请求非常小,而且似乎不值得。此外,该网站主要用于我个人使用)。 I know I can use JQuery .load() to replace the contents, but there must be some easy way to replace the whole thing. 我知道我可以使用JQuery .load()替换内容,但是必须有一些简单的方法替换整个内容。

您可以使用replaceWith jquery函数替换元素:

    $("#1").replaceWith("bla");
$.get(url,function(data){
   $("#1").replaceWith(data);
}); 

where url is the url to whatever you want to get and data is the data that is returned url是您要获取的内容的URL,data是返回的数据

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

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