Home  >  Article  >  Web Front-end  >  How to implement random numbers with jquery

How to implement random numbers with jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-24 14:36:537185browse

How to use jquery to implement random numbers: First use the p tag to create an area to display random numbers; then use [Math.random()] to generate a random number, and pass the [parseInt()] method Rounding; finally, the obtained random number is output in the p tag through the [html()] method.

How to implement random numbers with jquery

The operating environment of this tutorial: windows7 system, jquery3.5.1 version. This method is suitable for all brands of computers.

How to use jquery to implement random numbers:

1. Create a new html file, named test.html, to explain how jquery generates random numbers.

How to implement random numbers with jquery

2. In the test.html file, use the p tag to create an area for displaying random numbers, and set the id of the p tag to randnum.

How to implement random numbers with jquery

3. In the test.html file, use the button tag to create a button, bind the onclick click event to the button, and when the button is clicked, execute the createRand() function .

How to implement random numbers with jquery

4. In the js tag, create the createRand() function. Within the function, define two variables x and y, which are used to set the range of random numbers. For example, the range of random numbers set here is 0 to 1000.

How to implement random numbers with jquery

5. In the createRand() function, use Math.random() to generate a random number and passparseInt()The method rounds, and the obtained random number is output in the p tag through the html() method.

How to implement random numbers with jquery

#6. Open the test.html file in the browser and click the button to view the results.

How to implement random numbers with jquery

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to implement random numbers with jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn