我有一个像这样的HTML列表:
<ul>
<li>This is an item.</li>
<li>This is another item.</li>
<li>This is yet another item.</li>
</ul>
每次加载文档时,如何以随机顺序显示这些项目?
答案 0 :(得分:1)
添加此javascript:
var ul = document.getElementById("list");
for (var i = ul.children.length; i >= 0; i--)
ul.appendChild(ul.children[Math.random() * i | 0]);
(此答案最初由Alexey Lebedev针对此问题提供:javascript - shuffle HTML list element order)
答案 1 :(得分:1)
因为它在这里回答javascript - shuffle HTML list element order
var ul = document.getElementById(&#34; item&#34;);
for(var i = ul.children.length; i&gt; = 0; i--) { ul.appendChild(ul.children [Math.random()* i | 0]); }