gpt4 book ai didi

javascript - getelementbyid onclick 在调用函数后给出 undefined

转载 作者:行者123 更新时间:2023-11-30 08:48:57 26 4
gpt4 key购买 nike

我用 5 个按钮做了这个小测试(现在只考虑 4 个)。我没有太多使用 javascript 的经验,尝试使用 getelementbyid 和函数更改变量时,我唯一得到的是一个未定义的数字。我很确定按钮侧是正确的,但我不确定功能。

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

<style>
body {
overflow : hidden;
padding : 0;
margin : 0;
background-color: #BBB;

}
#AdvancedButton1
{
border: 1px #A9A9A9 solid;
background-color: #F0F0F0;
}
#AdvancedButton2
{
border: 1px #A9A9A9 solid;
background-color: #F0F0F0;
}
#AdvancedButton3
{
border: 1px #A9A9A9 solid;
background-color: #F0F0F0;
}
#AdvancedButton4
{
border: 1px #A9A9A9 solid;
background-color: #F0F0F0;
}
#AdvancedButton5
{
border: 1px #A9A9A9 solid;
background-color: #F0F0F0;
}
#info {
position : absolute;
top : 0px;
width : 100%;
padding : 5px;
text-align : center;
}
#info a {
color : #66F;
text-decoration : none;
}
</style>
</head>
<body>
<div id="info">
<span id="result"></span>
</div>
<button id="AdvancedButton1" type="button" name="" value="" style="position:absolute;left:896px;top:14px;width:102px;height:138px;z-index:1;" >
<div style="text-align:center"><span style="color:#FF0000;font-family:Arial;font-size:19px"><b>UP</b></span></div>
</button>
<button id="AdvancedButton2" type="button" name="" value="" style="position:absolute;left:896px;top:327px;width:102px;height:138px;z-index:2;">
<div style="text-align:center"><span style="color:#FF0000;font-family:Arial;font-size:16px"><b>DOWN</b></span></div>
</button>
<button id="AdvancedButton3" type="button" name="" value="" style="position:absolute;left:720px;top:181px;width:138px;height:102px;z-index:3;">
<div style="text-align:center"><span style="color:#FF0000;font-family:Arial;font-size:19px"><b>LEFT</b></span></div>
</button>
<button id="AdvancedButton4" type="button" name="" value="" style="position:absolute;left:1028px;top:181px;width:138px;height:102px;z-index:4;">
<div style="text-align:center"><span style="color:#FF0000;font-family:Arial;font-size:19px"><b>RIGHT</b></span></div>
</button>
<button id="AdvancedButton5" type="button" name="" value="" style="position:absolute;left:896px;top:181px;width:102px;height:105px;z-index:5;">
<div style="text-align:center"><span style="color:#DC143C;font-family:'Arial Black';font-size:27px">FIRE</span></div>
</button>
<script>

var outputEl = document.getElementById('result');
var arrow;

document.getElementById("AdvancedButton1").onclick = function(){
funarr('1');
}
document.getElementById("AdvancedButton2").onclick = function(){
funarr('2');
}
document.getElementById("AdvancedButton3").onclick = function(){
funarr('3');
}
document.getElementById("AdvancedButton4").onclick = function(){
funarr('4');
}


function funarr(value){

arrow = value ;
}

outputEl.innerHTML = '<b>Result:</b> '
+ arrow ;

</script>
</body>
</html>

我做错了什么?最后有标签,但是帖子格式不正确(我不知道为什么)并且标签被隐藏了

最佳答案

而 funar 应该是:

function funarr(value){
arrow = value ;
outputEl.innerHTML = '<b>Result:</b> '
+ arrow ;
}

事实上你不需要全局arrow变量

关于javascript - getelementbyid onclick 在调用函数后给出 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553214/

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