0
<!DOCTYPE html>
<html>
<head>
<title>Home Lab 4</title>
 <head>

 </head>
 <body>

 <table border="4px" bordercolor="navy" bgcolor="white" cellpadding="4px">

 <tr>
 <td>X-coordinate 1:</td> 
 <td><input type="text" id="x1"></td>
 <td>X-coordinate 2:</td>
 <td><input type="text" id="x1"></td>
 </tr>
 <tr>
 <td>Y-coordinate 1:</td>
 <td><input type="text" id="x1"></td>
 <td>Y-coordinate 2:</td>
 <td><input type="text" id="x1"></td>

 </center>
 </table>  

<script type="text/javascript">
function distance()
{
distance=sqrt( (x2 - x1)^2 + (y2 - y2)^2 )

var a = [564,426];
var b = [56,784];

var xN = Math.pow( b[0] - a[0], 2 );
var yN = Math.pow( b[1] - a[1], 2 );

var distance = Math.sqrt(xN + yN);


alert(distance);
}

</script>

<br>
 <input type="button" onclick="distance()" value="distance">





</body>
</html>

我不确定问题是什么,但我无法得到要计算的值。我还需要弹出警报以显示计算值的帮助。我试过无数次改变不同的东西,一切都保持不变。任何帮助都会非常好。提前致谢!

4

1 回答 1

0

我想你可能想写这样的东西:

<!DOCTYPE html>
<html>
<head>
<title>Home Lab 4</title>
</head>
 <body>

 <table border="4px" bordercolor="navy" bgcolor="white" cellpadding="4px">

 <tr>
 <td>X-coordinate 1:</td> 
 <td><input type="text" id="x1"/></td>
 <td>X-coordinate 2:</td>
 <td><input type="text" id="x2"/></td>
 </tr>
 <tr>
 <td>Y-coordinate 1:</td>
 <td><input type="text" id="y1"/></td>
 <td>Y-coordinate 2:</td>
 <td><input type="text" id="y2"/></td>
 </tr>
 </table>  

<script type="text/javascript">
function distance()
{
    var x1 = parseFloat(document.getElementById("x1").value);
    var x2 = parseFloat(document.getElementById("x2").value);
    var y1 = parseFloat(document.getElementById("y1").value);
    var y2 = parseFloat(document.getElementById("y2").value);
    var distance = Math.sqrt( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) );

    var a = [564,426];
    var b = [56,784];

    var xN = Math.pow( b[0] - a[0], 2 );
    var yN = Math.pow( b[1] - a[1], 2 );

    distance = Math.sqrt(xN + yN);

    alert(distance);
}

</script>

<br/>
 <input type="button" onclick="distance()" value="distance" />

</body>
</html>

但是,无论如何,您的代码没有多大意义,因为您正在计算distance并覆盖它在alert...之前的行中的值

于 2013-03-08T22:07:14.127 回答