This article describes how to generate random numbers that are not repeated in javascript within a specified range and related examples. If you need them, refer. Source of question: one test question when learning jQuery in MOOC.
Initial:
Only 5 elements are displayed.
- Element, including the last
- "More" characters are displayed in the element. When you click "more", the content of the link becomes "simplified". At the same time,
Show all
- Element. When you click the "simplified" link, its content changes to "more". At the same time,
Only the last element is displayed.
- Five elements, including.
Core point: He didn't tell which ones to hide
So I want to list eight
Elements, points simplify random hiding of the first seven
Three of the elements.Ideas:
① From 0 ~ Generate three random numbers in Step 6.
② Determine whether the three random numbers are equal. If they are not equal, perform the hidden operation.
③ If a random number already exists, re-execute the function.
Implementation: Generate 0 ~ A Random Number of 6
The Code is as follows:
Var ran1 = parseInt (Math. random () * 7); // Math. random () generates a random number [)
Complete code: