gpt4 book ai didi

javascript - 为什么我只获得一个 math.random 值以及如何获得更多值?

转载 作者:行者123 更新时间:2023-11-28 18:54:38 25 4
gpt4 key购买 nike

这是我的代码。问题是它生成一个 math.random 值,仅此而已。我需要它在每次单击 div 后继续生成值。

var rand = Math.random();
$(document).ready(function() {
if(rand < .5) {
$('div').click(function() {
$(this).addClass('blue');
});
} else if(rand < .7) {
$('div').click(function() {
$(this).toggleClass('red');
});
} else if(rand < .9) {
$('div').click(function() {
$(this).toggleClass('purple');
});
} else {
$('div').click(function() {
$(this).toggleClass('black');
});
}
});

最佳答案

如果您希望逻辑在每次单击 div 时运行,则应该生成随机数,并且树是否驻留在 div 单击回调中。像这样的东西:

$('div').click(function() {
var rand = Math.random();
if(rand < .5) {
$(this).addClass('blue');
} else if (rand < .7) {
$(this).addClass('red');
}
// and so on
});

关于javascript - 为什么我只获得一个 math.random 值以及如何获得更多值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33861655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com