【问题标题】:How do I randomize multiple unordered lists within a randomized ordered list?如何在随机有序列表中随机化多个无序列表?
【发布时间】:2015-02-17 21:49:23
【问题描述】:

我正在尝试使用包含随机答案(随机化无序列表)的随机问题(随机化有序列表)进行测试。我可以让有序列表随机化,但我只能让一个无序列表随机化。该代码仅随机化它找到的第一个无序列表。我想随机化所有无序列表。任何帮助表示赞赏。

<ol>
<li>What are the three main areas of the Standard User Interface?
  <ul type="none">
    <li><input type="radio" name="q1" value="0" />A. Header, Banner, Frame, Application Window</li>
    <li><input type="radio" name="q1" value="0" />B. Content Frame, Homepage, Form </li>
    <li><input type="radio" name="q1" value="1" />C. Application Navigator, Banner Frame, Content Frame </li>
    <li><input type="radio" name="q1" value="0" />D. None of the above</li>
  </ul>
</li>

<li>In the User interface, what is the gray toolbar called which allows you to add bookmarks?<br/>

  <ul type="none">
    <li><input type="radio" name="g2" value="0" />A. Gauge</li><br />
    <li><input type="radio" name="g2" value="1" />B. Edge</li><br />
    <li><input type="radio" name="g2" value="0" />C. Remedy</li><br />
    <li><input type="radio" name="g2" value="0" />D. Banner</li><br />
  </ul><br/>
</li>

<li>What can be captured in an update set?<br/>

  <ul type="none">
    <li><input type="radio" name="g3" value="0" />A. Modified CI Rules</li><br />
    <li><input type="radio" name="g3" value="1" />B. Business Rules</li><br />
    <li><input type="radio" name="g3" value="0" />C. Scheduled Jobs</li><br />
    <li><input type="radio" name="g3" value="0" />D. None of the above</li><br />
  </ul>
</li>

<li>What should you always do before you commit an update set?<br/>

  <ul type="none">
    <li><input type="radio" name="g4" value="1" />A. Preview</li><br />
    <li><input type="radio" name="g4" value="0" />B. Merge</li><br />
    <li><input type="radio" name="g4" value="0" />C. Ignore</li><br />
    <li><input type="radio" name="g4" value="0" />D. All of the above</li><br />
  </ul>
</li>

<li>Which of the following is a Business Rule best pratice?<br/>

  <ul type="none">
    <li><input type="radio" name="g5" value="1" />A. Make business rules small and specific</li><br />
    <li><input type="radio" name="g5" value="0" />B. Use of conditions is not necessary</li><br />
    <li><input type="radio" name="g5" value="0" />C. Global business rules should be used</li><br />
    <li><input type="radio" name="g5" value="0" />D. None of the above</li><br />
  </ul>
</li>

<li>Which of the following is a Client Script best practice?<br/>

  <ul type="none">
    <li><input type="radio" name="g6" value="0" />A. Use hard coded data</li><br />
    <li><input type="radio" name="g6" value="0" />B. Maximize server lookup</li><br />
    <li><input type="radio" name="g6" value="1" />C. Do not use g_form.getReference()</li><br />
    <li><input type="radio" name="g6" value="0" />D. All of the above</li><br />
  </ul>
 </li>

 <li>Which of the following are debugging features?<br/>


  <ul type="none">
    <li><input type="radio" name="g7" value="0"/>A. Debug Business Rule</li><br />
    <li><input type="radio" name="g7" value="0"/>B. Javascript</li><br />
    <li><input type="radio" name="g7" value="1"/>C. A and B</li><br />
    <li><input type="radio" name="g7" value="0"/>D. None of the above</li><br />
  </ul>
</li>


</ol>

这是我的随机化代码:

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

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

    ul.parentNode.replaceChild(temp, ol); 
}

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

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

    ul.parentNode.replaceChild(temp, ul); 
}

【问题讨论】:

  • 你能发布你用来随机化列表的代码吗?
  • 旁注:如果您将它们随机化并且“以上所有内容”在列表中排名第三,这是非常糟糕的做法。这是否包括第四个选择?另外,你不会让 A/B/C/D 出现故障吗?那么,如果你不知道哪些字母会被标记为这些字母,你如何得到像“A 和 B”这样的答案呢?
  • 这还不完整,现在会有 50 个问题,我只是想弄清楚如何随机化一堆列表,我发布了我必须随机化的代码。
  • 在您的第一个 for 循环中,您引用变量 ul,而不是当时已经定义的 ol
  • 如果您使用这样的单选按钮值,参加测试的人将能够从检查员那里查找正确答案! :P

标签: javascript html list random


【解决方案1】:

document.querySelector() 适用于 ol 元素,因为只有一个。要捕获uls,请使用querySelectorAll 并循环遍历相同的值。还有一个问题是olul的父节点丢失了。 parentNode.replaceChild... 语句必须在 for 循环之后,而不是在循环之内。在querySelectorAll 值的循环中,必须备份父节点;见setting parent node in javascript

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

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

    }
    ol.parentNode.replaceChild(temp, ol); 

    var ul = document.querySelectorAll('ul'), parent;
    console.log("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]);
    }

【讨论】:

  • 啊,我明白了,非常感谢你,我将 replaceChild 放在所有内容之后。也不知道 querySelectorAll() 方法。
猜你喜欢
  • 1970-01-01
  • 2012-01-03
  • 2011-12-06
  • 1970-01-01
  • 2010-09-15
  • 2020-05-02
  • 2013-01-11
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多