Home>

When writing the name of an element with jQuery, there is a number in a part of the name, and the number is incremented by one. Please help me because I don't know how to write the numbers.

Corresponding source code
var numb = // integer from 1 ~ n;
$('# mCSB_'+ numb +'_ scrollbar_vertical'). removeClass ('mCS-minimal');


I don't know how to retrieve this integer.

What I tried
var n = 100;// Change the value of this n as needed. I set it to 100 for the time being.
// Store integers from 1 to n in order in array arr
var arr = [];
var numb = for (var i = 0;i<n;i ++) {arr [i] = i + 1;};
Parsing error: Unexpected token for var numb = for (var i = 0;i<n;i ++) {arr [i] = i + 1;};


I got an error in the text editor and the page couldn't be loaded (js didn't work).
What should i do?

Supplementary information (FW/tool version, etc.)

Chrome latest version, Safari 604.1

  • Answer # 1

    $(function () {
      $('[id]'). filter (function () {
        var min = 1;
        var max = 10;
        if (r = $(this) .attr ('id'). match (/ ^ mCSB_ (\ d +) _scrollbar_vertical $/)) {
          var num = parseInt (r [1]);
          return num>= min&&num<= max;
        }
      }). removeClass ('mCS-minimal');
    });

  • Answer # 2

    I made a point early and posted a request for addition/correction, and I am very sorry.

    nFrom individual elementsmCS-minimalYou want to delete the class, right?

    I got an error in the text editor and the page couldn't be loaded (js didn't work).

    The cause of this isforI put the statement on the right side of the assignment statement.

    If you fix it while making the best use of the current code as much as possible,

    var n = 100;
    var arr = [];
    for (var i = 0;i<n;i ++) {arr [i] = i + 1;};
    arr.forEach (function (numb) {
        $('# mCSB_'+ numb +'_ scrollbar_vertical'). removeClass ('mCS-minimal');
    });

    ↑ It looks like this.

    But simpler

    var n = 100;
    for (var i = 0;i<n;i ++) {
        $('# mCSB_'+ (i + 1) +'_scrollbar_vertical'). removeClass ('mCS-minimal');
    }

    ↑ I think this is the same.