gpt4 book ai didi

JavaScript onclick 事件处理程序无法正常运行

转载 作者:行者123 更新时间:2023-11-28 14:03:06 25 4
gpt4 key购买 nike

由于某种原因,我的 onclick JavaScript 事件处理程序无法正常运行。

这是我的标记、脚本和样式:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>

<script>
document.ready = function() {
document.getElementById('calculate').onClick.calculateQuad()
{
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;

root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/2*inputa
root2 = (-inputb + Math.sqrt(root))/2*inputa

document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
if(root<'0')
{
alert('This equation has no real solution.')
}
else {
if(root=='0')
{
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
};
document.getElementById('erase').onClick.document.getElementById('form1').reset();
}
</script>

<style>
#container
{
text-align: center;
}
</style>

</head>

<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
<input id="calculate" value="Calculate!" type="button">
<input id="erase" value="Clear" type="button">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>

有可以解释的原因吗?

最佳答案

这是错误的
document.getElementById('calculate').onClick.calculateQuad()
一定是
document.getElementById('计算').onClick = function ()

这也是错误的
document.getElementById('erase').onClick.document.getElementById('form1').reset();
修复:
document.getElementById('erase').onClick = function(){document.getElementById('form1').reset();}

关于JavaScript onclick 事件处理程序无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3911415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com