0

我们最近开始在我的 HTML 课程中使用 Javascript,我似乎在我的所有程序中都遇到了一个我无法完全挑出的问题。我会问老师,但我一直在家里工作,我们正在休息,这会一直在吃我。我已经查看了“功能未定义”问题的其他答案,但他们并没有为我做这件事。那么我一直做错了什么?我敢打赌我的格式有问题。

<!DOCTYPE html> 
<html> 
<head><title>Area and Perimeter of a trapezoid</title></head>

<center><body> 

<h1>Area and Perimeter of a Trapezoid</h1> 

<p>Enter the first base length:
<input id="b1" type="text"> </br> 

Enter the second base length: 
<input id="b2" type="text"> </br> 

Enter the height of the trapezoid: 
<input id="height" type="text"> </br> 

Enter the length of the sides of the trapezoid: 
<input id="side" type="text"> </br>
</p> 

<button type="button" onclick="trapArea()">Area</button> 
<p id="area"></p>

<button type="button" onclick="trapPer()">Perimeter</button> 
<p id="perimeter"></p>

<script> 

function trapArea()
{
var b1=document.getElementById("b1"); 
var b2=document.getElementById("b2");
var height=document.getElementById("height"); 
var area=(.5*(b1+b2)*height);
document.getElementById("area").innerHTML="The area of this trapezoid is "+area+".</br>". 
}

function trapPer()
{
var b1=document.getElementById("b1"); 
var b2=document.getElementById("b2"); 
var side=document.getElementById("side"); 
var perimeter=(b1+b2+side); 
document.getElementById("perimeter").innerHTML="The perimeter of this trapezoid is         "+perimeter+".</br>".
}

</script>
</center>
</body> 
</html>
4

2 回答 2

1

您正在使用您的 dom 元素(htmlObjects)而不是它们的值(文本,在这种情况下可以解析为数字)。尝试这个

function trapArea()
{
var b1=document.getElementById("b1").value; 
var b2=document.getElementById("b2").value;
var height=document.getElementById("height").value; 
console.log(b1,b2,height);
var area=(.5*(b1+b2)*height);
document.getElementById("area").innerHTML="The area of this trapezoid is "+area+".</br>";
}
于 2013-11-27T23:20:31.030 回答
0

替换最后一个时期

document.getElementById("area").innerHTML="The area of this trapezoid is "+area+".</br>". 

document.getElementById("perimeter").innerHTML="The perimeter of this trapezoid is         "+perimeter+".</br>".

用分号;

于 2013-11-27T23:16:07.657 回答