jQuery - 检查元素是否有来自数组的类? [英] jQuery - Check if element has a class from an array?

查看:146
本文介绍了jQuery - 检查元素是否有来自数组的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查一个元素是否有一个数组的类,如果是这样的类的值是什么。目前我只是使用:

  if($(this).hasClass('purple_grad')){
$ thisFKeyColour ='purple_grad';
} else if($(this).hasClass('red_grad')){
$ thisFKeyColour ='red_grad';
} else if($(this).hasClass('green_grad')){
$ thisFKeyColour ='green_grad';
} else if($(this).hasClass('blue_grad')){
$ thisFKeyColour ='blue_grad';
} else if($(this).hasClass('yellow_grad')){
$ thisFKeyColour ='yellow_grad';
} else if($(this).hasClass('light_yellow_grad')){
$ thisFKeyColour ='light_yellow_grad';
} else if($(this).hasClass('lighter_yellow_grad')){
$ thisFKeyColour ='lighter_yellow_grad';
} else if($(this).hasClass('grey_grad')){
$ thisFKeyColour ='grey_grad';
} else if($(this).hasClass('light_grey_grad')){
$ thisFKeyColour ='light_grey_grad';
} else if($(this).hasClass('black_grad')){
$ thisFKeyColour ='black_grad';
}
}
alert($ thisFKeyColour);

有更好的方法吗?像:

  if(in_array(classes,element.attr('class'))){
$ thisFKeyColour = Class_Of_Element_That_Matched_Array;
}

此外,我不能只返回.attr

解决方案



/ div>

cols是你的类名数组。这是未经测试的,但会工作,无论元素有多少类。

  for(var i = 0; i  {
if($(this).hasClass(cols [i]))
{
$ thisFKeyColour = cols [i]
break;
}
}


Im trying to check if an element has a class from an array and if so what is the value of that class. At the moment I am just using:

if ($(this).hasClass('purple_grad')) {
            $thisFKeyColour = 'purple_grad';
        } else if ($(this).hasClass('red_grad')) {
            $thisFKeyColour = 'red_grad';
        } else if ($(this).hasClass('green_grad')) {
            $thisFKeyColour = 'green_grad';
        } else if ($(this).hasClass('blue_grad')) {
            $thisFKeyColour = 'blue_grad';
        } else if ($(this).hasClass('yellow_grad')) {
            $thisFKeyColour = 'yellow_grad';
        } else if ($(this).hasClass('light_yellow_grad')) {
            $thisFKeyColour = 'light_yellow_grad';
        } else if ($(this).hasClass('lighter_yellow_grad')) {
            $thisFKeyColour = 'lighter_yellow_grad';
        } else if ($(this).hasClass('grey_grad')) {
            $thisFKeyColour = 'grey_grad';
        } else if ($(this).hasClass('light_grey_grad')) {
            $thisFKeyColour = 'light_grey_grad';
        } else if ($(this).hasClass('black_grad')) {
            $thisFKeyColour = 'black_grad';
        }
}
alert($thisFKeyColour);

Is there a better way of doing this? Something like:

if (in_array(classes, element.attr('class'))) {
    $thisFKeyColour = Class_Of_Element_That_Matched_Array;
}

Also, I cant just return the .attr('class') as their are multiple classes on the element.

Cheers
Charlie

解决方案

cols is your array of class names. This is untested but will work regardless of number of classes an element has.

for ( var i = 0; i < cols.length; i++ )
{
  if ( $(this).hasClass( cols[i] ) )
  {
    $thisFKeyColour = cols[i];
    break;  
  }
}

这篇关于jQuery - 检查元素是否有来自数组的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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