将元素匹配到数据属性值的一部分? [英] Match element to part of a data attribute value?

查看:86
本文介绍了将元素匹配到数据属性值的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码针对的是在数据属性中没有匹配值的元素.如果单击按钮,则将显示此属性中没有匹配值的所有元素.

I have a piece of code which targets an element that does not have a matching value in a data attribute. If I click a button, all elements that don't have a matching value in this attribute will show.

例如:

<div data-the-vals="Female_Young.Dev,Male_Mid.Project"></div>

然后在我的jquery中,我试图访问'data-the-vals'以查看是否存在以下任何值,例如:

then in my jquery I'm trying to access 'data-the-vals' to see if ANY of these values exist, like so:

$('div.professions:not([data-the-vals="' + myElementVal + '"])');

myElementVal可以等同于"Female_Young.Dev"或"Male_Mid.Project".

where myElementVal could equate to 'Female_Young.Dev', OR 'Male_Mid.Project'.

在控制台中,我得到以下信息:

In the console I'm getting back the following:

$('div.professions:not([data-the-vals="Female_Young.Dev"])');

这是我想要的,但似乎找不到div,因为它正在查找"Female_Young.Dev,Male_Mid.Project",而不是我想要的值的第一部分.是否可以将其拆分,或者是否可以搜索data-the-vals属性以查看其是否存在某种容量?

which is what I want but it seems to be unable to find this div as it's finding "Female_Young.Dev,Male_Mid.Project" instead of the first part of the value I want. Is it possible to split this up, or be able to search the data-the-vals attribute to see if it exists in some capacity?

推荐答案

因此请使用属性包含而不是等号.

So use attribute contains instead of equals.

$('div.professions:not([data-the-vals*="Female_Young.Dev"])');

这篇关于将元素匹配到数据属性值的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆