【发布时间】:2019-09-20 18:30:15
【问题描述】:
我制作了一个测试网站,用于试验 javascript。
我创建的所有 3 个函数都独立工作,但只有在其他函数被禁用时(当它们是 cmets 时)。如果我不评论其他功能,所有功能都会表现得很奇怪并且无法正常工作(主题转换器不记得主题,页面加载时 TopButton 会显示在顶部等)。
//HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>K E K</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="exde.js"></script>
</head>
<body>
<button onclick="ChangeTheme()">Change Theme</button>
<button onclick="ChangeTopButton()">Change Top-Button</button>
<button onclick="ToggleTopButton()">Toggle Top-Button</button>
<button id="topbutton" onclick="TopButton()">Go Top</button>
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, ...</p>
</body>
</html>
//CSS
body {
background-color: #CFF;
}
button#topbutton {
position: fixed;
}
.displaynone {
display: none;
}
//JS
// Change Theme
window.onload = function LoadTheme() {
if (localStorage.theme) {
if (localStorage.theme == "dark") {
SetThemeDark();
}
}
else {
localStorage.setItem("theme","light");
}
}
function ChangeTheme() {
if (localStorage.theme == "dark") {
SetThemeLight();
localStorage.theme = "light";
}
else {
SetThemeDark();
localStorage.theme = "dark";
}
}
function SetThemeDark() {
let a = document.getElementsByTagName('body')[0];
a.style.backgroundColor = '#444';
}
function SetThemeLight() {
let a = document.getElementsByTagName('body')[0];
a.style.backgroundColor = '#CFF';
}
// Change Top Button
window.onload = function LoadTopButton() {
if (localStorage.topbutton) {
switch(localStorage.topbutton) {
case "bottomright":
SetBottomRight();
break;
case "bottomleft":
SetBottomLeft();
break;
case "topleft":
SetTopLeft();
break;
case "topright":
SetTopRight();
break;
default:
SetBottomRight();
}
}
else {
localStorage.setItem("topbutton","bottomright");
SetBottomRight();
}
}
function ChangeTopButton() {
switch(localStorage.topbutton) {
case "bottomright":
SetBottomLeft();
localStorage.topbutton = "bottomleft";
break;
case "bottomleft":
SetTopLeft();
localStorage.topbutton = "topleft";
break;
case "topleft":
SetTopRight();
localStorage.topbutton = "topright";
break;
case "topright":
SetBottomRight();
localStorage.topbutton = "bottomright";
break;
default:
SetBottomRight();
localStorage.topbutton = "bottomright";
}
}
function TopButton() {
document.documentElement.scrollTop = 0;
}
function SetBottomLeft() {
let a = document.getElementById("topbutton");
a.style.right = "auto";
a.style.top = "auto";
a.style.left = "5px";
a.style.bottom = "5px";
}
function SetBottomRight() {
let a = document.getElementById("topbutton");
a.style.left = "auto";
a.style.top = "auto";
a.style.right = "5px";
a.style.bottom = "5px";
}
function SetTopLeft() {
let a = document.getElementById("topbutton");
a.style.right = "auto";
a.style.bottom = "auto";
a.style.left = "5px";
a.style.top = "5px";
}
function SetTopRight() {
let a = document.getElementById("topbutton");
a.style.left = "auto";
a.style.bottom = "auto";
a.style.right = "5px";
a.style.top = "5px";
}
// Toggle Top Button
window.onload = function LoadTopButtonOnLoad() {
if (localStorage.displayButton) {
if (localStorage.displayButton == "no") {
DeleteTopButton();
}
}
else {
localStorage.setItem("displayButton","yes");
}
}
function ToggleTopButton() {
if (localStorage.displayButton == "yes") {
DeleteTopButton();
localStorage.displayButton = "no";
}
else {
ShowTopButton();
localStorage.displayButton = "yes";
}
}
function DeleteTopButton() {
let a = document.getElementById("topbutton");
a.classList.add("displaynone");
}
function ShowTopButton() {
let a = document.getElementById("topbutton");
a.classList.remove("displaynone");
}
【问题讨论】:
-
如果你不想为同一个事件向同一个对象添加多个监听器,请使用
.addEventListener -
或者将函数声明为单独的命名函数并在
window.onload中调用它们,例如`window.onload = function () { LoadTheme();加载顶部按钮(); LoadTopButtonOnLoad(); } -
它现在可以与 window.addEventListener 一起使用。
标签: javascript html