简体   繁体   English

数组为空时隐藏div

[英]Hide div when array is empty

I have an array set up that has something added to the array every time to drop a word into a bucket on the page. 我有一个数组设置,每次都有一些东西添加到数组中,将一个单词放入页面上的一个桶中。 I'm trying to show and hide certain div's depending on how many objects are in the array. 我试图显示和隐藏某些div,具体取决于数组中有多少个对象。

My code is: 我的代码是:

if (test > 5){
    $(".moving").hide();
    $("#done").show();
}

This works perfectly except when the page first loads. 除非页面首次加载,否则此操作非常有效。 The div with ID #done is still showing when the page first loads and then goes away when the array gets it's first object. ID #done的div在页面首次加载时仍然显示,然后在数组获得第一个对象时消失。 (Array starts empty) (数组开始为空)

In your css just add #done{display: none;} That way the div will not show when page first loads. 在你的CSS中只需添加#done{display: none;}这样,当页面首次加载时div不会显示。

Or use #done{visibility: hidden;} if you just want the div not to be visible. 或者如果您只想让div不可见,请使用#done{visibility: hidden;}

If you don't have access to the HTML code you could hide it in ready function : 如果您无权访问HTML代码,则可以将其隐藏在ready函数中:

$(function(){
    $("#done").hide();
    //Or
    $("#done").css("display","none");

    //The rest of code
});

Hope this helps. 希望这可以帮助。

Use : #done{display:none;} or #done{opacity:0;} 使用: #done{display:none;}#done{opacity:0;}

Later in code, whenever you want to display it, you may use js/css to change display to block or opacity to 1 . 稍后在代码中,无论何时想要显示它,都可以使用js / css将display更改为blockopacity更改为1

The following function will hide done div and show moving div when page is ready after complete page is rendered: 以下函数将隐藏完成的div并在完成页面呈现后页面准备就绪时显示移动div:

$(document).ready(function(){
  $("#done").hide();
  $(".moving").show();
});

Similarly you can use load method in to run a function on page load. 同样,您可以使用 load 方法在页面加载时运行函数。 but be aware load method is executed before complete page is rendered 但要注意在完成页面呈现之前执行load方法

In your current code, you can add the following at the very beginning of ready function, 在当前代码中,您可以在ready函数的最开头添加以下内容,
$('#done').hide(); $( '#做')隐藏()。
or 要么
$('#done').css('visibility','hidden'); $( '#做')的CSS( '知名度', '隐藏')。

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

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