gpt4 book ai didi

javascript - 错误 : updating values of elevator and user after clicking button

转载 作者:行者123 更新时间:2023-11-30 20:17:51 25 4
gpt4 key购买 nike

我正在创建一个电梯程序,其中包括电梯内部的向上、向下按钮(外部功能)和按钮(0 到 5::内部功能)我的外部功能正常工作,但是当在电梯内用户按下电梯中的任何按钮时应该去那个地方。并更新用户和电梯的位置。但是当我按下按钮(0,1,2,3,4,5)时,它不起作用。谁能帮我解决这个问题。提前致谢。

code::

<!DOCTYPE html>
<html lang="en">

<head>
<title>elevator</title>
</head>

<body>
<h2>Internal Functions</h2>
<span>current user position :: </span>
<p id="userPosition"></p>
<span>current elevator position :: </span>
<p id="elevatorPostion"></p>

<button id="btn" onclick="inFunction(0)" value="0">0</button>
<button id="btn" onclick="inFunction(1)" value="1">1</button>
<button id="btn" onclick="inFunction(2)" value="2">2</button>
<button id="btn" onclick="inFunction(3)" value="3">3</button>
<button id="btn" onclick="inFunction(4)" value="4">4</button>
<button id="btn" onclick="inFunction(5)" value="5">5</button>

<h2>External Functions</h2>
user position:
<input type="text" id="userText" name="UserPosition" value="0">

<button onclick="exFunction('up')">UP</button>
<button onclick="exFunction('down')">DOWN</button>
<script>
var userPosition = 0,
elevatorPosition = 4

function exFunction(pressedValue) {
console.log(pressedValue);
var userPosition = document.getElementById("userText").value;
document.getElementById("userPosition").innerHTML = userPosition;
document.getElementById("elevatorPostion").innerHTML = elevatorPosition;

var intervalEle = setInterval(function () {

if (userPosition > elevatorPosition) {
console.log('Elevator position', elevatorPosition);
elevatorPosition++;
} else if (userPosition < elevatorPosition) {
elevatorPosition--;
} else {
elevatorPosition == userPosition;
clearInterval(intervalEle);
}

document.getElementById("userPosition").innerHTML = userPosition;
document.getElementById("elevatorPostion").innerHTML = elevatorPosition;

}, 1000);
}

function inFunction(clickedValue) {
var btnValue = document.querySelector('btn').value;
document.getElementById("userPosition").innerHTML = userPosition;
document.getElementById("elevatorPostion").innerHTML = elevatorPosition;

console.log(btnValue);

var intervalEle2 = setInterval(function () {
if (elevatorPosition > btnValue) {
elevatorPosition--
} else if (elevatorPosition < btnValue) {
elevatorPosition++
} else {
elevatorPosition == btnValue;
clearInterval(intervalEle2)
}

document.getElementById("userPosition").innerHTML = userPosition;
document.getElementById("elevatorPostion").innerHTML = elevatorPosition;
}, 1000)

}
</script>
</body>

</html>

输出: /image/tKeeL.png

最佳答案

var btnValue = document.querySelector('btn').value;

没有 <btn>你页面上的元素,所以document.querySelector('btn')返回 null .你非法重复使用了 "btn"作为 id,所以 document.getElementById('btn')也不会做您期望的事情。

您已经获得了被点击按钮的编号;你把它传给了inFunction作为clickedValue .就用那个吧。

关于javascript - 错误 : updating values of elevator and user after clicking button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51735827/

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