简体   繁体   English

在加载另一个div时隐藏div

[英]hide div when another div is loaded

I have this : 我有这个 :

window.onload = function() {
    $("#show").load("show.php");
}

and my html : 和我的html:

<div id='loader'>Loading Data</div>
<div id='show'></div>
<div id='other'>some text.........</div>

What I want is, on page loaded, html show <div id='loader'> and <div id='other'> when JS/JQuery success loaded the <div id='show'> then hide <div id='loader'> 我想要的是,在页面加载时,当JS / JQuery成功加载<div id='show'>隐藏html show <div id='loader'><div id='other'> <div id='show'>然后隐藏<div id='loader'>

Can you help me? 你能帮助我吗?

.load() provide a callback you can use this. .load()提供一个可以使用的callback Just updated your js with following 刚刚使用以下更新了您的js

$("#show").load("show.php", function(){
   $('#loader, #show').toggle(); 
});

load() has a callback that runs when it is complete. load()具有一个在完成时运行的回调。 Use this to show and hide the relevant divs. 使用它可以显示和隐藏相关的div。 You should also use jQuery ready function instead of window.onload so that the page won't have to wait for all resources (images etc) before it can load in the php: 您还应该使用jQuery ready函数而不是window.onload,以便页面不必等待所有资源(图像等)就可以在php中加载:

$(document).ready(function() {
    $("#show").load("show.php", function() {
        $('#loader').hide();
    });
});

EDIT: Super User's answer provides a better solution than using .hide(), wish I'd thought of it! 编辑:超级用户的答案比使用.hide()提供了更好的解决方案,希望我能想到!

as @techLove's comment here , I found this working on me. 就像@techLove 在这里的评论一样,我发现它对我有效。 I modified his answer into this: 我将他的答案修改为:
$( "#show" ).load( "show.php", function() {$("#loader").css("display","none");});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM