How to create Hash object/array using jquery?

前端 未结 5 1076
挽巷
挽巷 2021-01-05 18:55

I know there is a Hash() object in the Javascript prototype framework, but is there anything in Jquery like this?

As I would like to stick with one javascript framew

相关标签:
5条回答
  • 2021-01-05 19:36

    There is no prototype Hash equivalent in jQuery that I know of.

    Could the following be of any use to you? using 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;
        }
    }
    
    0 讨论(0)
  • 2021-01-05 19:38

    There's a standalone hash table implementation called jshashtable (full disclosure: I wrote it). Using it, your code would be:

    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 讨论(0)
  • 2021-01-05 19:38

    A basic Hash can be achieved with basic JavaScript, not jQuery (I know that Prototype's Hash adds extra functionality, but you don't use it here).

    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 讨论(0)
  • 2021-01-05 19:43

    Glancing at the jQuery documentation I'm not seeing anything that specifically for this. But you can easily do this with just a regular javascript object:

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-05 19:46

    I think you don't need jQuery for this.

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题