简体   繁体   English

使用JavaScript隐藏div

[英]Using javascript to hide div

I am using Javascript to hide a search box until a box is clicked. 我正在使用Javascript隐藏搜索框,直到单击一个框为止。 That works fine. 很好

However when the page is first loaded, you can see the search box there and then it disappears once the page has fully loaded. 但是,第一次加载页面时,您可以在此处看到搜索框,一旦页面完全加载,搜索框就会消失。

How can I make it hide and not show at all until my button is clicked.. 在单击我的按钮之前,如何隐藏它而不显示它。

This is the Javascript: 这是Javascript:

<script type="text/javascript">
$(function(){
$(".search").hide();
$(".clicktosearch").on("click", function(){
    $(".search").slideToggle("600");

.search is the actual search box .clicktosearch is the box the user must click for the actual search box to show up. .search是实际的搜索框。clicktosearch是用户必须单击才能显示的实际搜索框。

They only thing I have tried is to move the Javascript above the html box but, to no luck, now I am asking you all on SOF. 我唯一尝试的是将Javascript移到html框上方,但是,不走运,现在我要在SOF上向大家提问。

You need to use display:none in the CSS to hide it initially. 您需要在CSS中使用display:none将其初始隐藏。 The javascript only executes after the page has loaded so you will always see it briefly before the javascript kicks in. javascript仅在页面加载后执行,因此您始终会在javascript启动之前短暂看到它。

Hide it using css . 使用CSS隐藏它。

.search { display:none;}

The reason for this happening is the javascript getting loaded after the css and HTML take affect. 发生这种情况的原因是在CSS和HTML生效后加载了javascript。 Hide it using CSS, the time lag before the div hides will be less. 使用CSS将其隐藏,div隐藏之前的时间将减少。

You have the .hide inside of document.ready, $(function(){ is short for $(document).ready(function() { 您在document.ready内部具有.hide, $(function(){$(document).ready(function() {缩写$(document).ready(function() {

So just put it before the document.ready and it should work... 因此,只需将其放在document.ready之前,它应该可以工作...

<script type="text/javascript">
$(".search").hide();         //moved this line too here
$(function(){
$(".clicktosearch").on("click", function(){
    $(".searchr").slideToggle("600");

although, What I personally would prefer is just in your css hide it. 尽管,我个人更希望将其隐藏在CSS中。

Like so, 像这样

.search { display: none; }

You'll have to touch the CSS. 您必须触摸CSS。

.search {
  height: 0px;
}

Then in your JavaScript: 然后在您的JavaScript中:

$(".clicktosearch").on("click", function(){
    $(".search").slideToggle("600");
});

This is what you need: 这是您需要的:

CSS CSS

.search { display:none;}

JS JS

$(".clicktosearch").on("click", function(){
    $(".search").slideToggle("600");
});

I removed $(".search").hide(); 我删除了$(".search").hide(); because it's unneeded and may cause other problems. 因为它是不必要的,并且可能导致其他问题。 (JQuery doesn't need to hide something that's already hidden via CSS.) (JQuery不需要隐藏已经通过CSS隐藏的内容。)

BTW: If there's only one element that matches .search , it should really be an id and so #search . BTW:如果只有一个匹配元素的.search ,它应该是一个id等等#search Same goes for .clicktosearch . .clicktosearch

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

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