var divs = ["Page1", "Page2", "Page3"];
var visibleDivId = null;
var i, divID, div;
function divVisibility(divID) {
if (visibleDivId == divID) {
visibleDivId = null;
} else {
visibleDivId = divID;
}
hideNonVisibleDivId();
}
function hideNonVisibleDivId() {
for (i = 0; i < divs.length; i++) {
divID = divs[i];
div = document.getElementById(divID);
if (visibleDivId === divID) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
}
<HTML>
<Head>
</Head>
<Body>
<Center>
<table style="border-radius:15px 15px 15px 15px;">
<tr>
<td>
<center>
<div style="width:850px; border:3px black solid; margin-top:30px; margin-bottom:-10px; border-radius:8px 8px 8px 8px; background-image: url('https://basilthesheep.neocities.org/SiteImages/color2.png')">
<!-- toolbar -->
<table style="border-top: none; height:0px;">
<tr>
<td>
<center>
<div class="toolbar" style="border-radius:15px 15px 15px 15px;">
<font size="3"><b>~ /
<a href="#" onClick="divVisibility('Page1')"> First </a> /
<a href="#" onClick="backward(?????);"> Back </a> /
<a href="#" onClick="forward(??????);"> Next </a> /
<a href="#" onClick="divVisibility('Page3');"> Latest </a> / ~</b></font>
</div>
</center>
</td>
</tr>
</table>
<!-- diary -->
<table style="border-radius:15px 15px 15px 15px;">
<tr>
<td>
<center>
<div id="diary">
<div id="Page1">
egg
</div>
<div id="Page2" style="display: none;">
egg bacon
</div>
<div id="Page3" style="display: none;">
egg bacon cheese
</div>
</div>
</center>
</td>
</tr>
</table>
</div>
</center>
</td>
</tr>
</table>
</center>
</body>
</html>
我试图这样做: 首先总是转到第一个Div(page1) 接下来将转到第2页3 4等。 返回将返回第3 2 1页。 最新将转到最后一个Div(当前第3页)
看它可以多么简单:
window.onload = () => {
const pages = document.querySelectorAll("body > section");
const buttons = document.querySelectorAll("body > button");
const back = buttons[0];
const forward = buttons[1];
let current = 0;
const updateEnabled = () => {
if (current == 0) {
back.disabled = true;
forward.disabled = false;
} else if (current >= pages.length - 1) {
back.disabled = false;
forward.disabled = true;
} else {
back.disabled = false;
forward.disabled = false;
}
}; //updateEnabled
updateEnabled();
const move = backward => {
pages[current].style.display = "none";
if (backward) current--; else current++;
pages[current].style.display = "block";
updateEnabled();
} //move
back.onclick = () => move(true);
forward.onclick = () => move(false);
};
* { font-family: sans-serif; }
body > section { display: none; }
body > section:first-of-type { display: block; }
button { font-size: 160%; }
<!DOCTYPE html>
<html>
<body>
<head><button>⇦</button><button>⇨</button></head>
<section>Page 1</section>
<section>Page 2</section>
<section>Page 3</section>
<section>Page 4</section>
</body>
</html>
关注这些时刻:
document.getElementById
使用
document.querySelectorAll
id
属性并立即采取整个元素。
- 请注意,您将一组页面作为数组。这样,您就不需要第一个和最后一个索引,从数组中知道。
const
和
let
。几乎一切都是
const
属性
.disabled
按钮上的属性使您免于多余的支票。