我正在尝试创建一个简单的javascript应用程序,它将要求用户输入一个圆的半径,然后在用户点击“Calculate”后将以句子形式显示周长和区域。现在,当用户输入一个数字并点击“计算”时,没有任何反应。
JavaScript包含在HTML文档中,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Create a Circle</title>
<h1 style="text-align:center">Create a Circle!</h1>
<br>
<div style="text-align:center">
Enter the radius for your circle:
<input type="text" id="txtRadius" size="10" />
<br>
<input type="button" value="Calculate" onclick="CalculateArea()"/>
<script>
function print() {
var p =
document.createElement("p"),
text = Array.prototype.join.call(arguments,",");
p.textContent = text;
document.getElementById("console").appendChild(p);
return text;
}
function CalculateCircumference() {
var radius =
parseInt(document.getElementById('txtRadius').value);//String to Integer
if (0 < radius)
print("The circumference of the circle is " + (radius * 2 * Math.PI);
else
print("Error - radius must be a whole number greater than 0.");
return false;
}
function CalculateArea() {
var radius =
parseInt(document.getElementById('txtRadius').value); //String to Integer
if (0 < radius)
print("The area of the circle is " + (radius * radius * Math.PI);
else
print("Error - radius must be a whole number greater than 0.");
return false;
}
</script>
</head>
<body>
</body>
</html>
我是打印方法的新手,所以我尝试将所有打印件更改为“警告”,但这没有做任何事情。谢谢您的帮助。链接到JSFiddle:https://jsfiddle.net/HappyHands31/xzsf8ca4/2/
答案 0 :(得分:4)
您的代码存在更多问题,但这会回答您的问题
没有任何反应的原因是因为您错过了)
并且使该功能无效。
更改此内容:print("The area of the circle is " + (radius * radius * Math.PI);
:print("The area of the circle is " + (radius * radius * Math.PI));
答案 1 :(得分:0)
function CalculateCircumference() {
var radius =
parseInt(document.getElementById('txtRadius').value);//String to Integer
if (radius > 0)
//calculate
else
//error
return false;
}