【问题标题】:JQuery category filter breaks when a category is more than 2 words当类别超过 2 个单词时,JQuery 类别过滤器会中断
【发布时间】:2019-01-21 03:50:45
【问题描述】:

我正在使用 Jquery 来显示项目类别并过滤选择类别的项目。

在此处查看代码笔:https://codepen.io/saintasia/pen/dzqZov

HTML:

<body>
    <div class="container">
        <nav>
            <ul>
                <li class="current"><a href="#">All projects</a></li>
                <li><a href="#">Front-end</a></li>
                <li><a href="#">Back-end</a></li>
                <li><a href="#">Apps</a></li>
                <li><a href="#">Design</a></li>
                <li><a href="#">Testing Testing Testing</a></li>
            </ul>
        </nav>
        <div id="projects">
            <h1 class="heading">All Projects</h1>
            <ul id="gallery">
                <li class="design"><a href="#"><img src="https://source.unsplash.com/jjtdL443L4w/700x700"></a></li>
                <li class="apps"><a href="#"><img src="https://source.unsplash.com/y1yQQmozTBw/700x700"></a></li>
                <li class="back-end"><a href="#"><img src="https://source.unsplash.com/b18TRXc8UPQ/700x700"></a></li>
                <li class="apps design"><a href="#"><img src="https://source.unsplash.com/klRB1BB9pV8/700x700"></a></li>
                <li class="front-end testing-testing-testing back-end"><a href="#"><img src="https://source.unsplash.com/y1yQQmozTBw/700x700"></a></li>
                <li class="front-end design"><a href="#"><img src="https://source.unsplash.com/1vwwZ-BmmrE/700x700"></a></li>
                <li class="apps"><a href="#"><img src="https://source.unsplash.com/WLOCr03nGr0/700x700"></a></li>
                <li class="back-end"><a href="#"><img src="https://source.unsplash.com/iOykDIkZLQw/700x700"></a></li>
            </ul>
            </nav>
        </div>
        <!-- modal gallery -->
        <div class="gallery">
            <a class="close" href="#">
                <i>
                <span class="bar"></span>
                <span class="bar"></span>
                </i>
            </a>
            <img src="">
        </div>
    </div>
</body>

CSS:

    * {
    margin: 0;
    padding: 0;
}

body {
    color: #333;
    font-size: 13px;
    background: #f4f4f4;
    font-family: sans-serif;
    overflow:auto;
}

.container {
    width: 90%;
    max-width: 960px;
    margin: 30px auto;
    position: relative;
    text-align: center;
}

h1 {
    margin-bottom: 20px;
    text-transform: uppercase;
    color: #F39CC3;
}

nav {
    display: block;
    width: 100%;
}

ul {
   list-style: none; 
   margin-bottom: 20px;
}

nav > ul > li {
    display: inline-block;
}
nav > ul > li > a {
    text-transform: uppercase;
    padding: 4px 10px;
    margin-right: 2px;
    margin-left: 2px;
    text-decoration: none;
    color: #27A4DD;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 25px;
    border: 1px solid #27A4DD;
    -webkit-transition: all 300ms ease-in-out;
    -moz-transition: all 300ms ease-in-out;
    transition: all 300ms ease-in-out;
}

.hidden {
    display: none;
}

nav > ul > li > a:hover, .current a {
    color: #fff;
    background-color: #27A4DD;
}

#projects > ul > li {
    display: inline-block;
    float: left;
    margin-right: 10px;
    margin-bottom: 5px;
    width: 23%;
    height: auto;
    cursor: pointer;
    border-radius: 5px;
    /* Padding stays within the width */
    box-sizing: border-box;
    position: relative;
    opacity: 0.7;
    -webkit-transition: all 300ms ease-in-out;
    -moz-transition: all 300ms ease-in-out;
    transition: all 300ms ease-in-out;
}

#projects > ul > li:hover {
    opacity: 1;
}

img {
    max-width: 100%;
    border-radius: 5px;
}

.gallery {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.8);
    padding: 40px 10px;
    display: none;
    box-sizing: border-box;
}

.gallery > img {
    max-height: 100%;
    width: auto;
}

.close i {
    position: fixed;
    top: 10px;
    right: 10px;
    height: 30px;
    width: 30px;
}

.bar {
    display: block;
    position: absolute;
    top: 13px;
    float: left;
    width: 30px;
    border-bottom: 4px solid #fff;
    transform: rotate(45deg);
}

.bar:first-child {
    transform: rotate(-45deg);
}

@media (max-width: 768px){
    #projects > ul > li {
        width: 48%;
    }
}

@media (max-width: 568px) {
    #projects > ul > li {
        width: 100%;
    }
}

JS:

$(document).ready(function(){
// filter
$('nav a').on('click', function(event){
    event.preventDefault();
    // current class
    $('nav li.current').removeClass('current');
    $(this).parent().addClass('current');

    // set new heading
    $('h1.heading').text($(this).text());

    // filter link text
    var category = $(this).text().toLowerCase().replace(' ', '-');

    // remove hidden class if "all" is selected
    if(category == 'all-projects'){
        $('ul#gallery li:hidden').fadeIn('slow').removeClass('hidden');
    } else {
        $('ul#gallery li').each(function(){
           if(!$(this).hasClass(category)){
               $(this).hide().addClass('hidden');
           } else {
               $(this).fadeIn('slow').removeClass('hidden');
           }
        });
    }
    return false;        
});
// lightbox
$('ul#gallery a').on('click', function(event){
    event.preventDefault();
    var link = $(this).find('img').attr('src');
    $('.gallery img').attr('src', '');
    $('.gallery img').attr('src', link);
    $('.gallery').fadeIn('slow');
});
// close lightbox
$('.gallery').on('click', function(event){
    event.preventDefault();
    $('.gallery').fadeOut('slow');
});

});

我遇到的问题是,如果一个类别超过 2 个单词,它不会显示该类别的项目。您会在代码笔中看到有一个名为“Testing Testing Testing”的类别,其中一个项目分配了该类别。但是,当您单击测试测试测试类别时,不会显示任何项目。然而,所有其他类别都有效,因为它们都只有 2 个字长。

非常感谢任何帮助!

【问题讨论】:

  • codepen中没有Testing Testing的分类。

标签: javascript jquery debugging filter categories


【解决方案1】:

您的代码中断,因为 .replace(' ', '-') 代码只是替换了第一个空格

如果你想用'-'改变所有空格,你应该改变这个代码

var category = $(this).text().toLowerCase().replace(' ', '-');

到这里

var category = $(this).text().toLowerCase().split(' ').join('-');

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2011-03-24
    • 2013-08-28
    相关资源
    最近更新 更多