简体   繁体   中英

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). I know I can use JQuery .load() to replace the contents, but there must be some easy way to replace the whole thing.

您可以使用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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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