Filter divs by data-attr containing string

前端 未结 4 1220
醉酒成梦
醉酒成梦 2021-01-18 17:21

How to filter a number of divs based on what they have in their custom data attribute using jQuery? The problem is that the attribute can have more than 1 value

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 17:23

    Try

    function filter(e) {
        var regex = new RegExp('\\b' + e + '\\b');
        $('.size').hide()
            .filter(function () {
            return regex.test($(this).data('size'))
        }).show();
    }
    

    Demo: Fiddle

提交回复
热议问题