Select 具有等于任何数据属性的元素
Select Elements that have Data Attribute Equal to Anything
我正在使用 jQuery 来显示和隐藏元素,并且需要传递一个数据属性值,该值将 'show all' 即:[data-country*="anything"]
或数据属性为 'country' 等于任何值。
我有两个过滤器列表(国家和类型),单击它们时会将过滤器值添加到数组中。然后将两个数组连接到 .show()
所有具有包含该值的数据属性的项目:
$("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'
属性的项目。
我知道可以查询数据属性是否存在,但在这种情况下我需要传递一个值。
如果您想 select 所有具有特定数据属性的元素,但您不关心值是什么,只需使用不带等号的 [data-attribute]
。所以如果你想要任何国家,但只有艺术家,请使用 $('[data-country][data-type="artist"]')
我正在使用 jQuery 来显示和隐藏元素,并且需要传递一个数据属性值,该值将 'show all' 即:[data-country*="anything"]
或数据属性为 'country' 等于任何值。
我有两个过滤器列表(国家和类型),单击它们时会将过滤器值添加到数组中。然后将两个数组连接到 .show()
所有具有包含该值的数据属性的项目:
$("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'
属性的项目。
我知道可以查询数据属性是否存在,但在这种情况下我需要传递一个值。
如果您想 select 所有具有特定数据属性的元素,但您不关心值是什么,只需使用不带等号的 [data-attribute]
。所以如果你想要任何国家,但只有艺术家,请使用 $('[data-country][data-type="artist"]')