我今天刚开始学习JavaScript,这个非常简单的问题给我带来了麻烦。这只是一个更大的代码段的一部分,但是为了修复它,我隔离了它。下面是:
<DOCTYPE! html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write(blarg());
function blarg()
{
return (Math.floor(Math.random()*(10-2))+1))
}
</script>
</body>
</html>我所要做的就是生成一个介于1到10之间的随机数,然后打印这个数字。它失败了,并给了我“不知名的SyntaxError:意外的标记{。这似乎是一个非常简单的问题,我觉得非常愚蠢……
发布于 2014-07-13 02:29:04
在blarg函数定义中有语法错误。
声明函数的语法应该是
function name() {
// function code here
}完整的例子:
document.write(blarg());
//function blarg{(Math.floor(Math.random()*(10-2))+1))}
function blarg() {
return Math.floor(Math.random() * (10 - 2) + 1);
}请注意,函数必须向document.write()返回一个值,以便显示随机数。
https://stackoverflow.com/questions/24718992
复制相似问题