简体   繁体   中英

Making a Javascript array of multiple CSS classes assigned to an element with jQuery

MY javascript: Updated Again.

$('.calc').change(function () {
    var classArray = $(this).attr('class').split(',');

    $.each(classArray, function () {
        alert(classArray);
    });
});

And the input:

<input type="text" class="calc R#r# C#i#" />

The pound signs are variables, I'm using ColdFusion.

What I need to be able to do is successfully take each class and place them in an array. That way I can use that to do the required calculations for the entire table.

Help would be appreciated.

Thanks

You could simply split the class attribute value, using space as the separator:

$('.calc').change(function(){
  var classArray = $(this).attr('class').split(' ');
});

Edit: I think that you want to do this.

$('.calc').change(function(){
  var classArray = $(this).attr('class').split(' ');

  $.each(classArray, function(){
    alert(this);
  });
});

Try this running example.

CMS' answer definitely works for me. I doubt this is a browser issue but I suppose that's a possibility?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM