简体   繁体   中英

How to show div content in HTML when using BootStrap 3 navbar items

I am working with BootStrap 3 CSS styling and have created the following:

<div class="navbar-header">
    <button type="button" class="navbar navbar-default" role="navigation">
        <span class="sr-only">Toggle navigation></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button> 
    <a class="navbar-brand" href="#home">Home</a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse-1">
                <ul class="nav navbar-nav">
                       <li><a href="#mywork" onclick="return show_div('mywork');">My Work</a></li>
                       <li><a href="#myprofile">Profile</a></li>
                       <li class="dropdown">
                           <a href="#other" data-toggle="dropdown" class="dropdown-toggle">Other actions <b class="caret"></b></a>
                           <ul class="dropdown-menu">
                               <li><a href="#contactme">Contact Me</a></li>
                           </ul>
                       </li>
                </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                                <a href="#actions" data-toggle="dropdown" class="dropdown-toggle">Actions <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#updateprofile">Update profile</a></li>
                                    <li><a href="#logout">Logout</a></li>
                                </ul>
                        </li>
                </ul>
        </div>
</div>

<div id="home" style="display: none;">
    Test Data
</div>

And from this I am trying to display the div at the bottom which I have given the id "home" which corresponds to the #home href given on the nav bar.

The trouble I am having is that I cannot show the div with the id of home when the Home element of the navbar is clicked, am I doing something wrong or can someone point me in the right direction? I have spent a lot of time trying to work this out but cannot.

EDIT:

JavaScript show_div function:

<script>
    //Functon to show divs from the nav menu
    function show_div(toShow)
    {
        var show = document.getElementById(toShow);
        show.style.display = "";
    }
</script>

Your anchor onclick event is sending the wrong id

<li><a href="#mywork" onclick="return show_div('mywork');">My Work</a></li>

It should be sending home to target the div tag with your test data:

<div id="home" style="display: none;">
    Test Data
</div>

Change the onclick to:

<li><a href="#mywork" onclick="show_div('home');">My Work</a></li>

I hope this helps. Happy coding!

Why you don't want to use jQuery like this:

<a id="sh" style="cursor:pointer">Show Home</a>
<div id="home" style="display: none;">Test Data</div>

<script type="text\javascript">
$(document).ready(function () {
    $("#sh").click(function () {
        $("#home").toggle();
    });
});
</script>

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