最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is jquery show() not working in example - Stack Overflow

programmeradmin0浏览0评论

Please consider this Plunk. It's purpose is to provide a very simple simulation of a loading mechanism.

I have a simple set up:

  <body>
    <div id="loading">
      <img src=".main/gfx/loading_spinner.gif"/>
    </div>
    <div id="content">
      <h3>Content fully loaded.</h3>
    </div>
  </body>

Where content is hidden through CSS:

body 
{
  width: 100%;
  height: 100%;
}

#loading {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  text-align: center;
}

#content {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  visibility: hidden;
}

The Javascript is equally simple:

$(window).ready(function() 
{
  simulateLongLoad();
  $('#content').show();
});

function simulateLongLoad() 
{
  setTimeout(showContent, 2000);
}

function showContent() 
{
  $('#loading').hide();
  $('#content').show();
}

But, for some reason the .show() of #content doesn't work. Any idea why?

PS: Likely something very stupid, but I don't see it.

Please consider this Plunk. It's purpose is to provide a very simple simulation of a loading mechanism.

I have a simple set up:

  <body>
    <div id="loading">
      <img src="http://www.arabianbusiness.com/skins/ab.main/gfx/loading_spinner.gif"/>
    </div>
    <div id="content">
      <h3>Content fully loaded.</h3>
    </div>
  </body>

Where content is hidden through CSS:

body 
{
  width: 100%;
  height: 100%;
}

#loading {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  text-align: center;
}

#content {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  visibility: hidden;
}

The Javascript is equally simple:

$(window).ready(function() 
{
  simulateLongLoad();
  $('#content').show();
});

function simulateLongLoad() 
{
  setTimeout(showContent, 2000);
}

function showContent() 
{
  $('#loading').hide();
  $('#content').show();
}

But, for some reason the .show() of #content doesn't work. Any idea why?

PS: Likely something very stupid, but I don't see it.

Share Improve this question asked Jan 12, 2016 at 12:46 kbdkbd 4,4497 gold badges37 silver badges78 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14

in css write display:none; and not visibility

Read More about the difference over Here

For Explanation :

display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). There will be no space allocated for it between the other tags.

visibility:hidden means that unlike display:none, the tag is not visible, but space is allocated for it on the page. The tag is rendered, it just isn't seen on the page.

But if you want to use visibility , and want to show/hide using JQuery,then use below

$('#element').css('visibility', 'visible'); //to show
$('#element').css('visibility', 'hidden'); //to hide

You need to change visibility:hidden to display:none and remove your second line of JS (you run the same code 2 times).

 $('#content').show(); 

this code adds css to #content elemment display:block not visibility:visible. If you want to use visibility:hidden style you must change your code to be

$('#content').css('visibility','visible'); 

your content visibility: hidden should be display: none and your second line of JS in the onload was not needed, see here:

http://plnkr.co/edit/MHbBjuCEVrIGpKarooLF

According to jQuery, (...)This is roughly equivalent to calling .css( "display", "block")

So, this function wont change the css property 'visibility'

发布评论

评论列表(0)

  1. 暂无评论