我正在制作一个刽子手游戏,我能够让用户输入一个单词,并且我能够将其更改为数组。现在我似乎无法获取这些数组值并使它们显示为单独的范围。
var makeAlphabetArr;
var $wordInput = $(wordInput);
var x;
var p = $('#wordInputArr');
var button = $('button');
var startGame = $('startGame');
button.click(function(){
if (startGame){
console.log('it works');
将变量设置为 wordInput
userWord = $wordInput.val();
makeAlphabetArr();
spanValues();
}
});
获取 userWord 并拆分为数组的函数
function makeAlphabetArr(){
x = userWord;
获取用户输入并将其拆分为数组值
x = x.split('');
console.log(x);
p.val(x).hide();
}
最佳答案
您可以使用 jQuery 的 each
方法来循环遍历数组:
function makeAlphabetArr(){
x = userWord;
x = x.split('');
console.log(x);
$(x).each(function() {
p.append($('<span>').html(this));
console.log(this);
});
}
在循环内,this
将是用户输入单词的字母。
关于javascript - 如何获取数组项并在html中制作相应的span,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40914344/