Hide div by default and show it on click with bootstrap

后端 未结 4 866
深忆病人
深忆病人 2020-12-22 19:10

I want to show and hide a div, but I want it to be hidden by default and to be able to show and hide it on click. Here is the code that I have made :



        
4条回答
  •  盖世英雄少女心
    2020-12-22 19:18

    Here I propose a way to do this exclusively using the Bootstrap framework built-in functionality.

    1. You need to make sure the target div has an ID.
    2. Bootstrap has a class "collapse", this will hide your block by default. If you want your div to be collapsible AND be shown by default you need to add "in" class to the collapse. Otherwise the toggle behavior will not work properly.
    3. Then, on your hyperlink (also works for buttons), add an href attribute that points to your target div.
    4. Finally, add the attribute data-toggle="collapse" to instruct Bootstrap to add an appropriate toggle script to this tag.

    Here is a code sample than can be copy-pasted directly on a page that already includes Bootstrap framework (up to version 3.4.1):

    Toggle Foo
    
    
    This div (Foo) is hidden by default
    This div (Bar) is shown by default and can toggle

提交回复
热议问题