我如何得到一个有序的列表,随机吐出列表中的一些

How do i get an ordered list to randomly spit out only some of the list

本文关键字:列表 随机 何得 一个      更新时间:2023-09-26

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

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

HTML

<!-- There are 40 questions but only want 30 to dislpay -->
<ol>
   <li >
      <p id = "question 1">What are the three main areas of the Standard User Interface?</p>
      <ul type="none">
         <li><input type="radio" name="q1" value="0"  /> Header, Banner, Frame, Application Window</li>
         <li><input type="radio" name="q1" value="0"  /> Content Frame, Homepage, Form </li>
         <li><input type="radio" name="q1" value="1"  /> Application Navigator, Banner Frame, Content Frame </li>
         <li><input type="radio" name="q1" value="0"  /> Larry, Moe, and Curly</li>
      </ul>
   </li>
   <li>
      <p id = "question 2">In the  User interface, what is the gray toolbar called which allows you to add bookmarks?</p>
      <ul type="none">
         <li><input type="radio" name="q2" value="0" /> Gauge</li>
         <li><input type="radio" name="q2" value="1" /> Edge</li>
         <li><input type="radio" name="q2" value="0" /> Remedy</li>
         <li><input type="radio" name="q2" value="0" /> Banner</li>
      </ul>
   </li>
   <li>
      <p id = "question 3">What can be captured in an update set?</p>
      <ul type="none">
         <li><input type="radio" name="q3" value="0" /> Modified CI Rules</li>
         <li><input type="radio" name="q3" value="1" /> Business Rules</li>
         <li><input type="radio" name="q3" value="0" /> Scheduled Jobs</li>
         <li><input type="radio" name="q3" value="0" /> None of these</li>
      </ul>
   </li>
</ol>

JavaScript

// 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]);
    }
}

我想好了如何仍然随机化问题,只输出我想要的内容。我只是假设(I>=30){temp.removeChild(temp.childs[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]);
}
}