简体   繁体   English

在导航元素上显示Div并单击并隐藏其他Div

[英]Show Div on Nav Element Click and Hide Other Divs

I built a navbar in HTML and also some Divs which are populated with a bunch of content that pertains to each nav element. 我用HTML构建了一个导航栏,还创建了一些Div,其中填充了与每个nav元素有关的一堆内容。

What I want to do is show the div which pertains to the selected nav element when it is clicked and hide the other divs that pertain to the other nav elements. 我想做的是单击时显示与所选nav元素有关的div,并隐藏与其他nav元素有关的其他div。

Basically what needs to happen is when a user clicks on a Nav Item, the class for that nav item needs to be set to 'navItem active on' in the html. 基本上需要发生的是,当用户单击导航项目时,该导航项目的类需要在html中设置为“ navItem active on”。 Not sure if this is something that happens automatically or not. 不知道这是否会自动发生。

After that, the display property defined in the CSS for the content panel of that nav item needs to be changed to 'block' and all other content panels should then have their 'display' property changed to 'none' so that they are not displayed in the page. 之后,需要将CSS中为该导航项的内容面板定义的显示属性更改为“阻止”,然后将所有其他内容面板的“显示”属性更改为“无”,以便不显示它们在页面中。

In the example given, I only have two content panels defined in the CSS and HTML (Capabilities and Tutorials), but each navItem will receive it's own content panel which should be toggled on when it is clicked. 在给出的示例中,我只有两个在CSS和HTML中定义的内容面板(功能和教程),但是每个navItem都会收到它自己的内容面板,单击该内容面板时应将其打开。

I really have no idea where to begin with this. 我真的不知道从哪里开始。 I'm pretty sure this requires JavaScript but this is literally my first attempt at building a web page and it took me 2 days even after copying a lot from another website I used for inspiration. 我很确定这需要JavaScript,但这实际上是我第一次构建网页,即使从另一个我启发的网站上复制了很多内容,我也花了2天的时间。 Any help, guidance or insight is greatly appreciated. 任何帮助,指导或见解,我们将不胜感激。

CSS + HTML: CSS + HTML:

 var links = document.getElementsByClassName("navItem"); for (i = 0; i < links.length; i++) { var link = links[i]; link.addEventListener('click',function(sender, event) { event.preventDefault(); /* hide all panels */ var panels = document.getElementsByClassName("panel"); for (j = 0; j < panels.length; j++) { panels[j].style.display = 'none'; } /* Show the selected panel */ var panel_id = sender.target.getAttribute("panel-id"); document.getElementById(panel_id).style.display = 'block'; } } 
 /* FONT ASSIGNMENTS --------------------------- */ /* General Use */ a { text-decoration: none; } a:hover { text-decoration: underline; } p, small { font-family: 'Avenir LT W01 35 Light', Arial, Helvetica, sans-serif; } p.large-text { font-size: 18px !important; } hr { background-color: #e0e0e0; color: #e0e0e0; } .center-content { text-align: center !important; } /* Special Use */ h1, h2, h1 a, h2 a, h3, h3 a, infoBar, .gisFont1, .gisFont1 a { font-weight: normal !important; font-style: normal; line-height: normal; font-variant: normal; font-family: 'Avenir LT W01 35 Light', Arial, Helvetica, sans-serif; } /*-- END FONT ASSIGNMENTS --*/ /* INFOBAR - The Infobar is the navigation element at the top used to navigate the subpages of the document and change the content panel's content depending on the selected infoBar navigation element --------------------------- */ /* infoBar Bottom Border */ #infoBar { background: #FFF; /*border-top: 1px solid #e5e5e5;*/ border-bottom: 1px solid #e5e5e5; max-width: 940px; text-align: center; /*display: table;*/ margin: 0 auto; } /* infoBar Bottom Border onHover or Active element*/ #infoBar a:hover, #infoBar a.active { border-bottom: 4px solid #2889DE; text-decoration: none; } /* infoBar Link Text */ #infoBar a { background: transparent; color: #000; display: inline-block; font-size: 16px; font-family: 'Avenir LT W01 35 Light', Arial, Helvetica, sans-serif; padding: 1.4em; text-decoration: none; } /* infoBar Link Text onHover */ #infoBar a:Hover { background: transparent; color: #2889DE; display: inline-block; font-size: 16px; font-family: 'Avenir LT W01 35 Light', Arial, Helvetica, sans-serif; padding: 1.4em; text-decoration: none; } /* infoBar Active element */ #infoBar a.active { font-family: 'Avenir LT W01 65 Medium', Arial, Helvetica, sans-serif; } /* Media Queries */ @media screen and (max-width: 960px) { #infoBar a { font-size: 14px; } } @media screen and (max-width: 830px) { #infoBar a { padding: 1em 0.6em; } } @media screen and (max-width: 760px) { #infoBar { display: none; } } /*-- END INFOBAR --*/ /* PAGE SECTIONS --------------------------- */ /* Page Section Styling */ .page-section { background-position: center top; color: #4d4d4d; min-height: 200px; padding: 60px 0; text-align: center; width: 940px; margin: auto; } /* Page Section - Header2 Styling */ .page-section h2 { font-size: 24px; margin-bottom: 20px; } /* Page Section Paragraph Styling */ .page-section p { color: #333; font-size: 18px; line-height: 1.5; /*margin: 10px 0 45px 0;*/ } /* Foreword-Section-Top Styling */ .foreword-section-top { padding: 0; min-height: 160px; } /* Foreword-Section-Top Header1 Styling*/ .foreword-section-top h1 { color: #222; font-size: 36px; } /* Foreword-Section-Top Paragraph Styling */ .foreword-section-top p { color: #4d4d4d; margin-bottom: 10px; } .grid-100 { width: 100%; margin: auto; } /* CONTENT PANELS ----------------------------- */ /* Capabilties Panel*/ #capabilities-panel { max-width: 980px; margin: 0 auto; display:block; } /*Tutorials Panel */ #tutorials-panel { max-width: 980px; margin: 0 auto; display:none; } .product-row { margin-bottom: 50px; /*width: 100%; */ max-width: 940px; margin: 0 auto; display: inline-block; } .product-box { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3); float: left; overflow: hidden; margin: 0.5%; position: relative; text-align: center; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3); width: 24%; } @media screen and (max-width: 960px) { .product-box { width: 48%; } } @media screen and (max-width: 480px) { .product-box { display: block; float: none; margin: 10px auto; width: 95%; } } .product-box a { color: #FFF; display: block; font-weight: bold; } .product-box a:hover .inner-box-padding { position: relative; width: 100%; -ms-transform: scale(1.1); -moz-transform: scale(1.1); -webkit-transform: scale(1.1); transform: scale(1.1); } .product-box .inner-box-padding { background-color: #007ac2; background-position: center center; background-repeat: no-repeat; background-size: cover; -ms-transition: all .2s ease-in-out; -moz-transition: all .2s ease-in-out; -webkit-transition: all .2s ease-in-out; transition: all .2s ease-in-out; width: 100%; } .product-box .inner-box-padding:before { content: ""; display: block; padding-top: 87%; } .product-box h3 { font-size: 22px; color: #FFF; position: absolute; left: 0; right: 0; bottom: 0; } .product-box.dark-text h3 { color: #333; } .product-box .capability-one { background-image: url('http://i64.tinypic.com/2mi16l1.png'); } .product-box .capability-two { background-image: url('http://i68.tinypic.com/10gwm75.png'); } .product-box .capability-three { background-image: url('http://i65.tinypic.com/5djxwh.png'); } .product-box .capability-four { background-image: url('http://i67.tinypic.com/15e7hu8.png'); } .product-box .tutorial-one { background-image: url('http://i68.tinypic.com/efhvfc.png'); } .product-box .tutorial-two { background-image: url('http://i66.tinypic.com/50199u.png'); } .product-box .tutorial-three { background-image: url('http://i63.tinypic.com/wvwcif.png'); } .product-box .tutorial-four { background-image: url('http://i67.tinypic.com/1zp1or8.png'); } /* END PRODUCT BOXES */ 
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <!-- include jQuery --> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <!-- INFO BAR --> <div id="infoBar"> <a href="/intro/capabilities" class="navItem active on" panel-id="capabilities-panel">Capabilities</a> <a href="/intro/tutorials" class="navItem" panel-id="tutorials-panel">Tutorials</a> <a href="/intro/use-cases" class="navItem">Use Cases</a> <a href="/intro/services" class="navItem">Services</a> <a href="/intro/security" class="navItem">Security</a> <a href="/intro/whats-new" class="navItem">What's New</a> <a href="/intro/request-access" class="navItem">Request Access</a> </div> <!-- END GIS INFO BAR --> <!-- FOREWORD --> <div class="page-section foreword-section-top"> <h1>Some Cool Tagline</h1> <p>blah blah blah. We're so awesome. Now give us money.</p> </div> <!-- CAPABILITIES PANEL --> <div id="capabilities-panel" class="panel"> <!-- Capability One --> <div class="product-box"> <a href="/capabilities/capability-one"> <div class="inner-box-padding capability-one"></div> <h3>Capability 1</h3> </a> </div> <!-- Capability Two box --> <div class="product-box dark-text"> <a href="/capabilities/capability-two"> <div class="inner-box-padding capability-two"></div> <h3>Capability 2</h3> </a> </div> <!-- Capability Three box --> <div class="product-box"> <a href="/capabilities/capability-three"> <div class="inner-box-padding capability-three"></div> <h3>Capability 3</h3> </a> </div> <!-- Capability Four box --> <div class="product-box"> <a href="/capabilities/capability-four"> <div class="inner-box-padding capability-four"></div> <h3>Capability 4</h3> </a> </div> </div> <!-- END CAPABILITIES PANEL --> <!-- TUTORIALS PANEL --> <div id="tutorials-panel" class="panel"> <!-- Tutorial One box --> <div class="product-box"> <a href="/tutorials/tutorial-one"> <div class="inner-box-padding tutorial-one"></div> <h3>Tutorial 1</h3> </a> </div> <!-- Tutorial Two box --> <div class="product-box dark-text"> <a href="/tutorials/tutorial-two"> <div class="inner-box-padding tutorial-two"></div> <h3>Tutorial 2</h3> </a> </div> <!-- Tutorial Three box --> <div class="product-box"> <a href="/tutorials/tutorial-three"> <div class="inner-box-padding tutorial-three"></div> <h3>Tutorial 3</h3> </a> </div> <!-- Tutorial Four box --> <div class="product-box"> <a href="/tutorials/tutorial-four"> <div class="inner-box-padding tutorial-four"></div> <h3>Tutorial 3</h3> </a> </div> </div> <!-- END TUTORIALS PANEL --> 

Pretty sure that this might do something like you'd want. 可以肯定的是,这可能会做您想要的事情。 There are more elegant pieces of code (not to mention that there should be loads of plugins) for this though, this is just out of the top of my head (dont use $.attr to find the corresponding panel etc). 尽管有很多优雅的代码片段(更不用说应该有大量的插件了),但这只是我的脑子(不要用$ .attr查找对应的面板等)。

$(document).ready(function(){
    $(".navItem").click(function(event) {
        event.preventDefault();

        $('.navItem').removeClass("active").removeClass("on");
        $(this).addClass("active").addClass("on");
        var panel = $(this).attr('panel-id');

        $(".panel").hide();
        $("#"+panel).show();
    });
});

In order to use this script you need to import jQuery into your page though, which literally putting 1 line in your page (just google that). 为了使用此脚本,您需要将jQuery导入到页面中,这实际上会将1行插入页面(只是google)。

You need to give every panel the class panel (ie instead of just the 'id' attribute. This will allow for the $(".panel") to find all html that is in a 您需要为每个面板提供类面板(即,不只是'id'属性。这将允许$(“。panel”)查找位于

The var panel = $(this).attr('panel-id'); var panel = $(this).attr('panel-id'); line finds the panel belonging to the anchor the user clicked, as long as you add an attribute to each anchor containing the id of the corresponding panel as the value (eg <a (..) panel-id="capabilities-panel"> ) 行会找到属于用户单击的锚的面板,只要您向每个锚添加一个属性,该属性包含相应面板的ID作为值(例如<a (..) panel-id="capabilities-panel">

======= =======

Updated answer so OP can use vanilla javascript per his request. 更新了答案,因此OP可以根据他的请求使用香草javascript。

   (function () {
        alert('hello');
        var links = document.getElementsByClassName("navItem");
        for (i = 0; i < links.length; i++) {
            var link = links[i];
            link.addEventListener('click',function(event) {
                event.preventDefault();

                for(int k = 0; k < links; k++) {
                    links[k].className = "navItem";
                }
                event.target.className += " active on";


                var panels = document.getElementsByClassName("panel");
                for (j = 0; j < panels.length; j++) {
                    panels[j].style.display = 'none';
                }
                var panel_id = event.target.getAttribute("panel-id");
                document.getElementById(panel_id).style.display = 'block';
            });
        }
    })();

I haven't tested this so there's probably some syntactic errors here and there, and Im not too sure how one gets the sender element from a click event in vanilla javascript (though this shouldn't be too hard to google). 我还没有测试过,所以可能到处都是语法错误,而且我不太确定是如何从普通javascript的click事件中获取sender元素的(尽管对Google来说应该不太难)。 You need to put scripts in between <script></script> tags in order for your browser to recognize javascript. 您需要将脚本放在<script></script>标记之间,以便浏览器识别javascript。

Note how the number of lines and readability decreased by abandoning jQuery. 注意通过放弃jQuery如何减少行数和可读性。

Hope this helps you! 希望这对您有所帮助!

Also, if this is just not working for you I recommend checking out the link the other guy posted under your question. 另外,如果这对您不起作用,我建议您查看其他人在您的问题下发布的链接。

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

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