Question

Here's a another noob question.

Again working with YUI3.

How can I change:

var values = [2, 3, 4];

to include ALL whole numbers, and not just 2, 3, and 4 ?

Thanks!

EDIT:

Here's a bit of extra info.

This is what I have:

var numbers = [2, 3, 4];

for(var i = 0; i < numbers.length; i++)
{
  var boxnum = numbers[i];
  Y.all(".box"+boxnum ).addClass(boxnum );  
}

so just want to check for each possible class of . box1, .box2, .box3, .... ,but I'm not sure how many of these classes there might be.

Ta!

Était-ce utile?

La solution

To Find all elements with class "box" + x:

for (var x = 0; x <= 9999; x++) {
    if ($('.box' + x).length < 1) {
        $('.box' + x).addClass('yourClass');
    } else {
        break;
    }
}

This assumes you wont have box x if you don't have box (x-1). (boxes are numbered in order without any missing numbers) If this assumption is not correct, let me know.

Autres conseils

for some reason I would not recommend looping through and calling the jQuery function 1000 times, I would recommend you call it once. Something like this perhaps

var elements = $('*').filter( function() { 
    return /box[0-9]/.test( this.className )
});

Demo: http://jsfiddle.net/qjVsU/

Benchmarks: http://jsperf.com/class-starts-with-selector-jquery

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top