for (var a = 0; a < 26; a++) {
var alpha = $('<span class="alpha" />');
alpha.addClass('alpha-' + (a % 2) + 1);
$('.alphabets').append(alpha);
$('.alpha').text(String.fromCharCode(65 + a));
开发者_Go百科 }
i am doing something like this and i know i am making mistake in
$('.alpha').text(String.fromCharCode(65 + a));
thats why i am getting "Z" in every span but i am not getting how to fix this
Change $('.alpha')
(every element that is a member of the class alpha) to alpha
(the specific element you just created and stored in that variable)
Your code overwrites the text of all <span>
's that happen to have the alpha
class.
$('.alpha').text(String.fromCharCode(65 + a));
You need to change your code so that it only alters the text of the last created <span>
:
alpha.text(String.fromCharCode(65 + a));
$('.alphabets').append(alpha);
$('.alpha').text(String.fromCharCode(65 + a));
Should be
alpha.text(String.fromCharCode(65 + a));
$('.alphabets').append(alpha);
精彩评论