Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript函数仅在禁用其他Javascript函数时工作_Javascript_Html - Fatal编程技术网

Javascript函数仅在禁用其他Javascript函数时工作

Javascript函数仅在禁用其他Javascript函数时工作,javascript,html,Javascript,Html,我制作了一个测试网站,用于测试javascript 我创建的所有3个函数都是独立工作的,但只有当其他函数被禁用时(当它们是注释时)。如果我没有对其他函数进行注释,那么所有函数的行为都很奇怪,无法正常工作(主题转换器不记得主题,加载页面时TopButton会显示在顶部,等等) 原因是您正在用另一个函数重写onload window.onload = 1; window.onload = 2; // 1 will not work anymore 改用.addEventListener windo

我制作了一个测试网站,用于测试javascript

我创建的所有3个函数都是独立工作的,但只有当其他函数被禁用时(当它们是注释时)。如果我没有对其他函数进行注释,那么所有函数的行为都很奇怪,无法正常工作(主题转换器不记得主题,加载页面时TopButton会显示在顶部,等等)


原因是您正在用另一个函数重写onload

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

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

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


如果不想为同一事件向同一对象添加多个侦听器,请使用或将这些函数声明为单独的命名函数,并在
window.onload
中调用它们,例如'window.onload=function(){LoadTheme();LoadTopButton();loadTopButtonLoad();}它现在可以与window.addEventListener一起使用。
//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");
}
window.onload = 1;
window.onload = 2; // 1 will not work anymore
window.addEventListener('load', function() {
});

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