[英]Hide/show elements with same class on click
I'm trying to have some text toggle on click of elements with same class.我正在尝试在单击具有相同类的元素时进行一些文本切换。 I have the text showing on click but when I click another element the text remains.
我在单击时显示文本,但是当我单击另一个元素时,文本仍然存在。 How do I have it removed from others when another element of same class is clicked?
单击同一类的另一个元素时,如何将其从其他元素中删除?
$('.video-carousel--thumbnail').on('click', function() { $(this).find('.now-playing').not(this).css('display', 'none'); $(this).find('.now-playing').css('display', 'block'); });
.now-playing { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="video-carousel--thumbnail"> <div class="l-thumbnail__title"> <p><strong>Thumbnail One</strong></p> <p class="now-playing"><strong>Now Playing</strong></p> </div> </div> <div class="video-carousel--thumbnail"> <div class="l-thumbnail__title"> <p><strong>Thumbnail Two</strong></p> <p class="now-playing"><strong>Now Playing</strong></p> </div> </div> <div class="video-carousel--thumbnail"> <div class="l-thumbnail__title"> <p><strong>Thumbnail Three</strong></p> <p class="now-playing"><strong>Now Playing</strong></p> </div> </div>
You don't need the DOM traversal logic when hiding all other elements.隐藏所有其他元素时不需要 DOM 遍历逻辑。 Just select them directly using
$('.now-playing')
.只需使用
$('.now-playing')
直接选择它们。 Also note that you can use hide()
and show()
instead of css()
.另请注意,您可以使用
hide()
和show()
而不是css()
。
$('.video-carousel--thumbnail').on('click', function() {
$('.now-playing').hide()
$(this).find('.now-playing').show();
});
Also note that if you want to toggle the elements then your not()
selector is incorrect as this
refers to the .video-carousel--thumbnail
element which raised the event, not the .now-playing
element to exclude from the collection.另请注意,如果您想切换元素,那么您的
not()
选择器不正确,因为this
指的是引发事件的.video-carousel--thumbnail
元素,而不是要从集合中排除的.now-playing
元素。
For that to work you need to find the relevant element first, then exclude it from those you remove the class from before calling toggle()
.为此,您需要先找到相关元素,然后在调用
toggle()
之前将其从删除类的元素中排除。 Try this:尝试这个:
$('.video-carousel--thumbnail').on('click', function() { var $target = $(this).find('.now-playing'); $('.now-playing').not($target).hide() $target.toggle(); });
.now-playing { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="video-carousel--thumbnail"> <div class="l-thumbnail__title"> <p><strong>Thumbnail One</strong></p> <p class="now-playing"><strong>Now Playing</strong></p> </div> </div> <div class="video-carousel--thumbnail"> <div class="l-thumbnail__title"> <p><strong>Thumbnail Two</strong></p> <p class="now-playing"><strong>Now Playing</strong></p> </div> </div> <div class="video-carousel--thumbnail"> <div class="l-thumbnail__title"> <p><strong>Thumbnail Three</strong></p> <p class="now-playing"><strong>Now Playing</strong></p> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.