简体   繁体   中英

How can I refresh a specific div after Ajax response?

The problem is that I want to refresh a specific div after the Ajax response successfully recently I am using:

$("#parent_div").load(location + "#child_div")

This will refresh whole parent div not specific child_div I have been use these

$("#parent_div").load(location + "#child_div")
$("#parent_div").reload(location + "#child_div")
$("#child_div").reload(window.location + "#child_div")

Any suggestion regarding this particular issue?

AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page. This Art comes to life when we were able refer to the tagName of DOM nodes ie, Element .You can allow or restrict any ajax response to be refreshed with this elements.

<div id="parent_div">
    <span class="parenttexthere">Parent Div Text </span>
    <div id="child_div" >
        <span class="childtexthere">Child Div Text </span>
    </div>
</div>
<br/>
<button> Refresh</button>
<script>
var i=1;
$('button').on('click',function(){
    var data="Parent Div Data Changed "+i+ " times";

    $("#parent_div .parenttexthere").hide().html(data).fadeIn('fast');
    i++;
               });
</script>

Any how this Demo is to specify Elements usage to your example.Where You Can Perform same thing on Ajax call Success. Working Demo

I assume you want to update a specific element on page with response of ajax call(or some manipluation etc). On ajax success or error event, set the specific element html using methods html or append as shown below:

 jQuery("#myelement").html("Ajax response came as success");

or if you want to append then:

 jQuery("#myelement").append("Ajax response came as success");

here the html, append word will replace the element content with the content you provided as string ("Ajax response came as success"). The content can inculde html div or simple text . Here in example "myelement" is id of the html element.

For example:

 <div id="myelement"></div>
$("#child_div").load(location + "#parent_div")

要加载特定的 div,请在第一部分添加您需要刷新的 div(即我添加子 div 的地方),现在 load 是一个加载页面的函数,其中 location 表示当前位置,父 div 是页面的内容加载

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