我做了一個配置頁面,當我按下按鈕時,它會更改背景、顏色或影像,我創建了 localStorages img 和顏色,我的問題是當它們同時不為空時,而不是背景用于影像它用于使用“顏色”制作背景,我正在嘗試的代碼是這樣的:
if (localStorage.getItem("Color") === null) { //if Color is null
if (localStorage.getItem("img") === null) {// And Image too
document.body.style.backgroundImage = "url('css/Images/Instruments/pexels-pixabay-459797.jpg')"; // a image appear in the background
} else { // else if Color is null but img isint
document.body.style.backgroundImage = localStorage.getItem("img"); //load the image in the background
}
// THIS IS My problem: i dont know how to check if both arent null
if(localStorage.getItem("Color") !== 'null' localStorage.getItem("img") !== 'null'){ //If Both arent null
document.body.style.backgroundImage = localStorage.getItem("img"); // load the Image not the background
}
}};
uj5u.com熱心網友回復:
只需使用“localStorage.removeItem('item')”洗掉另一個本地存盤專案(仍在測驗中)
uj5u.com熱心網友回復:
您可以使用邏輯運算子 &&,如下所示:
if (!localStorage.getItem('color') {
if (!localStorage.getItem('img') {
document.body.style.backgroundImage = 'url("your image url")';
} else {
document.body.style.backgroundImage = localStorage.getItem('img');
}
if (localStorage.getItem('color') && localStorage.getItem('img') {
document.body.style.backgroundImage = localStorage.getItem('img');
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/361712.html
