开发者

jquery: adding custom key value to object

whats wrong with this jquery code. it is n开发者_运维技巧ot outputting anyting?

var imagesToLoad = [];

var name = 'hi';
var src = 'ho';

imagesToLoad[name] = src;

$.each(imagesToLoad, function(index, value) {
 alert(index + ': ' + value);
});

basically i want to add custom variables to my object after it has been created.


Javascript arrays don't support non numerical indexes. You probably want to use an object instead:

var imagesToLoad = {};
imagesToLoad.hi = 'ho';

$.each(imagesToLoad, function(index, value) {
 alert(index + ': ' + value);
});


You should check the doc for $.each method - it accepts only callback function as parameter and it can iterate only over jQuery object

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜