本文摘自PHP中文网,作者V,侵删。

用JS实现计算正方形的面积可以使用一个Math.pow()函数。
先来看实现效果:

实例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!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>
|
相关教程推荐:js教程
以上就是如何利用js计算正方形的面积的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
js怎么跳转到新页面?
js事件之自建函数bind()与兼容性问题解决
js 基本类型与引用类型值
网页代码中js和css指的是什么
爬虫分析之 js逆向某验滑动加密(1)
直击js,jquery获取屏幕的宽度和高度代码
简单实用的进度条加载组件loader.js
console.log()的作用
js代表什么
js的prototype是什么
更多相关阅读请进入《js》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 如何利用js计算正方形的面积