如何循环遍历数组并将数组的元素添加到 div 内的 span 的 data 属性中?
html
<div id="wordBank">
<span>Play</span>
<span>Game</span>
</div>
jQuery
var words = ["play", "game"];
$.each(words, function(index, value){
$("#wordBank span").each(function(){
$(this).attr('data-word', value);
});
});
电流输出
<div id="wordBank">
<span data-word="game">Play</span>
<span data-word="game">Game</span>
</div>
所需输出
<div id="wordBank">
<span data-word="play">Play</span>
<span data-word="game">Game</span>
</div>
叮当猫咪
相关分类