如何循環(huán)遍歷數(shù)組并將數(shù)組的元素添加到 div 內(nèi)的 span 的 data 屬性中?html<div id="wordBank"> <span>Play</span> <span>Game</span></div>jQueryvar 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>
迭代數(shù)組并使用元素作為數(shù)據(jù)屬性
藍山帝景
2023-10-14 16:43:59