javascript 如何在 Ajax 响应后刷新特定的 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31716106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I refresh a specific div after Ajax response?
提问by Usman Waheed
The problem is that I want to refresh a specific div after the Ajax response successfully recently I am using:
问题是我想在最近使用的 Ajax 响应成功后刷新特定的 div:
$("#parent_div").load(location + "#child_div")
This will refresh whole parent div not specific child_div
I have been use these
这将刷新整个父 div 不具体child_div
我一直在使用这些
$("#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?
关于这个特定问题的任何建议?
回答by ANR Upgraded Version
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 i.e.,Element.You can allow or restrict any ajax response to be refreshed with this elements.
AJAX 是与服务器交换数据和更新网页的一部分的艺术 - 无需重新加载整个页面。当我们能够引用 DOM 节点的 tagName 即Element时,这项艺术就变得栩栩如生。您可以允许或限制任何 ajax 响应使用此元素刷新。
<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
此演示如何为您的示例指定元素用法。您可以在 Ajax 调用成功时执行相同的操作。 工作演示
回答by adang
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 htmlor appendas shown below:
我假设您想使用 ajax 调用(或一些操作等)的响应更新页面上的特定元素。在 ajax 成功或错误事件中,使用方法html或append设置特定元素 html ,如下所示:
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.
这里的 html, append word 将用您作为字符串提供的内容替换元素内容(“Ajax 响应成功”)。内容可以包括 html div 或简单的文本。在示例中,“myelement”是 html 元素的 id。
For example:
例如:
<div id="myelement"></div>