3

如何更改此功能以使用该jQuery.contains()方法而不是find()

我正在使用该函数按各种属性(即颜色、产品类别、场合等)过滤服装,而该函数目前仅返回完全匹配。由于项目通常具有多个属性值(即不止一种颜色),因此我想使用contains()而不是find(),但我无法弄清楚contains()与 data-attributes一起使用的正确语法

我在这里发布了一个简单的函数示例:http: //jsfiddle.net/chayacooper/WZpMh/5/

    $('#filterStyleOptions li a').click(function () {
        var attrStyle = $(this).data('style');
        $('#filterStyleOptions li').removeClass('active');
        $(this).parent().addClass('active');
        if (attrStyle == 'All') {
            $('#content').find('li').show();
        } else {
            // $('#content').find('li').hide();
            $('#content').find('li:not([data-style="' + attrStyle + '"])').hide();
            $('#content').find('li[data-style="' + attrStyle + '"]').show();
        }
        return false;
    });
4

1 回答 1

12

jQuery.contains您对包含选择器语法感到困惑:

$('#content').find('li[data-style*="' + attrStyle + '"]').show();

*=不是=意味着包含。您也可以使用~=仅查找整个单词,这更有可能是您想要的。所有这些的文档都在这里:http ://api.jquery.com/category/selectors/

于 2013-03-23T04:14:12.600 回答