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]); });
- http://api.jquery.com/each/
- http://api.jquery.com/jQuery.each/
精彩评论