[英]Click to hide each div and show another div in jquery
I am new to jQuery. 我是jQuery新手。 Trying to hide the first div and show second div.
尝试隐藏第一个div并显示第二个div。 When I again click on 2nd div, it will show me the first div.
当我再次单击第2个div时,它将显示第一个div。
Here is my code. 这是我的代码。
$(".c1").on('click', function() { $(".one").fadeIn(); $(".two").fadeOut(); }); $(".c2").on('click', function() { $(".two").fadeIn(); $(".one").fadeOut(); });
.right { font-size: 20px; float: right; margin-right: 50px; } .ab-container { margin-bottom: 50px; } .container { padding: 30px 60px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="ab-container one"> <div class="ab-head"> <h1>This is div 1 <a href="" class="right c1"> Click to see div 2</a></h1> </div> <div class="ab-content"> <p>In publishing and graphic design, lorem ipsum is a filler text or greeking commonly used to demonstrate </p> </div> </div> <div class="ab-container two"> <div class="ab-head "> <h1>This is div 2 <a href="" class="right c2"> Click to see div 1</a></h1> </div> <div class="ab-content"> <p>Replacing meaningful content with placeholder text allows designers to design the form of the content before the content itself has been produced.</p> </div> </div> </div>
fadeOut
and fadeIn
calls inverted. fadeOut
和fadeIn
调用反转。 prevent
the a
link behavior. prevent
的a
链接行为。 fadeIn
call. fadeIn
呼叫作为回调传递。 I've added a hide
class to show how the DIVs appear and disappear. 我添加了一个
hide
类,以显示DIV如何出现和消失。
$(".c1").on('click', function(e) { e.preventDefault(); $(".one").fadeOut(function() { $(".two").fadeIn(); }); }); $(".c2").on('click', function(e) { e.preventDefault(); $(".two").fadeOut(function() { $(".one").fadeIn(); }); });
.right { font-size: 20px; float: right; margin-right: 50px; } .ab-container { margin-bottom: 50px; } .container { padding: 30px 60px; } .hide { display: none }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="ab-container one"> <div class="ab-head"> <h1>This is div 1 <a href="" class="right c1"> Click to see div 2</a></h1> </div> <div class="ab-content"> <p>In publishing and graphic design, lorem ipsum is a filler text or greeking commonly used to demonstrate </p> </div> </div> <div class="ab-container two hide"> <div class="ab-head "> <h1>This is div 2 <a href="" class="right c2"> Click to see div 1</a></h1> </div> <div class="ab-content"> <p>Replacing meaningful content with placeholder text allows designers to design the form of the content before the content itself has been produced.</p> </div> </div> </div>
See? 看到? now is working your logic!
现在正在按您的逻辑工作!
First, it will work better if you use spans instead of links 首先,如果使用跨距而不是链接,它将更好地工作
The nyou have the order of fadein/fadeout confused: nyou对淡入/淡出的顺序感到困惑:
$(".c1").on('click', function() { $(".two").fadeIn(); $(".one").fadeOut(); }); $(".c2").on('click', function() { $(".one").fadeIn(); $(".two").fadeOut(); });
.right { font-size: 20px; float: right; margin-right: 50px; } .ab-container { margin-bottom: 50px; } .container { padding: 30px 60px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="ab-container one"> <div class="ab-head"> <h1>This is div 1 <span class="right c1"> Click to see div 2</span></h1> </div> <div class="ab-content"> <p>In publishing and graphic design, lorem ipsum is a filler text or greeking commonly used to demonstrate </p> </div> </div> <div class="ab-container two"> <div class="ab-head "> <h1>This is div 2 <span class="right c2"> Click to see div 1</span></h1> </div> <div class="ab-content"> <p>Replacing meaningful content with placeholder text allows designers to design the form of the content before the content itself has been produced.</p> </div> </div> </div>
Your code is working you only need to remove the href=""
from your <a>
tag. 您的代码可以正常工作,您只需要从
<a>
标记中删除href=""
。 However, here is my way of doing it if you like to take a look. 但是,如果您想看看,这是我的处理方法。 And the edited version of the HTML
以及HTML的编辑版本
$('.c1').click(function () { $('.one').fadeIn(); $('.two').fadeOut(); }) $('.c2').click(function () { $('.two').fadeIn(); $('.one').fadeOut(); })
<div class="container"> <div class="ab-container one"> <div class="ab-head"> <h1>This is div 1 <a class="right c1"> Click to see div 2</a></h1> </div> <div class="ab-content"> <p>In publishing and graphic design, lorem ipsum is a filler text or greeking commonly used to demonstrate </p> </div> </div> <div class="ab-container two"> <div class="ab-head "> <h1>This is div 2 <a class="right c2"> Click to see div 1</a></h1> </div> <div class="ab-content"> <p>Replacing meaningful content with placeholder text allows designers to design the form of the content before the content itself has been produced.</p> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.