2010-06-09 16 views
5

Sé que hay un objeto Hash() en el marco de prototipo Javascript, pero ¿hay algo en Jquery como este?¿Cómo crear el objeto/matriz Hash usando jquery?

Como me gustaría seguir con un framework javascript, en lugar de mezclar el trabajo Prototype Frame y el framework JQuery y usarlo al mismo tiempo, ya que me preocupa que haya conflicto y cree efectos secundarios.

Así que mi pregunta es: ¿cómo crear Hash objeto/matriz utilizando jquery?

Aquí es mi función:

/* 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); 
    } 
} 

Respuesta

0

Echando un vistazo a la documentación de jQuery que no estoy viendo nada que específicamente para esto. Pero se puede hacer esto fácilmente con sólo un objeto de javascript normal:

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

Un básico Hash se puede lograr con JavaScript básica, no jQuery (Sé que Hash de Prototype añade una funcionalidad adicional, pero no lo uso aquí)

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

No hay prototipo de hash equivalente en jQuery que yo sepa.

¿Podría ser de alguna utilidad para usted? usando 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

Hay una aplicación tabla hash independiente llamado jshashtable (información completa: lo escribí). Utilizándolo, su código sería:

var starSaves = new Hashtable(); 

function myHover(id, pos) 
{ 
    var starStrip = $('star_strip_' + id); 
    if (!starSaves.containsKey(id)) 
    { 
     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.put(id, starSave); 
    } 
} 
+0

Gracias, es compacto y muy útil. – arturaz

3

Creo que no necesita jQuery para esto.

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; 
    } 
} 
Cuestiones relacionadas