我如何得到一个有序的列表,随机吐出列表中的一些
How do i get an ordered list to randomly spit out only some of the list
我有一个随机显示测试中所有问题的列表,但我试图只显示部分测试。
我试着缩短列表的长度并以这种方式输出,但它仍然会分解所有列表。感谢您的帮助。
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]);
}
}
相关文章:
- 如何在 Javascript/jQuery 中获取一些随机下拉列表的选定值
- 如何随机显示列表项的顺序
- 在列表中至少使用两次随机生成的nr
- 如何使用CSS在列中显示随机生成的列表
- 如何在Javascript中设置随机生成的项目列表的样式
- 我如何得到一个有序的列表,随机吐出列表中的一些
- 如何提交从列表中随机选择的非重复变量
- 如何在一个随机化的有序列表中随机化多个无序列表
- 如何从列表中突出显示一个随机单词
- 如何使用JavaScript随机化有序列表
- 如何使用JavaScript在HTML5网站上随机选择歌曲播放(从列表中)
- 从1-9之间的随机数字列表中选出3个相同的数字
- 从列表中选择随机段落
- 从数组列表中随机选择一个段落 Javascript
- 随机音频列表在调用时不播放
- 给定一个很长的列表,我如何随机选择 X 元素
- 如何使用NightwatchJS从下拉列表中选择一个有效的随机值
- 反应:在列表随机播放的连续渲染上会发生什么
- 将无序列表随机化
- 大列表随机文本/URL - Opi