简体   繁体   English

隐藏div并显示另一个div

[英]Hide div and show another divs

I just want to show a div when someone click "add" button. 我只想在有人点击“添加”按钮时显示div。 If he clicks that "add" button again, needs to hide the current one and show another div. 如果他再次单击“添加”按钮,则需要隐藏当前的按钮并显示另一个div。 It also needs to show the approved div list on top area. 它还需要在顶部区域显示已批准的div列表。 If someone clicks the top area approved div, need to load the div again. 如果有人点击顶部区域批准的div,则需要再次加载div。

在此输入图像描述

I try to hide and show on click but no luck. 我试图隐藏并显示点击但没有运气。 (I'm bit new to jquery and I know this is pretty basic code.) (我对jquery有点新鲜,我知道这是非常基本的代码。)

Here is the fiddle Fiddle 这是小提琴小提琴

 $('.add-box').click(function() { $('.test-div-2').show(); }); $('.add-box').click(function() { $('.test-div-1').hide(); }); 
 .test-div-1, .test-div-2, .test-div-3 { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test-div-1">1</div> <div class="test-div-2">2</div> <div class="test-div-3">3</div> <a class="add-box">Add</a> 

Do below things:- 做以下事情: -

1.Add jQuery library before your script code. 1.在脚本代码之前添加jQuery库。

2.Wrap your code inside $(document).ready(function(){..}); 2.将代码包含在$(document).ready(function(){..}); (needed when script code is added above the HTML. if script code is added at the bottom of the page then wrapping is not needed). (当在HTML上方添加脚本代码时需要。如果在页面底部添加脚本代码,则不需要包装)。

3.Do show(),hide() in single click itself. 3.单击一下show(),hide()

 $(document).ready(function(){ $('.add-box').click(function() { $('.test-div-2').show(); $('.test-div-1').hide(); }); }); 
 .test-div-1, .test-div-2, .test-div-3{ display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test-div-1">1</div> <div class="test-div-2">2</div> <div class="test-div-3">3</div> <a class="add-box">Add</a> 

Fiddle example:- https://jsfiddle.net/rrj1818a/ 小提琴示例: - https://jsfiddle.net/rrj1818a/

Note:- 注意:-

If you want to show one-by-one then do like below:- 如果你想一个一个地展示,那么就像下面这样: -

https://jsfiddle.net/87re6avo/ https://jsfiddle.net/87re6avo/

<div class="test-div active">1</div>
<div class="test-div">2</div>
<div class="test-div">3</div>
<a class="add-box">Add</a>



$('.add-box').click(function(e) {
e.preventDefault();
var $current = $('.test-div.active');
var $next = $current.next();

$current.removeClass('active');



if(!$next.length) {
$next = $('.test-div').first();


}



$next.addClass('active');

});

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

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