我是攻城狮3922093
2016-10-12 17:08
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function change(){
document.getElementById("daan").value=Math.random(document.getElementById("shuru").value);}
function arry(){
document.getElementById("shuzi").value=Math.round(document.getElementById("daan") .value ) }
</script>
<style type="text/css">
*{font-size:18px; color:#F00;}
#shuru{background:#93C;}
button{background:#0F0;}
#daan{background:#FF0;}
</style>
</head>
<body>
<form>
<input type="text" placeholder="请输入数值" value="" id="shuru"/>
<button type="button" onclick="change()">取随机值</button>
<input type="text" placeholder="输出答案" value="" id="daan"/>
<button type="button" onclick="arry()">四舍五入</button>
<input type="text" placeholder="最终答案" value="" id="shuzi"/>
</form>
</body>
</html>得出的结果如下:点击取随机值的时候回去一个随机值,但是取到的随机值永远是在0-1之间的,那么怎么才能在设置才可以取到0-1之外的数值,比如说2-8之间的数值呢?
random()默认没有参数,所以变换区间可以用Math方法,Math.random()*(max - min) + min
2到8就用round*6+2
你可以乘数字
JavaScript进阶篇
469062 学习 · 22582 问题
相似问题