jQuery 从类数组中查找类名值

发布于 2024-10-16 15:27:45 字数 775 浏览 4 评论 0原文

我试图找到包含文本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-10-23 15:27:45

您是否正在寻找能够在项目上查找与格式“iconGroup##”匹配的类的东西?如果是这样,请尝试如下操作:

function getIconGroup(class_array) {
  var iconGroup = false;
  $.each(class_array, function(index, item) {
    if item.match(/^iconGroup[0-9]+$/) {
      iconGroup = item;
    }
  });
  return 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:

function getIconGroup(class_array) {
  var iconGroup = false;
  $.each(class_array, function(index, item) {
    if item.match(/^iconGroup[0-9]+$/) {
      iconGroup = item;
    }
  });
  return iconGroup;
}

Updated to act as a function so you can use getIconGroup(ChosenIconClassArray).

计㈡愣 2024-10-23 15:27:45

JavaScript 有一个 String.indexOf 方法。它将返回您提供的字符串的索引,如果未找到该字符串,则返回 -1。

下面的代码应该会给你你想要的类。

var chosenIconClassArray = $('.selectedGroupIcon').attr('class').split(/\s+/),
    foundClass;
$.each(chosenIconClassArray, function(index, item) {
    if (item.indexOf('iconGroup') > -1) {
        foundClass = item;
    }
});

变量 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.

var chosenIconClassArray = $('.selectedGroupIcon').attr('class').split(/\s+/),
    foundClass;
$.each(chosenIconClassArray, function(index, item) {
    if (item.indexOf('iconGroup') > -1) {
        foundClass = item;
    }
});

The variable foundClass should contain the full class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文