gpt4 book ai didi

Javascript 函数仅在其他 javascript 函数被禁用时才起作用

转载 作者:行者123 更新时间:2023-12-02 22:54:53 25 4
gpt4 key购买 nike

我制作了一个测试网站来试验 JavaScript。

我创建的所有 3 个函数都是独立工作的,但只有当其他函数被禁用时(当它们是注释时)。如果我不评论其他功能,所有功能都会表现得很奇怪并且无法正常工作(主题更换器不记得主题,加载页面时顶部按钮显示在顶部等)。

//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");
}

最佳答案

原因是您正在使用其他函数覆盖 onload。

window.onload = 1;
window.onload = 2; // 1 will not work anymore

改用.addEventListener。

window.addEventListener('load', function() {
});

// instead of
window.load = function () {
}

关于Javascript 函数仅在其他 javascript 函数被禁用时才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58033554/

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