Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 我如何得到一个有序的列表,随机地只吐出其中的一部分_Javascript_Html - Fatal编程技术网

Javascript 我如何得到一个有序的列表,随机地只吐出其中的一部分

Javascript 我如何得到一个有序的列表,随机地只吐出其中的一部分,javascript,html,Javascript,Html,我有一个随机显示测试中所有问题的列表,但我试图只显示部分测试 我试图缩短列表的长度并以这种方式输出,但它仍然会显示所有列表。感谢您的帮助 HTML 我想出了如何仍然随机化的问题,只输出多少我想要的。我只需要输入if(I>=30){ 临时移除儿童(临时儿童[Math.random()*i|0]) }在用于附加问题列表的for循环中,它可以正常工作。不确定这是否是最理想的方式,但它是有效的 function call_onLoad() { var ol = document.querySelect

我有一个随机显示测试中所有问题的列表,但我试图只显示部分测试

我试图缩短列表的长度并以这种方式输出,但它仍然会显示所有列表。感谢您的帮助

HTML


我想出了如何仍然随机化的问题,只输出多少我想要的。我只需要输入if(I>=30){ 临时移除儿童(临时儿童[Math.random()*i|0]) }在用于附加问题列表的for循环中,它可以正常工作。不确定这是否是最理想的方式,但它是有效的

function call_onLoad() {

var ol = document.querySelector('ol');
temp = ol.cloneNode(true);

for (var i = temp.children.length - 10; i >= 0; i--;) {
    temp.appendChild(temp.children[Math.random() * i | 0]);
    alert(temp.children.length);
       if(i>=30){
            temp.removeChild(temp.children[Math.random() * i| 0])
                }
}

ol.parentNode.replaceChild(temp, ol);

var ul = document.querySelectorAll('ul'),
    parent;
alert("found " + ul.length + " ul's");

for (var k = ul.length - 1; k >= 0; k--) {
    parent = ul[k].parentNode;
    temp = ul[k].cloneNode(true);

    for (var i = temp.children.length + 1; i--;) {
        temp.appendChild(temp.children[Math.random() * i | 0]);

    }

    parent.replaceChild(temp, ul[k]);
}

}

使用普通js非常痛苦,但这里有一个提示:1。使用css并将“所有显示”设置为“无”。2.设置为循环并运行30次,检查没有类的问题
。显示
并添加该类问题。3.使用css设置
。显示
以显示元素。此外,id或类名中不能有空格,并且没有两个id可以相同。我认为您应该用javascript定义数据,然后生成HTML。或者,如果您可以在服务器端生成代码,那可能会更好。不确定用javascript定义数据是什么意思。@Nameszero喜欢数组,例如
var quick=[[“问题”,“选项1”,“选项2”,“选项3”,“选项4”,“回答”],[…],[…],[…]
然后根据它生成html。
// Function that randomizes the test questions that are displayed along 
// with the answers as soon as the page loads

function call_onLoad() {

    var ol = document.querySelector('ol');
    temp = ol.cloneNode(true);

    for (var i = temp.children.length - 10; i >= 0; i--;) {
        temp.appendChild(temp.children[Math.random() * i | 0]);
        alert(temp.children.length);

    }

    ol.parentNode.replaceChild(temp, ol);

    var ul = document.querySelectorAll('ul'),
        parent;
    alert("found " + ul.length + " ul's");

    for (var k = ul.length - 1; k >= 0; k--) {
        parent = ul[k].parentNode;
        temp = ul[k].cloneNode(true);

        for (var i = temp.children.length + 1; i--;) {
            temp.appendChild(temp.children[Math.random() * i | 0]);

        }

        parent.replaceChild(temp, ul[k]);
    }

}
function call_onLoad() {

var ol = document.querySelector('ol');
temp = ol.cloneNode(true);

for (var i = temp.children.length - 10; i >= 0; i--;) {
    temp.appendChild(temp.children[Math.random() * i | 0]);
    alert(temp.children.length);
       if(i>=30){
            temp.removeChild(temp.children[Math.random() * i| 0])
                }
}

ol.parentNode.replaceChild(temp, ol);

var ul = document.querySelectorAll('ul'),
    parent;
alert("found " + ul.length + " ul's");

for (var k = ul.length - 1; k >= 0; k--) {
    parent = ul[k].parentNode;
    temp = ul[k].cloneNode(true);

    for (var i = temp.children.length + 1; i--;) {
        temp.appendChild(temp.children[Math.random() * i | 0]);

    }

    parent.replaceChild(temp, ul[k]);
}

}