jQuery 从类数组中查找类名值
我试图找到包含文本 iconGroup
的类名的确切值,并且我能够使用以下方法获取类名数组:
var chosenIconClassArray = $('.selectedGroupIcon').attr('class').split(/\s+/);
console.log(chosenIconClassArray);
$.each(chosenIconClassArray, function(index, item) {
console.log(item);
});
这给出了控制台输出:
["createAGroupIconList", "inlinelist", "ram", "left", "iconGroup20", "selectedGroupIcon"]
createAGroupIconList
inlinelist
ram
left
iconGroup20
selectedGroupIcon
我已经尝试了一些方法的这个来找到它是哪个iconGroup
,但它不起作用:
if (item ^= 'iconGroup') {
console.log($(this));
}
目标是能够告诉类数组包含值iconGroup20
(或任何数字)例如 1、2、3、4 等),然后我可以将其添加到另一个元素以更新其图标。
我怎样才能搜索该数组,然后找到其中的 iconGroup
类?
I am trying to find the exact value of a class name containing the text iconGroup
and I am able to get an array of class names using:
var chosenIconClassArray = $('.selectedGroupIcon').attr('class').split(/\s+/);
console.log(chosenIconClassArray);
$.each(chosenIconClassArray, function(index, item) {
console.log(item);
});
This gives a console output of:
["createAGroupIconList", "inlinelist", "ram", "left", "iconGroup20", "selectedGroupIcon"]
createAGroupIconList
inlinelist
ram
left
iconGroup20
selectedGroupIcon
I have tried something along the lines of this to find which iconGroup
it is, but it didn't work:
if (item ^= 'iconGroup') {
console.log($(this));
}
The goal is to be able to tell that the class array contains the value iconGroup20
(or any number such as 1, 2, 3, 4, etc) which I can then add to another element to update it's icon.
How can I go about searching that array and then find which iconGroup
class is in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否正在寻找能够在项目上查找与格式“iconGroup##”匹配的类的东西?如果是这样,请尝试如下操作:
已更新为函数,以便您可以使用
getIconGroup(ChosenIconClassArray)
。Are you looking for something that finds classes on an item that match the format 'iconGroup##'? If so, try something like this:
Updated to act as a function so you can use
getIconGroup(ChosenIconClassArray)
.JavaScript 有一个
String.indexOf
方法。它将返回您提供的字符串的索引,如果未找到该字符串,则返回 -1。下面的代码应该会给你你想要的类。
变量
foundClass
应包含完整的类。JavaScript has a
String.indexOf
method. It'll return the index of the string you give it, or -1 if the string wasn't found.The following piece of code should give you the class you're hoping for.
The variable
foundClass
should contain the full class.