我对Javascript很新,所以请原谅我缺乏知识。 我正在尝试使用Javascript来查找两个数据点(x1,y1)(x2,y2)的斜率。斜率等式为m = y1-y2/x2-x1。这里是我在JS小提琴中迄今为止所做的:http://jsfiddle.net/1wvfz0ku/ 问题是,当我尝试计算点时,只有NaN出现。 就像我说的,我在JS编码方面很新颖,所以我不能看到我在哪里搞乱了。任何帮助都将非常感谢! :-)为什么我的JS功能不能正常工作?
<!DOCTYPE html>
<html>
<body>
<div style="width:250px;height:auto;margin:auto;">
<h1>Find the Slope of Two Data Points</h1>
<p>
Find the slope of two sets of points:
</p>
<var>M</var>=<p id="slope"></p>
Enter your points below:
<form id="points">
X<sub>1</sub>: <input type="text" id="x1"/><br/>
X<sub>2</sub>: <input type="text" id="x2"/><br/>
Y<sub>1</sub>: <input type="text" id="y1"/><br/>
Y<sub>2</sub>: <input type="text" id="y2"/><br/>
</form>
<script>
function findSlope() {
//The points//
var xOne = document.getElementById("x1").value;
var xTwo = document.getElementById("x2").value;
var yOne = document.getElementById("y1").value;
var yTwo = document.getElementById("y2").value;
//slope equation//
var op1 = yTwo - yOne;
var op2 = xTwo - xOne;
var answer = op1/op2;
document.getElementById("slope").innerHTML = answer;
}
function reset(){
document.getElementById("points","slope").reset();
}
</script>
<button type="submit" onClick="findSlope()">Find the Slope</button>
<button type="submit" onClick="reset()">Clear Points</button>
</div>
</body>
</html>
对我来说工作正常:http://jsbin.com/mokoya/1/edit?html,console,output – Aweary 2015-02-06 22:32:33
你在测试什么浏览器? *不应该*重要,但这是真实的世界,如果您使用的是<在此处插入不良浏览器>,您将遇到问题 – Avery 2015-02-06 22:34:01
从2006年开始,我在MacBook Pro上使用旧版Chrome。为什么它不能正常工作... – 2015-02-09 16:45:49