Question

I ai une matrice de dimension 2:

var fondetcaption = [
        ["fond/fond1.jpg","« aaa"],    
        ["fond/fond2.jpg","« bbb"],
        ["fond/fond3.jpg","« ccc"],
        ["fond/fond4.jpg","« ddd"]          
    ];

Ce tableau peut avoir 4, 7, 10, un certain nombre de valeur dans ...

Je voudrais savoir combien de paires de valeur je (il devrait revenir 4 dans ce cas)

var howmany = fondetcaption.lenght; // doesn't work !

Et après ça ... je vais montrer fondetcaption[0][0] (le premier arrière-plan) et après que le clic sur un bouton j'aime montrer la prochaine: [1][0] puis à côté [2][0] puis [3][0] puis [0][0] à nouveau .... pousser ne semble pas fonctionner

.

Toute idée?

Était-ce utile?

La solution

Comme les autres ont dit, il est length, pas lenght.

Mais personne ne semble avoir abordé la deuxième partie de votre question, donc:

Vous n'avez pas besoin push pour faire défiler les valeurs. Tout ce que vous avez besoin est un indice:

var fondetcaption = [
    ["fond/fond1.jpg","« aaa"],    
    ["fond/fond2.jpg","« bbb"],
    ["fond/fond3.jpg","« ccc"],
    ["fond/fond4.jpg","« ddd"]          
];
var fondetcaptionIndex = 0;

// Call this when you click your button or whatever
function getNextBackground() {
    if (fondetcaptionIndex >= fondetcaption.length) {
        fondetcaptionIndex = 0;
    }
    return fondetcaption[fondetcaptionIndex++];
}

Ou, si vous le souhaitez, vous pouvez simplement mettre l'index directement sur l'objet de tableau, puisque les objets de tableau JavaScript peuvent avoir des propriétés non-éléments arbitraires et qui aide à garder les symboles ensemble:

var fondetcaption = [
    ["fond/fond1.jpg","« aaa"],    
    ["fond/fond2.jpg","« bbb"],
    ["fond/fond3.jpg","« ccc"],
    ["fond/fond4.jpg","« ddd"]          
];
fondetcaption.index = 0;

// Call this when you click your button or whatever
function getNextBackground() {
    if (fondetcaption.index >= fondetcaption.length) {
        fondetcaption.index = 0;
    }
    return fondetcaption[fondetcaption.index++];
}

En fait, vous pouvez même faire la partie fonction du tableau:

var fondetcaption = [
    ["fond/fond1.jpg","« aaa"],    
    ["fond/fond2.jpg","« bbb"],
    ["fond/fond3.jpg","« ccc"],
    ["fond/fond4.jpg","« ddd"]          
];
fondetcaption.index = 0;
fondetcaption.getNext = function() {
    if (this.index >= this.length) {
        this.index = 0;
    }
    return this[this.index++];
};

// Use
background = fondetcaption.getNext();

Si faire le tableau lui-même le conteneur de ces propriétés supplémentaires embêtements vous (cela dérange certaines personnes), envelopper le tout dans un objet:

var fondetcaption = (function() {
    var index = 0,
        values = [
            ["fond/fond1.jpg","« aaa"],    
            ["fond/fond2.jpg","« bbb"],
            ["fond/fond3.jpg","« ccc"],
            ["fond/fond4.jpg","« ddd"]          
        ];

    function fondetcaption_getNext() {
        if (index >= values.length) {
            index = 0;
        }
        return values[index++];
    }

    return {
        values:  values,
        getNext: fondetcaption_getNext
    };
})();

// Sample use:
background = fondetcaption.getNext();

// Original array still accessible if desired as fondetcaption.values

Autres conseils

vérifier votre orthographe ..

fondetcaption.length

Avez-vous essayé:

var howmany = fondetcaption.length;

Votre message original a une faute d'orthographe, longueur doit être longueur .

Essayez length.

alert(fondetcaption.length);

Je ne sais pas quel est le problème car il fonctionne très bien ici ..

var fondetcaption = [
        ["fond/fond1.jpg","« aaa"],    
        ["fond/fond2.jpg","« bbb"],
        ["fond/fond3.jpg","« ccc"],
        ["fond/fond4.jpg","« ddd"]          
];

alert(fondetcaption.length); 

http://jsfiddle.net/tLPwp/

Peut-être fondetcaption.lenght est réellement copié à partir du code, et thusly mal orthographié.

retourne la longueur du tableau: fondetcaption.length

Vous avez une faute de frappe dans le code. Il devrait être var howmany = fondetcaption.length, et non ... fondetcaption.lenght.

Je vous suggère d'utiliser des outils tels que Firebug de, Chrome ou Safari Console. Il peut être très utile, surtout avec les problèmes comme ceux-ci. Je copie-collé votre code, il a essayé, et il fonctionne comme un charme. Avec la faute de frappe fixe mentionné, bien sûr.

Vive.

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