function toggler(divId) {
$("#" + divId).toggle();
}

<div class="row">
<div class="col">
<a href="#" onclick="toggler('slide1');">slide 1</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide2');">slide 2</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide3');">slide 3</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide4');">slide 4</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide5');">slide 5</a>
</div>
</div>
<div class="container">
<div id="slide1" class='hidden'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div
<div id="slide2" class='hidden'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide3" class='hidden'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide4" class='hidden'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide5" class='hidden'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
您可以将JS更改为如下所示。基本上,代码会将hidden
类添加到ID为divs
的所有slide*
,并排除点击的当前slide
,然后切换类hidden
点击幻灯片。
function toggler(divId) {
$("[id*='slide']").not($("#" + divId)).addClass("hidden");
$("#" + divId).toggleClass("hidden");
}
function toggler(divId) {
$("[id*='slide']").not($("#" + divId)).addClass("hidden");
$("#" + divId).toggleClass("hidden");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col">
<a href="#" onclick="toggler('slide1');">slide 1</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide2');">slide 2</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide3');">slide 3</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide4');">slide 4</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide5');">slide 5</a>
</div>
</div>
</div>
<div class="container">
<div id="slide1" class='hidden'>
<div class="row">
<div class="col"> 111
<img src="img.jpg">
</div>
<div class="col">111
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide2" class='hidden'>
<div class="row">
<div class="col"> 222
<img src="img.jpg">
</div>
<div class="col"> 222
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide3" class='hidden'>
<div class="row">
<div class="col"> 333
<img src="img.jpg">
</div>
<div class="col"> 333
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide4" class='hidden'>
<div class="row">
<div class="col"> 444
<img src="img.jpg">
</div>
<div class="col"> 444
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide5" class='hidden'>
<div class="row">
<div class="col"> 555
<img src="img.jpg">
</div>
<div class="col"> 555
<img src="img.jpg">
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
问题是你没有隐藏jquery中的所有其他div
1)为每个需要切换的div添加一个类
2)在js中,用该类隐藏所有div
新的HTML将是:
<div class="row">
<div class="col">
<a href="#" onclick="toggler('slide1');">slide 1</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide2');">slide 2</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide3');">slide 3</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide4');">slide 4</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide5');">slide 5</a>
</div>
</div>
<div class="container">
<div id="slide1" class='hidden slide'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div
<div id="slide2" class='hidden slide'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide3" class='hidden slide'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide4" class='hidden slide'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide5" class='hidden slide'>
<div class="row">
<div class="col">
<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
</div>
和javascript将是
function toggler(divId) {
// hide all other divs first
$('.slide').hide();
$("#" + divId).toggle();
}
答案 2 :(得分:0)
补充第一个答案,html中有未公开的divs
,阻止js运行。下面的代码段有效。
function toggler(divId) {
$(".slide").hide();
$("#" + divId).show();
}
img {
width: 20px;
height: 20px;
border: 1px solid black;
}
.slide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col">
<a href="#" onclick="toggler('slide1');">slide 1</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide2');">slide 2</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide3');">slide 3</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide4');">slide 4</a>
</div>
<div class="col">
<a href="#" onclick="toggler('slide5');">slide 5</a>
</div>
</div>
<div class="container">
<div id="slide1" class='slide'>
<div class="row">
<div class="col">
1<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide2" class='slide'>
<div class="row">
<div class="col">
2<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide3" class='slide'>
<div class="row">
<div class="col">
3<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide4" class='slide'>
<div class="row">
<div class="col">
4<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
<div id="slide5" class='slide'>
<div class="row">
<div class="col">
5<img src="img.jpg">
</div>
<div class="col">
<img src="img.jpg">
</div>
</div>
</div>
</div>
答案 3 :(得分:0)
答案 4 :(得分:0)
"use strict";
let menu = document.querySelector('.row');
let menuItems = document.querySelector('.container');
let sliders = menuItems.querySelectorAll('div.hidden');
menu.addEventListener('click', showMenu);
function showMenu(event) {
event.preventDefault();
let target = event.target;
if (target.tagName !== 'A') {
return;
}
let slide = target.getAttribute('data-slide');
let slideId = '#' + slide;
let currentSlider = menuItems.querySelector(slideId);
let currentClass = currentSlider.className;
for (let i = 0, max = sliders.length; i < max; i++) {
sliders[i].className = 'hidden';
}
if (!currentClass) {
currentSlider.className = 'hidden';
return;
}
currentSlider.className = '';
}
&#13;
.hidden {
display: none;
}
&#13;
<div class="row">
<div class="col">
<a href="#" data-slide="slide1">slide 1</a>
</div>
<div class="col">
<a href="#" data-slide="slide2">slide 2</a>
</div>
<div class="col">
<a href="#" data-slide="slide3">slide 3</a>
</div>
<div class="col">
<a href="#" data-slide="slide4">slide 4</a>
</div>
<div class="col">
<a href="#" data-slide="slide5">slide 5</a>
</div>
</div>
<div class="container">
<div id="slide1" class='hidden'>
<div class="row">
<div class="col">
111
</div>
<div class="col">
111
</div>
</div>
</div>
<div id="slide2" class='hidden'>
<div class="row">
<div class="col">
222
</div>
<div class="col">
222
</div>
</div>
</div>
<div id="slide3" class='hidden'>
<div class="row">
<div class="col">
333
</div>
<div class="col">
333
</div>
</div>
</div>
<div id="slide4" class='hidden'>
<div class="row">
<div class="col">
444
</div>
<div class="col">
444
</div>
</div>
</div>
<div id="slide5" class='hidden'>
<div class="row">
<div class="col">
555
</div>
<div class="col">
555
</div>
</div>
</div>
</div>
&#13;
纯JS中的另一个例子
答案 5 :(得分:0)
您需要先隐藏所有内容,然后显示所需的内容。我使用了通用的jQuery,但你可以使用bootstrap的崩溃函数。
$('.slide-toggle').click(function() {
var $this = $(this)
$('.slide').hide().filter(function() { return $(this).data('slide') === $this.data('toggle') }).show()
})
&#13;
.slide {
padding: 10px;
display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="slide-toggle" data-toggle="1">Show Slide 1</button>
<button class="slide-toggle" data-toggle="2">Show Slide 2</button>
<button class="slide-toggle" data-toggle="3">Show Slide 3</button>
<div class="slide" data-slide="1">Slide 1</div>
<div class="slide" data-slide="2">Slide 2</div>
<div class="slide" data-slide="3">Slide 3</div>
&#13;