max函数可以用于返回多个数字中最大的那个值,如果没有传递参数,则结果为“-Infinity”,如果至少有一个参数无法转换为数字,则结果为NaN。下面我们就来看看max函数的具体使用方法。 
我们先来看一下max函数的基本语法
Math.max(value1,value2,...) Value1,Value2,……:传递到math.max()函数的值,用于查找最大值。 下面我们来看具体的示例 当参数是正数和负数时: 代码如下 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
document.write("Output : " + Math.max(10, 32, 2));
document.write("Output : " + Math.max(-10, -32, -1));
</script>
</body>
</html> 执行结果如下:
Output : 32
Output : -1 没有参数传递时 代码如下: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
document.write("Output : " + Math.max());
</script>
</body>
</html> 执行结果如下 Output : -Infinity 当参数中有参数无法转换为数字时: 代码如下 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
document.write("Output : " + Math.max(10,2,NaN));
</script>
</body>
</html> 执行结果如下
Output : NaN 本篇文章到这里就已经全部结束了,更多精彩内容大家可以关注模板之家(www.mb5.com.cn)的其他相关栏目教程!!! 以上就是max函数怎么使用的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |