[英]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.