Php 如何在不加载整个页面的情况下加载单个Div并显示加载状态?
如何在不影响当前页面的情况下单独加载单个Div,并使用PHP和MySQL或Ajax和SQL显示该Div的加载状态使用PHP+Ajax+MySQL创建一个包含该Div的“.PHP”文件,调用其中必须调用该.PHP(不要为文件命名,这只是一个参考示例)文件的Ajax函数。。将根据事件调用显示…我执行以下操作: 首先,您有一个隐藏的div,其中有一个加载if和一个加载按钮:Php 如何在不加载整个页面的情况下加载单个Div并显示加载状态?,php,mysql,ajax,Php,Mysql,Ajax,如何在不影响当前页面的情况下单独加载单个Div,并使用PHP和MySQL或Ajax和SQL显示该Div的加载状态使用PHP+Ajax+MySQL创建一个包含该Div的“.PHP”文件,调用其中必须调用该.PHP(不要为文件命名,这只是一个参考示例)文件的Ajax函数。。将根据事件调用显示…我执行以下操作: 首先,您有一个隐藏的div,其中有一个加载if和一个加载按钮: <div id="displayDiv" style="display: none"> <img id="
<div id="displayDiv" style="display: none">
<img id="loadingGif" src="loadingGif" style="display:none"; />
<div id="actualContent" style="display:none" />
</div>
<input type="button" id="loadButton" />
然后就是JS代码(我使用jQuery)
$(文档).ready(onDocumentReady);//这将在页面加载之前运行
函数onDocumentReady()
{
$(“#加载按钮”)。单击(onLoadClick);//在按钮单击时分配操作
}
函数onLoadClick()
{
$('#loadingGif').show();//显示正在加载的gif。只要其父级被隐藏,它就不会显示
$(“#actualContent”).hide();//隐藏响应的实际内容;
$('#displayDiv').show();//显示div
$.get(“test.php”,onRequestComplete);//向独立的php文件发出ajax请求
//只要内容加载,加载的gif就会显示出来;
}
函数onRequestComplete(数据)
{
$('#loadingGif').hide();
$('#actualContent').html(数据);
$(“#实际内容”).show();
}
所以。您有一个容器“displayDiv”;里面有一个图像“loadingGIf”和另一个容器“actualContent”;当您单击“加载”按钮时,将显示带有加载gif的大容器,通知用户有东西正在加载。加载内容时,只需隐藏加载gif,并在“actualContent”gif中显示信息。在test.php中,您只需回显div中必须出现的内容。我建议使用JSON,但您将阅读更多关于它的内容
希望这能有所帮助。正确地重新组织您的问题,并提供有关何时或在何种情况下显示此问题的更多信息。
<script type="text/javascript">
$(document).ready( onDocumentReady); // this runs before page load
function onDocumentReady()
{
$('#loadButton').click( onLoadClick ); //assign action on button click
}
function onLoadClick()
{
$('#loadingGif').show(); // show the loading gif. It won't show as long as it's parent is hidden
$('#actualContent').hide(); // hide the actual content of the response;
$('#displayDiv').show(); // display the div
$.get("test.php", onRequestComplete ); // make the ajax request to the stand alone PHP file
//so as long as the content loads, the loading gif will show;
}
function onRequestComplete( data )
{
$('#loadingGif').hide();
$('#actualContent').html( data );
$('#actualContent').show();
}
</script>