选择数据属性等于任何东西的元素



我使用jQuery来显示和隐藏元素,需要传递一个数据属性值,将'显示所有'即:[data-country*="anything"]或'country'的数据属性等于任何值。

我有两个过滤器列表(国家和类型),单击后将过滤器值添加到数组中。然后将两个数组连接到.show(),所有具有包含该值的data attr的项:

$("section.collaborator[data-country*='" + countryTog.join("'][data-country*='") + "'][data-collaborator_type*='" + collaborator_typeTog.join("'][data-collaborator_type*='") + "']").show('fast');

是否有一个值,我可以添加到我的数组,这将作为通配符如上所述?

例如:如果我在国家滤镜菜单上点击'Ireland',在类型滤镜菜单上点击'artist',我的选择器是这样的:

$(".collaborator[data-country*='ireland'][data-collaborator_type*='partner']").show();

或者如果我添加另一个国家,它看起来像:

$("section.collaborator[data-country*='ireland'][data-country*='united-kingdom'][data-collaborator_type*='partner']").show();

现在,如果我把鼠标向上一点,点击国家过滤器菜单上的'all'按钮,我现在得到:

$("section.collaborator[data-country*='*'][data-collaborator_type*='artist']").show();

我想要发生的是显示所有属性为data-country'[anything]'data-type='artist'的项目。

我知道可以查询data属性的存在,但在这种情况下,我需要传递一个值。

如果您想选择所有具有特定数据属性的元素,但您不关心其值是什么,只需使用不带等号的[data-attribute]。因此,如果您想要任何国家,但仅限于艺术家,请使用$('[data-country][data-type="artist"]')

最新更新