Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不显示10个随机框_Javascript_Jquery - Fatal编程技术网

Javascript 不显示10个随机框

Javascript 不显示10个随机框,javascript,jquery,Javascript,Jquery,我的问题是,我没有返回10个随机框,而是只返回一个。更改 $(function() { max = 25; min = 1; var i = 0; do { var match = Math.ceil(Math.random() * (max - min )- min); $('td').each(function() { i++; console.log( match );

我的问题是,我没有返回10个随机框,而是只返回一个。

更改

$(function() {
    max = 25;
    min = 1;
    var i = 0;

    do {
        var match = Math.ceil(Math.random() * (max - min )- min);

        $('td').each(function() {
            i++;
            console.log( match );   
            if (i == match) {
                $(this).addClass('active');
            }       
        })
    } while ($('td.active').length == 10);
})


试试这个:如果你确定他们至少有10个td

$(function() {
    var max = 25; // don't forget the "var", if you don't want to declare global variables
    var min = 1;
    do {
        var match = Math.ceil(Math.random() * (max - min )- min);
        $('td').eq(match).addClass('active');
     } while ($('td.active').length < 10);
})
$(函数(){
最大值=25;
最小值=1;
var i=0;
var y=0;
做{
var match=Math.ceil(Math.random()*(max-min)-min);
$('td')。每个(函数(){
i++;
控制台日志(匹配);
如果(i==匹配){
$(this.addClass('active');
y++;
}       
})

}虽然(y您没有说太多关于代码试图做什么,但是除了其他建议之外,我认为您的意思也是

   $(function() {
    max = 25;
    min = 1;
    var i = 0;
    var y=0;

    do {
        var match = Math.ceil(Math.random() * (max - min )- min);

        $('td').each(function() {
            i++;
            console.log( match );   
            if (i == match) {
                $(this).addClass('active');
                y++;
            }       
        })
    } while (y<10);
})

当我更改浏览器时,请注意“+min”而不是负数。

crashes@NikiLichev我修复了另一个逻辑问题。请参阅编辑
$(function() {
    var max = 25; // don't forget the "var", if you don't want to declare global variables
    var min = 1;
    do {
        var match = Math.ceil(Math.random() * (max - min )- min);
        $('td').eq(match).addClass('active');
     } while ($('td.active').length < 10);
})
   $(function() {
    max = 25;
    min = 1;
    var i = 0;
    var y=0;

    do {
        var match = Math.ceil(Math.random() * (max - min )- min);

        $('td').each(function() {
            i++;
            console.log( match );   
            if (i == match) {
                $(this).addClass('active');
                y++;
            }       
        })
    } while (y<10);
})
var match= Math.ceil(Math.random() * (max - min )+ min);