【发布时间】:2018-08-23 22:01:26
【问题描述】:
我已经阅读了论坛和教程,但我是 jQuery 和 JS 的初学者。我想在我的页面上有多个切换器,在我的 div 之外显示/隐藏内容,但我无法实现它。我得到的最接近的方法是拥有它们,但它们都同时打开和关闭。
我正在尝试实现这样的效果,即如果您单击“更多内容指示器”,它就会消失并显示隐藏的内容,并且在隐藏且现在可见的内容的末尾将是另一个隐藏按钮。
请看代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<style>
.wrapper {max-width: 1080px; margin: auto;}
.hidden0 {display: none;}
.showed0 {display: block;}
#switcher0 {background: #fff; font-size: 50px; border-style: none; margin: auto; cursor: pointer;}
.hidden1 {display: none;}
.showed1 {display: block;}
#switcher1 {background: #fff; font-size: 50px; border-style: none; margin: auto; cursor: pointer;}
#hide0 {margin: 20px auto; display: block; cursor: pointer; background: #fff; font-size: 50px; border-style: none;}
#hide1 {margin: 20px auto; display: block; cursor: pointer; background: #fff; font-size: 50px; border-style: none;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#toggler0").toggle(800);
$('#switcher0').toggleClass('showed0 hidden0');
});
});
$("#hide0").click(function(){
$("#toggler0").hide(800);
});
$(document).ready(function(){
$("button").click(function(){
$("#toggler1").toggle(800);
$('#switcher1').toggleClass('showed1 hidden1');
});
});
$("#hide1").click(function(){
$("#toggler1").hide(800);
});
</script>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<img class="img-fluid" src="http://placehold.it/900x600" alt="Placeholder">
</div>
<div class="col-md-4 align-self-center">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ultricies euismod erat ut lacinia. Phasellus ipsum orci, tristique eu bibendum et, sodales eu metus.</p>
<button id="switcher0" class="showed0">+<p style="font-size: 16px;">Číst více</p></button>
</div>
</div>
</div>
<div id="toggler0" style="display: none;">
<div class="container-fluid" style="margin-top: 20px;">
<div class="row" style="padding-left: 15px; padding-right: 15px;">
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
</div>
</div>
<button id="hide0">↑ <p style="font-size: 16px;">Skrýt</p></button>
</div>
<div class="container-fluid" style="margin-top: 30px;">
<div class="row">
<div class="col-md-4 align-self-center">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ultricies euismod erat ut lacinia. Phasellus ipsum orci, tristique eu bibendum et, sodales eu metus.</p>
<button id="switcher1" class="showed1">+<p style="font-size: 16px;">Číst více</p></button>
</div>
<div class="col-md-8">
<img class="img-fluid" src="http://placehold.it/900x600" alt="Placeholder">
</div>
</div>
</div>
<div id="toggler1" style="display: none;">
<div class="container-fluid" style="margin-top: 20px;">
<div class="row" style="padding-left: 15px; padding-right: 15px;">
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img style="margin-bottom: 30px;" class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
<div class="col-md-4">
<img class="img-fluid" src="http://placehold.it/600x600" alt="Placeholder">
</div>
</div>
</div>
<button id="hide1">↑ <p style="font-size: 16px;">Skrýt</p></button>
</div>
</div><!-- Wrapper -->
</body>
</html>
提前感谢大家的帮助。
【问题讨论】:
-
不工作,为什么?小提琴:jsfiddle.net/npd2qsfm/1 - 我根据建议编辑了脚本: $(document).ready(function(){ $("#hide0").click(function(){ $("#toggler0").hide(800 ); }); $("button").click(function(){ $("#toggler0").toggle(800); $('#switcher0').toggleClass('showed0 hidden0'); }); }); $(document).ready(function(){ $("button").click(function(){ $("#hide1").click(function(){ $("#toggler1").hide(800) ; }); $("#toggler1").toggle(800); $('#switcher1').toggleClass('showed1 hidden1'); }); });
-
请看我的回答。
标签: jquery