在此函数中使用带有数据属性的jQuery.contains()方法而不是find()

Cha*_*per 3 jquery contains custom-data-attribute

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

我正在使用该功能通过各种属性(即颜色,产品类别,场合等)过滤服装,并且该功能目前仅返回完全匹配.由于项目通常具有多个属性值(即多于一种颜色),我想使用contains()而不是find(),但我无法找出contains()与数据属性一起使用的正确语法

我在这里发布了一个函数的简单例子: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;
    });
Run Code Online (Sandbox Code Playgroud)

Dav*_*ave 11

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

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

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