我对 jQuery 和 Python 有点摸不着头脑,我试图展示一个非常简单的页面来显示正在加载的 img,而一个有点长的 python 脚本获取我需要显示的信息。
当然,当 Python 脚本准备好后,我希望隐藏加载图像并在其位置显示 Python 脚本的结果。
到目前为止,借助 Google 对我网页的一些帮助,我能够将其拼凑起来:
<html>
<head>
<img id="img" src="loader.gif">
<script src="assets/js/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
url: "http://localhost/test.py";
$.post(url, function(data){
$("#id").html(data);
});
$("#img").hide();
});
</script>
</head>
</html>
如有任何帮助,我们将不胜感激。谢谢!
最佳答案
这部分
$("#id").html(data);
表示 jQuery 正在使用 Python 脚本的响应填充 ID 为“id”的元素。问题是您没有具有 id="id"
的元素.
你还想做的是把$("#img").hide();
在 $.post
的成功处理程序中.这样当 $.post
时图像被隐藏已完成。使用您当前的代码,它会立即隐藏,因为 $.post
是一个异步请求,因此任何其他代码都不会等待它。
您的代码应如下所示:
<html>
<head>
<img id="img" src="loader.gif">
<div id="id"></div>
<script src="assets/js/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
url: "http://localhost/test.py";
$.post(url, function(data){
$("#id").html(data);
$("#img").hide();
});
});
</script>
</head>
</html>
注意 <div>
我已经添加了(您可以将其替换为您想要的任何其他 HTML 元素)。
更新
您的 $.post
有可能由于不同的原因,请求失败。目前,您只有一个成功处理程序,只有在请求成功时才会调用它。您可以像这样添加“不成功”处理程序:
$.post(url, function(data){
$("#id").html(data);
$("#img").hide();
})
.fail(function(response) {
alert('Error: ' + response.responseText);
// .. do something else ..
});
关于javascript - jQuery 在执行 Python 脚本 PHP 时加载 img,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40417587/