通过向前或向后滚动,我希望从数组中检索相关信息。为什么“页面”的值在document.getElementById("car").innerHTML =(Carspage)中无效;
<!DOCTYPE html>
<html lang='sv-se'>
<head>
<title>Page Scroll</title>
<meta charset='utf-8'>
<script>
const cars = [ [ 'Ford' ],[ 'Rolls Royce' ],[ 'Bentley' ],[ 'Volvo' ],];
</script>
<script>var page = 0;let length = cars.length;</script>
</head>
<body style='text-align:center;'>
<h1 id='page'>0</h1>
<h2 id='car'>Car</h2>
<button type='button' onclick='prevPage()'> PREV </button>
Scroll with the arrows
<button type='button' onclick='newPage()'> NEXT </button>
<p>The length of the data file: <a id='len'>0</a></p>
<script>
function newPage() {
if (page > length - 2) {
page = 0;
} else {
page += 1;
}
document.getElementById('page').innerHTML = page;
};
function prevPage() {
if (page < 1) {
page = length - 1;
} else {
page -= 1;
}
document.getElementById('page').innerHTML = page;
};
</script>
<script>
document.getElementById('car').innerHTML = (cars[page][0]);
document.getElementById('len').innerHTML = length;
</script>
</body></html>发布于 2022-03-08 13:20:52
document.getElementById('car').innerHTML = (cars[page][0]);只运行一次,在页面加载开始时。将其添加到nextPage和prevPage中的代码中
https://stackoverflow.com/questions/71395712
复制相似问题