显示div如果项目包含与链接文本相同的文本
我有一个博客,该博客从多个类别中输出帖子,并希望在留在页面上时根据类别过滤它们。例如,如果我单击标签2,我希望所有帖子都显示出标签2。 jQuery看看是否有匹配并返回结果。
- 默认情况下,我想
- 在单击“过滤器”(来自TR-NAV)之后显示所有帖子,我想仅显示该类别(来自TR类别)的帖子。
- 如果tritle和tr类是相同的,那么只有那些帖子才能显示
任何人知道处理此问题的简单方法?
我找到了以下基于类别自动选择的片段,但是我希望在单击时发生这种情况。我已经尝试弄乱了这一点,但无法实现任何工作。
任何帮助将不胜感激!
$(function() {
for (var i = 0; i < $('.tr-wrap .tr-category').length; i++) {
var name = $('.tr-wrap .tr-category').eq( i );
for (var d = 0; d < $('.tr-title').length; d++) {
var title = $('.tr-title').eq( d );
if ( title.text() == name.text() ) {
var parent = name.closest('.tr-item');
var titleContain = title.closest('.tr-contain').find('.tr-list');
parent.clone(true, true).appendTo( titleContain );
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="tr-nav">
<a href="#"><div class="tr-title">All</div></a>
<a href="#"><div class="tr-title">Tag 1</div></a>
<a href="#"><div class="tr-title">Tag 2</div></a>
<a href="#"><div class="tr-title">Tag 3</div></a>
</div>
<div class="tr-wrap">
<div class="tr-list">
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 3</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 2</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 3</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 1</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 2</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 3</div>
</div>
</div>
</div>
I have a blog that outputs posts from multiple categories and want to be able to filter them based on category while staying on the page. For instance, if I click Tag 2 I want all posts to show up with Tag 2. Since there isn't an easy way to do this in the CMS I figured I would add another field to the post that displays the category, and use jQuery to see if there's a match and return the results.
- By default I'd like to display all posts
- After clicking a "filter" (from tr-nav) I'd like to only show posts from that category (from tr-category).
- If tr-title and tr-category are the same then only those posts should show
Does anyone know of an easy way to handle this?
I've found the following snippet which auto sorts based on category, but I want this to happen on click. I've tried messing with this but can't get anything to work.
Any help would be greatly appreciated!
$(function() {
for (var i = 0; i < $('.tr-wrap .tr-category').length; i++) {
var name = $('.tr-wrap .tr-category').eq( i );
for (var d = 0; d < $('.tr-title').length; d++) {
var title = $('.tr-title').eq( d );
if ( title.text() == name.text() ) {
var parent = name.closest('.tr-item');
var titleContain = title.closest('.tr-contain').find('.tr-list');
parent.clone(true, true).appendTo( titleContain );
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="tr-nav">
<a href="#"><div class="tr-title">All</div></a>
<a href="#"><div class="tr-title">Tag 1</div></a>
<a href="#"><div class="tr-title">Tag 2</div></a>
<a href="#"><div class="tr-title">Tag 3</div></a>
</div>
<div class="tr-wrap">
<div class="tr-list">
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 3</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 2</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 3</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 1</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 2</div>
</div>
<div class="tr-item">
<h5>Lorem ipsum dolor sit amet, consectetur</h5>
<div class="tr-category">Tag 3</div>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,您可以在不添加任何类的情况下使用此JavaScript代码,但是您应该确保
tr-category
elements中的文本与tr-title
elements中的文本相同。Also, you can use this JavaScript code without adding any classes, but you should make sure the text in
tr-category
elements is the same as the text intr-title
elements.您可以通过在每个帖子中添加
标签名称
作为类来过滤帖子。我希望这对你有帮助You can filter your posts by adding the
tag name
as a class to each post. I hope this will help you