我需要用三个数字连接输入当我写a = 1 b = -3且c = -4并且当我按下按钮时 - >有解决方案
function solve(a, b, c) {
let x1 = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
let x2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
return "X1= " + x1 + ", X2= " + x2;
}
console.log(solve(1, -3, -4));
答案 0 :(得分:0)
我不完全理解您的问题,但我猜您希望按下按钮时执行solve()
功能。以下是三个选项:
window.onload = function() {
var button = document.getElementById("button");
// Method 1
button.addEventListener("click", function() {
alert("clicked1");
solve(a, b, c);
// ^ Insert Params ^
});
// Method 2
button.onclick = function() {
alert("clicked2");
solve(a, b, c);
// ^ Insert Params ^
};
};

#button {
width: 100px;
height: 100px;
background-color: green;
}

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="button"> <p>This is a button</p></div>
</body>
</html>
&#13;
我希望这能解决你的问题。