javascript - 为什么 jquery show() 在示例中不起作用

标签 javascript jquery html css

请考虑这个Plunk .它的目的是提供一个非常简单的加载机制模拟。

我有一个简单的设置:

  <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>

通过 CSS 隐藏 content 的地方:

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;
}

Javascript 同样简单:

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

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

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

但是,由于某些原因,#content 的 .show() 不起作用。知道为什么吗?

PS:可能是非常愚蠢的东西,但我没看到。

最佳答案

在 css 中写 display:none; 而不是 visibility

通过 Here 阅读更多关于差异的信息

解释:

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.

但是如果你想使用 visibility ,并且想使用 JQuery 显示/隐藏,那么使用下面

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

关于javascript - 为什么 jquery show() 在示例中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34744105/

相关文章:

javascript - 为什么不能从句柄中拖动 jQuery UI 可排序元素?

javascript - 如何使用 jquery 或 javascript 隐藏空的 href 值按钮

jQuery - 检查类是否存在,如果不破坏函数

html - 为什么我们不能发布未选中的复选框?

javascript - 以数字开头的 pug/jade 类名

javascript - 推荐的解决方案让 AngularJS 识别浏览器已检测到的无效输入?

javascript - JavaScript 有非对称加密选项吗?

javascript - 我如何确定我的 Ajax 调用已完全完成?

javascript - 具有公共(public)和私有(private)方法的 jQuery 插件模板

php - 使用 PHP 和 mysql Ajax 提交