简体   繁体   English

如何从数组中随机获取字符串项目并将字符串单词的每个字符随机放入li标签

[英]How to get random string items from an array randomly and put each character of the string word into li tags randomly

I am trying to prepare a vocabulary practice exercise. 我正在尝试准备词汇练习。 In this exercise the letters of a word are seperated in li tags and below that there is a an answer button to show the correct order of the letters. 在本练习中,单词的字母以li标签分隔,下面是一个应答按钮,以显示字母的正确顺序。 Here you can see how a "rabbit" word asked. 在这里,您可以看到如何询问“兔子”一词。

<div>
<div id="container4" class="orderD">
   <ul  id="items4" style="margin:auto">
        <li class="list hvr">A</li>
        <li class="list hvr">I</li>
        <li class="list hvr">R</li>
        <li class="list hvr">B</li>
    <li class="list hvr">B</li>
    <li class="list hvr">T</li>
    </ul> 
</div>
</div>

<!-- display Answer section-->
<div>
<div  class="altCizgi">
  <ul style="margin:auto;margin-bottom:4px;">
    <p class="cevapsStili" id="p4">RABBIT</p>
       <div style="display:block;margin:auto;margin-bottom:4px;text-align: center;"> 
       <button class="bttnStyle" id="show4">▼ Answer</button></div>
  </ul> 
  </div>
</div>

well every thing is ok by the help of ready codes found on internet and a user can drag the letters into the correct order and see the answer. 借助互联网上提供的现成代码,每件事都可以解决,用户可以将字母拖入正确的顺序并查看答案。 BUT the words are the same all the time. 但是单词一直都是一样的。 how can I get random words from an array and put each letter of the words into the li tags randomly. 如何从数组中获取随机单词并将单词的每个字母随机放入li标签中。 So that the user can practice different words on each page load. 这样用户就可以在每次加载页面时练习不同的单词。 I've found the shuffle word code below but I cant figure out how to put letters in to the li tags. 我在下面找到了随机的单词代码,但我不知道如何在li标签中放入字母。 I am not good at javascript. 我不擅长javascript。

<script>
function getRandomSubarray(arr, size) 
{
var shuffled = arr.slice(0), i = arr.length, temp, index;
while (i--) 
{
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];shuffled[i] = temp;
}
return shuffled.slice(0, size);
}
$(document).ready( function () {
var mywordsList = ["class","classroom","book","bookcase","brush","calendar","clock","crayon"]
;callwords = getRandomSubarray(mywordsList, 7);
var words = new String(callwords);
</script>

Ok, first of all I added JQuery which helps with DOM manipulation a lot (It's that $ sign stuff). 好的,首先我添加了JQuery ,它对DOM操作有很大帮助(这是$符号的东西)。

After that i get the ul element $("#items4"); 之后,我得到ul元素$("#items4"); with this and save into a variable. 与此并保存到变量中。 You already had your shuffled array with words. 您已经用单词打乱了数组。 So I made the first Word in the list into an array ( var word = callwords[0].split(""); ) and called that shuffle function again to mesh up the characters: var shuffledWord = getRandomSubarray(word, word.length); 因此,我将列表中的第一个Word放入一个数组中( var word = callwords[0].split(""); ),并再次调用该shuffle函数来var shuffledWord = getRandomSubarray(word, word.length);字符: var shuffledWord = getRandomSubarray(word, word.length); . And changing the solution to word , $("#p4").html(word) 并将解决方案更改为word$("#p4").html(word)

After this its quite easy you empty your unordered list ul.empty(); 在此之后,您很容易清空无序列表ul.empty(); and then append all the li's with one char in it one by one. 然后在所有li后面附加一个字符。 shuffledWord.forEach(function(char){ ul.append('<li class="list hvr">'+ char +'</li>') }); by iterating over the shuffled word. 通过遍历洗过的单词。

 function getRandomSubarray(arr, size) { var shuffled = arr.slice(0), i = arr.length, temp, index; while (i--) { index = Math.floor(parseInt((i + 1) * Math.random())); temp = shuffled[index]; shuffled[index] = shuffled[i];shuffled[i] = temp; } return shuffled.slice(0, size); } var mywordsList = ["class","classroom","book","bookcase","brush","calendar","clock","crayon"]; callwords = getRandomSubarray(mywordsList, mywordsList.length); var word = callwords[0].split(""); $("#p4").html(word) var shuffledWord = getRandomSubarray(word, word.length); var ul = $("#items4"); ul.empty(); shuffledWord.forEach(function(char){ ul.append('<li class="list hvr">'+ char +'</li>') }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <div id="container4" class="orderD"> <ul id="items4" style="margin:auto"> <li class="list hvr">A</li> <li class="list hvr">I</li> <li class="list hvr">R</li> <li class="list hvr">B</li> <li class="list hvr">B</li> <li class="list hvr">T</li> </ul> </div> </div> <!-- display Answer section--> <div> <div class="altCizgi"> <ul style="margin:auto;margin-bottom:4px;"> <p class="cevapsStili" id="p4">RABBIT</p> <div style="display:block;margin:auto;margin-bottom:4px;text-align: center;"> <button class="bttnStyle" id="show4">▼ Answer</button></div> </ul> </div> </div> 

And as a side note for the future if you see this in your code callwords = getRandomSubarray(mywordsList, 7) , don't do this because if you change your mywordsList eg by adding one more word you always have to change the function call by counting up the value. 并作为将来的注释,如果您在代码中看到此callwords = getRandomSubarray(mywordsList, 7) ,请不要这样做,因为如果您更改mywordsList例如,通过添加一个以上的字,则您始终必须更改函数调用计算价值。 I corrected this in my example. 我在示例中对此进行了更正。 Just do callwords = getRandomSubarray(mywordsList, mywordsList.length) and you never have to think about it again:). 只需执行callwords = getRandomSubarray(mywordsList, mywordsList.length) ,就无需再考虑它了:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM