如何使用jQuery创建哈希对象/数组? [英] How to create Hash object/array using jquery?
问题描述
我知道Javascript原型框架中有一个Hash()对象,但是Jquery中有这样的东西吗?
I know there is a Hash() object in the Javascript prototype framework, but is there anything in Jquery like this?
正如我想坚持一个javascript框架,而不是混合Prototype Frame工作和JQuery框架并同时使用,因为我担心会有冲突并产生副作用。
As I would like to stick with one javascript framework, rather than mixing the Prototype Frame work and the JQuery framework and use at the same time, as I worry there will be conflict and create side-effects.
所以我的问题是:如何使用jquery创建Hash对象/数组?
So my question is: how to create Hash object/array using jquery?
以下是我的函数:
Here is my function:
/* 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);
}
}
推荐答案
一个独立的哈希表实现,名为 jshashtable (完全公开:我写了它)。使用它,你的代码将是:
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);
}
}
这篇关于如何使用jQuery创建哈希对象/数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!