我在boostrap中有一个自定义的内置部分,其中有6张卡片,每个card
下方都有自己的全角折叠,
我希望每次点击卡片时只显示一个折叠项;
这是结构的外观,X表示,如果您访问了崩溃1,崩溃2和崩溃3,则只需要显示第三个元素下拉列表即可:
<div class="container hardware-section">
<div class="row">
<div class="single-hardware">
<div class="col-md-2" >
<a href="#first-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#sec-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#thr-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#for-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#fiv-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#six-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
</div> <!-- Product display row end -->
<div class="row hardware-info-container">
<div id="first-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..1..</p>
</div>
<div id="sec-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..2..</p>
</div>
<div id="thr-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..3..</p>
</div>
<div id="for-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..4..</p>
</div>
<div id="fiv-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..5..</p>
</div>
<div id="six-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..6..</p>
</div>
</div>
</div>
如果您也可以通过jsfiddle示例帮助我,那就太好了。
最诚挚的问候
答案 0 :(得分:1)
未添加额外的CSS或HTML,仅添加了一些jquery代码。希望这会有所帮助。 还添加了“在”第一个描述容器中的类,以默认显示第一个容器
$(document).ready(function(){
$(document).on('click','.single-hardware a',function(){
ref = $(this).attr('href');
$('.collapse').removeClass('in');
$('.hardware-info-container').find(ref).addClass('in');
})
});
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container hardware-section">
<div class="row">
<div class="single-hardware">
<div class="col-md-2" >
<a href="#first-hardware" data-toggle="collapse " id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#sec-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#thr-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#for-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#fiv-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#six-hardware" data-toggle="collapse" id="hardwareSection">
<img src="img/sample-hardware.png" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
</div> <!-- Product display row end -->
<div class="row hardware-info-container">
<div id="first-hardware" class="collapse in hardware-info">
<p>Lorem ipsum dolor text..1..</p>
</div>
<div id="sec-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..2..</p>
</div>
<div id="thr-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..3..</p>
</div>
<div id="for-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..4..</p>
</div>
<div id="fiv-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..5..</p>
</div>
<div id="six-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..6..</p>
</div>
</div>
</div>
答案 1 :(得分:1)
这很简单
首先,使用多个具有相同名称的id会产生一个错误,因此将它们还原为类似id=hardwareSection
的类,例如class=hardwareSection
因此,每次点击hardwareSection
链接时,都应使用jQuery从所有in
div中删除class='hardware-info'
类,除具有相同hardwareSection
div点击元素索引的元素外,如下所示
$(".hardwareSection").on("click",function(e,i){
var index = $('.hardwareSection').index(this);
$(".hardware-info").not($(".hardware-info").eq(index)).removeClass("in");
})
请参见下面的工作片段:
$(function(){
$(".hardwareSection").on("click",function(e,i){
var index = $('.hardwareSection').index(this);
$(".hardware-info").not($(".hardware-info").eq(index)).removeClass("in");
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container hardware-section">
<div class="row">
<div class="single-hardware">
<div class="col-md-2" >
<a href="#first-hardware" data-toggle="collapse" class="hardwareSection">
<img src="https://i.ebayimg.com/images/g/kDwAAOSwYK1bE8Oq/s-l225.jpg" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#sec-hardware" data-toggle="collapse" class="hardwareSection">
<img src="https://i.ebayimg.com/images/g/kDwAAOSwYK1bE8Oq/s-l225.jpg" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#thr-hardware" data-toggle="collapse" class="hardwareSection">
<img src="https://i.ebayimg.com/images/g/kDwAAOSwYK1bE8Oq/s-l225.jpg" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#for-hardware" data-toggle="collapse" class="hardwareSection">
<img src="https://i.ebayimg.com/images/g/kDwAAOSwYK1bE8Oq/s-l225.jpg" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#fiv-hardware" data-toggle="collapse" class="hardwareSection">
<img src="https://i.ebayimg.com/images/g/kDwAAOSwYK1bE8Oq/s-l225.jpg" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
<div class="single-hardware">
<div class="col-md-2">
<a href="#six-hardware" data-toggle="collapse" class="hardwareSection">
<img src="https://i.ebayimg.com/images/g/kDwAAOSwYK1bE8Oq/s-l225.jpg" alt="Product Image">
<h4 class="single-hardware-title">Procut title</h4>
</a>
</div>
</div>
</div> <!-- Product display row end -->
<div class="row hardware-info-container">
<div id="first-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..1..</p>
</div>
<div id="sec-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..2..</p>
</div>
<div id="thr-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..3..</p>
</div>
<div id="for-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..4..</p>
</div>
<div id="fiv-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..5..</p>
</div>
<div id="six-hardware" class="collapse hardware-info">
<p>Lorem ipsum dolor text..6..</p>
</div>
</div>
</div>