js绝对值函数的使用abs()
编写方式:
Math.abs(-1);
其中-1为要求绝对值的数值。
注意:
在求取一个数值的绝对值时可以使用abs函数,由于此函数属于Math类,所有在前边要加上Math.。
范例:
- <html>
- <head>
- <title>绝对值函数的使用</title>
- </head>
- <body>
- <script language="javascript">
- <!--
- document.write("0的绝对值为:",Math.abs(0),"<br>");
- document.write("1的绝对值为:",Math.abs(1),"<br>");
- document.write("-1的绝对值为:",Math.abs(-1),"<br>");
- //-->
- </script>
- </body>
- </html>
新闻热点
疑难解答