jQuery 默认隐藏 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16271972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:50:24  来源:igfitidea点击:

Hiding a div by default

jquery

提问by The Learner

I have a div that should be shown only when the user clicks the show button

我有一个 div 应该只在用户点击显示按钮时显示

$("div.toshow").show();

I have written in the body

我已经在正文中写了

<body>

<div class="toshow">
</div>
</body>

so by default when the page is displayed all this content is seen the user. Is there a way that I can hide it bydefault?

所以默认情况下,当页面显示时,所有这些内容都会被用户看到。有没有办法可以默认隐藏它?

回答by Shree

Yes you can can use stylelike this:

是的,您可以style像这样使用:

<div class="toshow" style="display:none"></div>

回答by PSL

You can use either Display or Visibility

您可以使用 Display 或 Visibility

display:none|block|etc.. // This will not preserve the space for the div.
visibility:visible|hidden //This will preserve the space for the div but wont be shown.

If you use display your $("div.toshow").show();will cause your element to jump up as the space wasn't preserved for it. That will not happen with Visibility.

如果你使用 display 你$("div.toshow").show();会导致你的元素跳起来,因为没有为它保留空间。Visibility 不会发生这种情况。

One way to do this is have your dispay assigned to the class

一种方法是将您的付款分配给班级

.toshow
{
  display:none;
}

in your script:-

在您的脚本中:-

$("div.toshow").show();

Or Just provide:-

或者只提供:-

<div class="toshow" style="display:none;">...

 $("div.toshow").show();

When you do a .show()jquery just adds display:blockinline style to your markup.

当您执行.show()jquery 时,只需将display:block内联样式添加到您的标记中。

回答by Jude Duran

You can use CSS display:

您可以使用 CSS display

.toshow
{
    display: none;
}

回答by Jude Duran

.toshow{
         display:none;
  }

or

或者

.toshow{
       visibility:hidden;
         }

回答by NINCOMPOOP

$('#myDiv').hide();

$("myDiv").css("visibility", "hidden");

.toshow {
  visibility:hidden;
  display:none;
 }

回答by basarat

To do it in the body:

在身体中做到这一点:

<body>

<div class="toshow" style="display:none">
</div>

</body>

Although I would avoid inline css

虽然我会避免内联 css

回答by Priyangna

You can use jQuery to change the CSS styles of 'toshow' class adding "display: none". for example

您可以使用 jQuery 更改“toshow”类的 CSS 样式,添加“display: none”。例如

$(function(){
    $('.toshow').css("display", "none");
});