2010-06-09 5 views
5

Je sais qu'il y a un objet Hash() dans le framework prototype Javascript, mais est-ce qu'il y a quelque chose dans Jquery comme ça? Comme je voudrais m'en tenir à un framework javascript, plutôt que de mélanger le travail Prototype Frame et le framework JQuery en même temps, car je crains qu'il y ait des conflits et des effets secondaires. Donc, ma question est: comment créer un objet/tableau de hachage en utilisant jQuery?Comment créer un objet/tableau de hachage à l'aide de jquery?

Voici ma fonction:

/* prototype framework, I want to change this to jQuery! */ 
var starSaves = new Hash(); 

function myHover(id, pos) 
{ 
    var starStrip = $('star_strip_' + id);  
    if (starSaves.keys().indexOf(id) == -1) 
    { 
     var starSave = new Array(); 
     var imgs = starStrip.select("img") 
     alert(imgs); 
     for (var i = 0; i < imgs.length; i++) 
     { 
      starSave[starSave.length] = imgs[i].src; 
      if (i < pos) 
       imgs[i].src = "/images/star_1.gif"; 
      else 
       imgs[i].src = "/images/star_0.gif"; 

     } 
     starSaves.set(id, starSave); 
    } 
} 

Répondre

0

Jetant à la documentation jQuery Je ne vois rien que spécifiquement pour cela. Mais vous pouvez facilement le faire avec juste un objet régulier javascript:

var starSaves = {}; 
function myHover(id, pos) 
{ 
    var starStrip = $('star_strip_' + id);  
    if (typeof starSaves[id] == 'undefined') 
    { 
     var starSave = new Array(); 
     var imgs = starStrip.select("img") 
     alert(imgs); 
     for (var i = 0; i < imgs.length; i++) 
     { 
      starSave[starSave.length] = imgs[i].src; 
      if (i < pos) 
       imgs[i].src = "/images/star_1.gif"; 
      else 
       imgs[i].src = "/images/star_0.gif"; 

     } 
     starSaves[id] = starSave; 
    } 
} 
4

Une base Hash peut être réalisé avec JavaScript de base, non jQuery (je sais que Hash Prototype ajoute des fonctionnalités supplémentaires, mais vous ne l'utilisez pas ici).

var starSaves = {}; 

function myHover(id, pos) 
{ 
    if (!starSaves.hasOwnProperty(id)) { 
     var starSave = starSaves[id] = []; 

     $('#star_strip_' + id + ' img').attr('src', function (index, current) { 
      starSave.push(current); 

      return (index < pos) ? '/images/star_1.gif' : '/images/star_0.gif'; 
     });   
    } 
} 
0

Il n'y a pas d'équivalent de hachage prototype dans jQuery que je connaisse.

Est-ce que ce qui suit pourrait vous être utile? utilisant jQuery

var starSaves = {}; 

function myHover(id,pos) 
{ 
    var starStrip = $('.star_strip_' + id); 
    if(!starSaves[id]) 
    { 
     var starSave = []; 
     starStrip.each(function(index,element){ 
      starSave[index] = $(element).attr('src'); 
      $(element).attr('src', index < pos ? '/images/star_1.gif' : '/images/star_0.gif'); 
     }); 
     starSaves[id] = starSave; 
    } 
} 
5

Il y a une implémentation de table de hachage autonome appelé jshashtable (divulgation complète: je l'ai écrit). En l'utilisant, votre code serait:

+0

Merci, c'est compact et très utile! – arturaz

3

Je pense que vous n'avez pas besoin de jQuery pour cela.

var hashtable = { 
    hash: {}, 
    exists: function(key){ 
     return this.hash.hasOwnProperty(key); 
    }, 
    get: function(key){ 
     return this.exists(key)?this.hash[key]:null; 
    }, 
    put: function(key, value){ 
     this.hash[key] = value; 
    } 
} 
Questions connexes