javascript - 隐藏后div不显示
问题描述
我在执行 AJAX 调用时显示的 div 中有一个加载微调器。在第一次 AJAX 调用中,它按预期显示和工作。在这种情况下,加载微调器正在替换占位符内容。
在 AJAX 调用完成并从 AJAX 调用填充 Html 内容后,如果再次运行相同的函数,则不会显示加载微调器 div。其他一切都按预期工作。
<div class="col-md-8>
<div id="divForSectionPreview">
<div class="loaderDiv">
<center>
<img src="~/Images/loadingSpinner.gif" alt="loading.." />
</center>
</div>
<div align="center" class="placeholderDiv">
<h5 style="padding-top:30px">Placeholder content used before AJAX call is made....</h5>
</div>
</div>
</div>
<!--Html stored in another file, loaded by AJAX call-->
<div class="divContent" style="background-color:white;">
Html content returned from AJAX call...
</div>
<script>
$(document).ready(function () {
$('.loaderDiv').hide();//Hide loaderDiv
}
function loadContent(reportSectionId) {
$('.divContent').hide();//Hide the existing preview content if it's there
$('.placeholderDiv').hide();//Hide placehoder content if it's there
$('.loaderDiv').show();//Show loaderDiv ****THIS IS WHAT ONLY WORKS THE FIRST TIME THE FUNCTION IS CALLED****
$.ajax({
type: "GET",
url: "@Url.Action("../Path/File")",
data: { id: id },
dataType: 'html',
success: function (data) {
$('#divForSectionPreview').html(data);//Load PartialView for requested section. PartialView is selected in the method based on what type of section is requested
$('.loaderDiv').hide();//Hide loaderDiv
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
},
});
}
</script>
解决方案
加布里埃尔在评论中回答了这个问题。我只需要将.loaderDiv
元素移出,divForSectionPreview
因为正如他所说,整个内容divForSectionPreview
都被 AJAX 调用替换,并且divForSectionPreview
在哪里.loaderDiv
。
<div class="col-md-8>
<div class="loaderDiv">
<center>
<img src="~/Images/loadingSpinner.gif" alt="loading.." />
</center>
</div>
<div id="divForSectionPreview">
<div align="center" class="placeholderDiv">
<h5 style="padding-top:30px">Placeholder content used before AJAX call is made....</h5>
</div>
</div>
</div>
<!--Html stored in another file, loaded by AJAX call-->
<div class="divContent" style="background-color:white;">
Html content returned from AJAX call...
</div>
<script>
$(document).ready(function () {
$('.loaderDiv').hide();//Hide loaderDiv
}
function loadContent(reportSectionId) {
$('.divContent').hide();//Hide the existing preview content if it's there
$('.placeholderDiv').hide();//Hide placehoder content if it's there
$('.loaderDiv').show();//Show loaderDiv ****THIS IS WHAT ONLY WORKS THE FIRST TIME THE FUNCTION IS CALLED****
$.ajax({
type: "GET",
url: "@Url.Action("../Path/File")",
data: { id: id },
dataType: 'html',
success: function (data) {
$('#divForSectionPreview').html(data);//Load PartialView for requested section. PartialView is selected in the method based on what type of section is requested
$('.loaderDiv').hide();//Hide loaderDiv
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
},
});
}
</script>
推荐阅读
- laravel - 制作带有评论的帖子页面的最佳方法是什么。Laravel - vue.js - 惯性 vue
- google-anthos - 将 GCE 虚拟机迁移到 GKE 失败
- database - SQLite 新手 - 如何存储包含子对象和数组的对象列表的数据?
- jpeg - 当零填充 JPEG 编码段是 0xff 字节 algined 或任何连续的位运行时?
- web-services - 封装每个模型的 Web 请求
- ruby - Ruby 无法加载“随机”,默认的 gem
- typescript - 通过传播对象道具的类型函数
- python - Pythonic 方法来绘制数据帧和具有不同 x 值的 y 值的平均值
- c# - 如何使用 LINQ 和 string.Split() 函数获取 MAX
- php - 通过 PHP shell_exec() 函数(Apache/Ubuntu)更改用户并在远程服务器上运行 bash 命令