bir*_*git 1 arrays random jquery text
我想让divs随机填充存储在jquery数组中的文本.我的HTML看起来像
<div class="row">
<div class="cell"> </div>
<div class="cell"> </div>
<div class="cell"> </div>
<div class="cell"> </div>
<div class="cell"> </div>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的jQuery中,我有一个看起来像的数组
var fruits = ["Apple", "Pear", "Orange"]
Run Code Online (Sandbox Code Playgroud)
每个单元格应该随机填充一个水果,但我坚持正确迭代并用类单元格挑选每个div的随机值.这段代码显然不起作用:
$.each(fruits), function(fruit) {
var fruit = fruits[Math.floor(Math.random()*fruits.length)];
$('.row .cell').text(fruit);
}
Run Code Online (Sandbox Code Playgroud)
如何使随机猜测恰好发生5次并正确填充div?
试试这个
$('.cell').each(function(){
$(this).text(fruits[Math.floor(Math.random()*fruits.length)])
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1045 次 |
| 最近记录: |