简体   繁体   中英

Setting a div to display:none; using javascript or jQuery

I have a really quick question that should be an easy answer.

I have a mobile navigation menu that uses jQuery to "drill down" menu levels. Each time a level is loaded, the jQuery determines the menu's height, and sets it accordingly.

I am using the following script on a button to toggle show and hide the main menu based on the current page:

<script type="text/javascript">
    (function ($) {
        $("a.BNnavTrigger").click(function (event) {
            event.preventDefault();
            $("div.drill-down-wrapper").slideToggle("9000");
        });
    })(jQuery);     
</script>

In order for the menu to start "closed" on certain pages, I know that I must give the css value of display:none; to the containing div. This is all working fine with one small problem.

When I initially hide the menu, the jQuery drilldown menu plugin detects that it is hidden, and sets the menu height to 0px. When I toggle the menu on a page that had the menu initially hidden, all the other content in the pane toggles correctly, but the menu itself is stuck at a 0 height.

In my brain, the obvious answer to this problem would be to hide the containing menu div ,via javascript, after the menu has been loaded and it's height set. Does this sound correct?

Could anyone provide me with a little script to do this? I am a complete and utter noob with js. Does anyone else have a different recommendation as to how to handle this issue?

Thanks for your time! Alex

UPDATE!

JS fiddle for the issue here: http://jsfiddle.net/fyyG2/17/ *

On document ready do $("#yourId").hide(); .

For example:

$(document).ready(function () {

     $("div.drill-down-wrapper").hide();
     var $drillDown = $("#drilldown");

        // (what ever your code is)
 });

Try this way:

$("#yourId").css("display","none");

it should work it!

I hope it helps.

Adding css value display:none to your css file works just fine. http://jsfiddle.net/dxkX6/4/

but I can see that you have a typo in your html file:

$("a.BNnavTrigger").click(function (event) {
       ^

<a class="BNavTrigger">Toggle Menu</a>
        ^

is that possibly the reason why you are experiencing the problem?

I'm not sure that I fully understood your problem, but here's a thought:

Set the opacity of what you wish to hide to 0, instead of changing it's size / hiding it. This way it will keep its dimensions but won't be seen to the users:

$('#id').css('opacity',0);

And to re-show the element:

$('#id').css('opacity',1);

尝试:

$('#id').style = "display: none;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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