javascript - 进度 .gif 不显示 .load

标签 javascript jquery html css

我正在尝试编写一些代码,以便在将文件 pts.php 加载到 index.php 上的#content div 时(在页面加载时)显示加载 gif,因为 pts.php 非常大并且需要几秒钟。我不希望人们开始垃圾点击和刷新尝试让它工作,因此 gif。

我遇到的问题是据我所知 gif 根本没有显示。有没有办法测试它是否正在显示,如果没有,有人知道如何修复它吗?我通过 CSS 隐藏了 div #loadingimagepage,这段代码位于带有脚本标签的 index.php 页面中:

$(document).ready(function(){
    $('#loadingimagepage').show();
    $("#content"). load ("pts.php");
    $('#loadingimagepage').hide();

这就是#content div:

<div id="content">
        <div id="loadingimagepage">
            <img src="images/ajax-loader.gif" />
        </div>
</div>

如有任何帮助/提示,​​我们将不胜感激。谢谢!

最佳答案

您的代码将在不等待加载完成的情况下进行处理。您需要将 $('#loadingimagepage').hide(); 放在您完成的加载函数中。

$("#content").load("pts.php", function(){
    $('#loadingimagepage').hide();
});

您甚至可以测试加载是否成功并通知用户(同时仍然隐藏加载 gif)

$("#content").load("pts.php", function(passedSuccessVar){
    $('#loadingimagepage').hide();
    if(!passedSuccessVar) { alert("Error!Error! Danger Will Robinson");}
});

你目前拥有的:

$('#loadingimagepage').show();   //starts showing right away
$("#content"). load ("pts.php"); //starting loading content
$('#loadingimagepage').hide();   //hides the loading gif regardless of the previous function status

关于javascript - 进度 .gif 不显示 .load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20618332/

相关文章:

javascript - 如何动态加载模态

html - 显示 :none when rotating smartphone

html - 我可以从哪里访问我的 netlify.toml 文件?

html - 使用 rem 时,Bootstrap 标题字体在移动设备中较小

javascript - 使用 Ajax 请求单击链接后更改 div 的内容

javascript - Nodejs 持久变量

javascript - CSS伪元素混淆

javascript - 在PHP变量中修改/加载CDN JS的内容

Javascript 通过引用调用不起作用

javascript - 将多个 csv 文件中的时间戳数据添加到 highchart