Home > Web Front-end > JS Tutorial > How to calculate the area of ​​a square using js

How to calculate the area of ​​a square using js

王林
Release: 2020-04-04 09:05:37
forward
3865 people have browsed it

How to calculate the area of ​​a square using js

You can use a Math.pow() function to calculate the area of ​​a square using JS.

Let’s first look at the implementation effect:

How to calculate the area of ​​a square using js

Example code:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <form method="get" action="">
  <h2>计算正方形的面积</h2>
  正方形的边长:<input type="text" id="radius"><br>
  正方形的面积:<input type="text" readonly="readonly" id="area"><br>
  <input type="button" value="计算" onclick="show()" />
  <input type="reset" value="重置" />
 </form>
</body>
<script type="text/javascript">
 function area(radius){
  var radius=document.getElementById("radius").value;//获取正方形的边长
  var area=Math.pow(radius,2);//计算正方形的面积
  return area;
 }
 function show(){
  //输出正方形的面积
  document.getElementById("area").value=area(radius);
 }
</script>
</html>
Copy after login

Related tutorial recommendations: js tutorial

The above is the detailed content of How to calculate the area of ​​a square using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template