开发者

jquery: adding custom key value to object

开发者 https://www.devze.com 2023-02-09 02:14 出处:网络
whats wrong with this jquery code. it is n开发者_运维技巧ot outputting anyting? var imagesToLoad = [];

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

精彩评论

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

关注公众号