<!DOCTYPE html>
<html>
<head>
<title>Ambiente Web</title>
<script type="text/javascript">
function fibo(num) {
var f = [];
for (var c = 0; c < num; c++) {
f.push((c < 2) ? c : f[c-1] + f[c-2]);
}
return f;
}
var aux=document.getElementById('Largo').value;
document.getElementById("textarea").innerHTML = fibo(aux);
</script>
</head>
<body>
<input type="text" value="" id="Largo" name="largo del arreglo"></input>
<input type="button" value="Calcular" onclick="fibo();"></input>
<textarea id="textarea" readonly rows="5" cols="10"></textarea>
</body>
</html>所以问题是,我正在计算斐波那契数列,函数本身运行完美,但我希望有人将斐波那契数列的长度放在输入文本“type=”中,并将其传递给fibo参数,然后结果将其发送到文本区域,但它应该可以工作,而我不知道会发生什么。我也不知道如何让脚本只在点击“计算器”按钮时运行。
发布于 2018-03-28 04:22:48
将您的代码放在一个函数中,然后从onclick调用该函数
<!DOCTYPE html>
<html>
<head>
<title>Ambiente Web</title>
<script type="text/javascript">
function fibo(num){
var f = [];
for (var c = 0; c < num; c++) {
f.push((c < 2) ? c : f[c-1] + f[c-2]);
}
return f;
}
function go(){
var aux=document.getElementById('Largo').value;
document.getElementById("textarea").innerHTML = fibo(aux);
}
</script>
</head>
<body>
<input type="text" value="" id="Largo" name="largo del arreglo"></input>
<input type="button" value="Calcular" onclick="go();"></input>
<textarea id="textarea" readonly rows="5" cols="10"></textarea>
</body>
</html>你可能还想读一读这个问题:addEventListener vs onclick,它展示了与这个内联版本相比使用更多的其他方式。
发布于 2018-03-28 04:42:02
Pass the value of the DOM element in fibo function in following way:
<!DOCTYPE html>
<html>
<head>
<title>Ambiente Web</title>
<script type="text/javascript">
function fibo(num){
var f = [];
for (var c = 0; c < num; c++) {
f.push((c < 2) ? c : f[c-1] + f[c-2]);
}
document.getElementById("textarea").innerHTML = f;
}
</script>
</head>
<body>
<input type="text" value="" id="Largo" name="largo del arreglo"></input>
<input type="button" value="Calcular"
onclick="fibo(document.getElementById('Largo').value);"></input>
<textarea id="textarea" readonly rows="5" cols="10"></textarea>
</body>
</html>https://stackoverflow.com/questions/49521771
复制相似问题