jquery - jQuery 上的选项卡内容
问题描述
我是初学者前端开发。我来自乌克兰,我的英语是如此……但我一次又一次地学习英语。
那我继续...
当我单击一个新块时,如何使前一个块关闭? 在此处输入图像描述
我有这个代码
<pre>
<div class="gallery__column">
<div class="gallery__item">
<div class="overlay gallery__overlay"></div>
<div class="gallery__image">
<a class="content-open" href="##">
<img class="gallery__img" src="img/example/01.jpg" alt="">
<span data-tab="tab-4" class="gallery__arrow arrow"></span>
</a>
</div>
<div class="gallery__content" id="tab-4">
<h4 class="gallery__title">Электрические шторы</h4>
<div class="gallery__brand">SOMFY 1</div>
<div class="gallery__text"></div>
</div>
</div>
</div>
</pre>
<div class="gallery__item">
<div class="overlay gallery__overlay"></div>
<div class="gallery__image">
<img class="gallery__img" src="img/example/02.jpg" alt="">
<a class="content-link" href="##"></a>
<span data-tab="tab-1" class="gallery__arrow arrow"></span>
</div>
<div class="gallery__content" id="tab-1">
<h4 class="gallery__title">Электрические шторы</h4>
<div class="gallery__brand">SOMFY 1</div>
<div class="gallery__text">
</div>
</div>
$('.arrow').on('click', function(){
$('.arrow').removeClass('active');
$('.arrow').parents().siblings('.gallery__content');
$(this).addClass('active');
$(this).parents().siblings('.gallery__content').slideToggle();
});
解决方案
您几乎拥有它,但我建议您在您的情况下使用.closest()
组合与.find()
,如下例所示。
$('.arrow').on('click', function() {
$('.arrow').removeClass('active');
$('.arrow').not(this).closest('.gallery__item').find('.gallery__content');
$(this).addClass('active');
$(this).closest('.gallery__item').find('.gallery__content').slideToggle();
});
演示
$('.arrow').on('click', function() {
$('.arrow').removeClass('active');
$('.arrow').not(this).closest('.gallery__item').find('.gallery__content').hide();
$(this).addClass('active');
$(this).closest('.gallery__item').find('.gallery__content').slideToggle();
});
.gallery__content {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre>
<div class="gallery__column">
<div class="gallery__item">
<div class="overlay gallery__overlay"></div>
<div class="gallery__image">
<a class="content-open" href="##">
<img class="gallery__img" src="img/example/04.jpg" alt="">
<span data-tab="tab-4" class="gallery__arrow arrow">arrow</span>
</a>
</div>
<div class="gallery__content" id="tab-4">
<h4 class="gallery__title">Электрические шторы</h4>
<div class="gallery__brand">SOMFY 1</div>
<div class="gallery__text"></div>
</div>
</div>
</div>
</pre>
<pre>
<div class="gallery__column">
<div class="gallery__item">
<div class="overlay gallery__overlay"></div>
<div class="gallery__image">
<a class="content-open" href="##">
<img class="gallery__img" src="img/example/04.jpg" alt="">
<span data-tab="tab-4" class="gallery__arrow arrow">arrow</span>
</a>
</div>
<div class="gallery__content" id="tab-4">
<h4 class="gallery__title">Электрические шторы</h4>
<div class="gallery__brand">SOMFY 1</div>
<div class="gallery__text"></div>
</div>
</div>
</div>
</pre>
推荐阅读
- javafx - JavaFX - 按钮大小百分比
- javascript - arguments 对象的长度为零,即使我可以访问它的属性
- angular - 如何在 Ng2 图表中显示标签而不必将鼠标悬停在它们上面?
- r - 如何按日期分组以显示零和非零条目的计数?
- python - Pyspark:使用私钥连接到雪花时出错
- node.js - 无法启动“npm”:确保已安装“npm”并且可以找到
- groovy - 如何使用匿名类覆盖 Groovy 变量和方法?
- python - 如何创建一个类来定义包含标题的 CSV 文件布局?
- python - 关于 tf.data.Dataset、tf.shape() 和 ? 在高度和宽度,但我想使用 h 和 w 从图像中随机裁剪,我该怎么做
- javascript - Angular 7 build:如何删除输出哈希和默认值~
~ 文件?