[英]Hide a div when another div is shown
I have 4 divs, two of them are shown on click (link), and hidden the same way. 我有4个div,其中两个在点击(链接)上显示,并以相同的方式隐藏。 When I click the link for the other 2 divs, the first 2 should be hidden again and the other way around.
当我单击其他2个div的链接时,前2个应该再次隐藏,反之亦然。 Right now all 4 divs would be shown if the 2 links were clicked.
现在,如果单击2个链接,将显示所有4个div。
Easy: click link>show div; 简单:点击链接> show div; click second link>show second div while hiding fist div
单击第二个链接>显示第二个div同时隐藏拳头div
The 2 links: 2个链接:
<a class="show_hideAbout show_hideAboutArr" href="#" >About</a>
<a class="show_hideContact show_hideContactArr" href="#" >Contact</a>
First 2 divs: 前2个div:
<div class="slidingDivAbout">Some Content</div>
<div class="slidingDivAboutArr">
<img src="img/dropdownarrow.png" width="24" height="12" alt="">
</div>
Other 2 divs: 其他2个div:
<div class="slidingDivContact">Some Content</div>
<div class="slidingDivContactArr">
<img src="img/dropdownarrow.png" width="24" height="12" alt="">
</div>
And my script: 我的剧本:
$(document).ready(function() {
$(".slidingDivAbout").hide();
$(".show_hideAbout").show();
$('.show_hideAbout').click(function() {
$(".slidingDivAbout").slideToggle(350);
});
$(".slidingDivAboutArr").hide();
$(".show_hideAboutArr").show();
$('.show_hideAboutArr').click(function() {
$(".slidingDivAboutArr").fadeToggle("fast", "linear");
});
$(".slidingDivContact").hide();
$(".show_hideContact").show();
$('.show_hideContact').click(function() {
$(".slidingDivContact").slideToggle(350);
});
$(".slidingDivContactArr").hide();
$(".show_hideContactArr").show();
$('.show_hideContactArr').click(function() {
$(".slidingDivContactArr").fadeToggle("fast", "linear");
});
});
Without change your markup you can use this: 如果不更改您的标记,您可以使用:
$(document).ready(function() {
$(".slidingDivAbout, .slidingDivAboutArr, .slidingDivContact, .slidingDivContactArr").hide(0);
$('.show_hideAbout').click(function() {
$(".slidingDivContact").slideUp(300, function() {
$(".slidingDivContactArr").fadeOut(300, function() {
$(".slidingDivAbout").slideToggle(350, "linear", function() {
$(".slidingDivAboutArr").fadeToggle(350);
});
});
});
});
$('.show_hideContact').click(function() {
$(".slidingDivAbout").slideUp(300, function() {
$(".slidingDivAboutArr").fadeOut(300, function() {
$(".slidingDivContact").slideToggle(350, function() {
$(".slidingDivContactArr").fadeToggle(350, "linear");
});
});
});
});
});
Hi I would have done it like this jsfiddle example 嗨,我会像这个jsfiddle例子那样做
HTML HTML
<a class="show_hideAbout" href="#" >About</a>
<a class="show_hideContact" href="#" >Contact</a>
<div class="slidingDivAbout">Some Content1</div>
<div class="slidingDivAboutArr"><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
<div class="slidingDivContact">Some Content2</div>
<div class="slidingDivContactArr"><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
script 脚本
$(function(){
$('.slidingDivAbout').hide();
$('.slidingDivAboutArr').hide();
$('.slidingDivContact').hide();
$('.slidingDivContactArr').hide();
$('.show_hideAbout').click(function() {
$('.slidingDivAbout').show(350);
$('.slidingDivAboutArr').show()
$('.slidingDivContact').hide();
$('.slidingDivContactArr').hide();
});
$('.show_hideContact').click(function() {
$('.slidingDivContact').show(350);
$('.slidingDivContactArr').show('fast', 'linear');
$('.slidingDivAbout').hide();
$('.slidingDivAboutArr').hide();
});
});
Maybe try something like this? 也许尝试这样的事情?
<a class="show_hideAbout" href="#" >About</a>
<a class="show_hideContact" href="#" >Contact</a>
<div class="divAbout">
<div class="slidingDivAbout">Some Content</div>
<div class="slidingDivAboutArr"><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
</div>
<div class="divContact">
<div class="slidingDivContact">Some Content</div>
<div class="slidingDivContactArr"><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
</div>
And the javascript: 和javascript:
$(document).ready(function() {
$(".divAbout").hide();
$(".divContact").hide();
$('.show_hideAbout').click(function() {
if( $(".divContact").is(":visible") ) {
$(".divContact").slideToggle(350, 'linear', function(){
$(".divAbout").slideToggle(350, 'linear');
});
} else {
$(".divAbout").slideToggle(350, 'linear');
}
});
$('.show_hideContact').click(function() {
if( $(".divAbout").is(":visible") ) {
$(".divAbout").slideToggle(350, 'linear', function(){
$(".divContact").slideToggle(350, 'linear');
});
} else {
$(".divContact").slideToggle(350, 'linear');
}
});
});
for each link you should bind the click once. 对于每个链接,您应该绑定一次单击。
<a class="show_hideAbout show_hideAboutArr" href="#" >About</a>
<a class="show_hideContact show_hideContactArr" href="#" >Contact</a>
the 2 following are duplicates since they bind the click to the same tag. 以下2个是重复的,因为它们将点击绑定到同一个标签。 class="show_hideAbout show_hideAboutArr")
class =“show_hideAbout show_hideAboutArr”)
$('.show_hideAbout').click(function(){
$(".slidingDivAbout").slideToggle(350);
});
$('.show_hideAboutArr').click(function(){
$(".slidingDivAboutArr").fadeToggle("fast", "linear");
});
keep only one. 只保留一个。 and
和
$('.show_hideAbout').click(function(){
$(".slidingDivAbout").slideToggle(350);
$(".slidingDivAboutArr").fadeToggle("fast", "linear");
});
I think it's better to do this with CSS classes rather than a load of hide/show methods. 我认为使用CSS类而不是加载hide / show方法更好。 The HTML would look something like:
HTML看起来像:
<a class="showAbout" href="#" >About</a>
<a class="showContact" href="#" >Contact</a>
<div id='wrapper'>
<div class="about">
<div class="slidingDivAbout">Some Content 1</div>
<div class="slidingDivAboutArr"><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
</div>
<div class="contact">
<div class="slidingDivContact">Some Content 2</div>
<div class="slidingDivContactArr"><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
</div>
</div>
With two CSS rules: 有两个CSS规则:
#wrapper > div {
display: none;
}
#wrapper > div.show {
display: block;
}
And then the jQuery: 然后是jQuery:
$(function() {
$(document).on('click', '.showAbout', function() {
$('.about').siblings('div').removeClass('show').end().toggleClass('show');
});
$(document).on('click', '.showContact', function() {
$('.contact').siblings('div').removeClass('show').end().toggleClass('show');
});
});
I would use a custom attribute in your html tag: 我会在你的html标签中使用自定义属性:
Here's a working demo 这是一个有效的演示
HTML: HTML:
<a class="slidingLink" slide="about" href="#" >About</a>
<a class="slidingLink" slide="contact" href="#" >Contact</a>
<div class="slidingContainer" id="about">
<div>Some Content</div>
<div><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
</div>
<div class="slidingContainer" id="contact">
<div>Some Content</div>
<div><img src="img/dropdownarrow.png" width="24" height="12" alt=""></div>
</div>
JavaScript: JavaScript的:
$(document).ready(function(){
$('.slidingContainer').hide();
$('.slidingLink').click(function () {
var toSlide = $(this).attr("slide");
$("#"+toSlide).slideDown(350);
$('.slidingContainer').each(function () {
if ($(this).attr("id") != toSlide) {
$(this).slideUp(350);
}
});
});
});
This is absolutely generic, so all you have to do to add new links is add a new a
tag with the slide
attribute set to the id
of a div with the class slidingContainer
. 这是绝对通用的,所以你要做的就是添加新的链接是添加一个新
a
标签,其slide
属性设置为一个div的id
,类为slidingContainer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.