简体   繁体   English

显示其他部分时如何隐藏部分?

[英]How do I hide a section when I show a different section?

Basically I want one section to be shown at first as the home page. 基本上,我希望首先将一个部分显示为主页。 When I click on a link, I want that section to be shown, but everything else hidden, and so on. 当我单击链接时,我希望显示该部分,但其他所有内容都隐藏,依此类推。 Every time I click a link, I only want the clicked on link's section to be shown. 每次单击链接时,我只希望显示单击的链接部分。

I am close, but stuck. 我很近,但是被卡住了。 Here is my HTML: 这是我的HTML:

<a href="#about" id="about" class="text-center" title="">About Me</a>                   
<a href="#contact" id="contact" class="text-center" title="">Contact</a>

<div class="container-fluid">
    <div class="container about showing">
        <h2>About Me</h2>
    </div>
</div>

<div class="container-fluid">
    <div class="container about showing">
        <h2>Contact</h2>
    </div>
</div>

And here is my script: 这是我的脚本:

$(document).ready(function() {

$('.about').hide();
$('.contact').hide();

$('.showing').show();

$('#contact').on('click', function(e) {
    e.preventDefault();
    $('div').removeClass('showing');
    $('.contact').addClass('showing');
    if ($('div').hasClass('showing')) {
        $('div').show();
    } else {
        $('div').hide();
    }
});

});

So it shows the 'About Me' section right away and everything else is hidden. 因此,它立即显示“关于我”部分,其他所有内容都被隐藏。 When I click the 'Contact' link it removes the 'showing' class from all my 'div's and adds the 'showing' class to my contact section. 当我单击“联系人”链接时,它将从我所有的“ div”中删除“正在显示”类,并将“正在显示”类添加到我的联系人部分。 I then check to see if the 'div' has the 'showing' class, if it does I have it show the 'div' otherwise I want it to hide it. 然后,我检查“ div”是否具有“ showing”类,如果确实有,则显示“ div”,否则我希望其将其隐藏。

However, when I click on the Contact link, it does show the Contact section, but it does not hide the About Me section. 但是,当我单击“联系人”链接时,它会显示“联系人”部分,但不会隐藏“关于我”部分。 I know this is a long post, I am just trying to make sure I explain everything correctly. 我知道这是一篇很长的文章,我只是想确保我正确地解释了所有内容。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

I would strongly suggest you use data fields and common classes to make it generic. 我强烈建议您使用数据字段和通用类使其通用。

 var $pages = $('.container'); $('.menu').on('click', function(e){ //remove showing from all containers $pages.removeClass('showing'); //put it on the container with the matching class of the data target $pages.filter('.'+ $(e.target).data('target')).addClass('showing'); }); 
 .container { display: none; } .container.showing { display: unset; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#about" class="menu text-center" data-target="about" title="">About Me</a> <a href="#contact" class="menu text-center" data-target="contact" title="">Contact</a> <a href="#other" class="menu text-center" data-target="other" title="">Other</a> <div class="container-fluid"> <div class="container home showing"> <h2>Home</h2> </div> </div> <div class="container-fluid"> <div class="container about"> <h2>About Me</h2> </div> </div> <div class="container-fluid"> <div class="container contact"> <h2>Contact</h2> </div> </div> <div class="container-fluid"> <div class="container other"> <h2>Other</h2> </div> </div> 

Here's a few things you can optimize: 您可以优化以下几点:

  $(document).ready(function() { $('.about, .contact').hide(); //put both of them in single selector $('.showing').show(); //this shows the only element that has "showing $('#contact, #about').on('click', function(e) { e.preventDefault(); var self = $(this); //reference to the object being clicked var target = $("." + self.attr('id')); //removes "showing" class from all others and hides them $(".container").not(target).removeClass("showing").hide(); //adds "showing" class to the new current item target.addClass("showing").show(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#about" id="about" class="text-center" title="">About Me</a> <a href="#contact" id="contact" class="text-center" title="">Contact</a> <div class="container-fluid"> <div class="container about showing"> <h2>About Me</h2> </div> </div> <div class="container-fluid"> <div class="container contact"> <!--you don't want that "showing" class here!--> <h2>Contact</h2> </div> </div> 

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

相关问题 如何显示和隐藏不同 <section> 使用jQuery我的html页面? - How can I show and hide different <section> of my html page using jquery? 如何显示和隐藏部分? - How to show and hide a section? 如果数据为空,如何隐藏可折叠 FAQ 的一部分? - How do I hide a section of a collapsible FAQ if the data is empty? 如何隐藏HTML部分而不删除它? - How do I hide a section of HTML without removing it? 单击链接到页面某个部分的菜单项时,如何隐藏一个汉堡菜单? - How do I get a hamburger menu to hide when clicking a menu item that links to a section on the page? 如何创建一个按钮,当单击该按钮时向下滚动到页面上的不同部分? - How do I create a button that when clicked scrolls down to a different section on the page? 单击后展开一个部分时如何折叠该部分的rest? - How do I collapse the rest of the section when one section is expanded after click? 单击标题时,如何隐藏页面的一部分并显示另一部分? jQuery的 - How to hide one section of the page and show the other section when a heading is clicked? jQuery 如何仅显示所需部分并使用 vanilla Java Script 隐藏所有其他部分? - How can I show only a desired section and hide all others with vanilla Java Script? 如何检测 React 组件是否已完全加载以显示或隐藏部分? - How can I detect if a React component is completely loaded to show ot to hide a section?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM