开发者

assign each elem of an array of values to each element in an array of elements jQuery

When I do $('#conatiner1 span') on my page, it returns an 开发者_运维技巧array with 11 span elements. I also have an array 'vals' with 11 different values.

Is there a fast 'do more, write less' jquery command to easily assign each value of val to each span element?

span 1 -> val 1;
span 2 -> val 2; 
span 3 -> ...


Yes, take a look at jquery each methods. There are few of them and all of them are passing element index to your handler function.

For example, if you store text in values which you want to put into those spans:

var values = [...] // some values
$('#conatiner1 span').each(function(i) { $(this).html(values[i]); });
  1. http://api.jquery.com/each/
  2. http://api.jquery.com/jQuery.each/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜