首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为多项设置localStorage

为多项设置localStorage
EN

Stack Overflow用户
提问于 2022-03-26 04:59:19
回答 1查看 54关注 0票数 1

我在设置本地存储以保存每个时间块的textArea输入时遇到了问题。我已经评论了三个不同的部分,我在其中发挥不同的技术,试图使它的工作。但是,我无法将其设置为将文本保存在本地存储中,并使其在您重新访问时保留在页面上。如果你能帮我解决这个问题,请告诉我。我提供了一个指向我的存储库的链接。请查看功能/日历分支,因为这是我的代码所在。我还将代码片段插入这里,供您查看。我对编码很陌生,所以请对我有耐心。我仍在学习术语和实现目标/目标的不同方法。

代码语言:javascript
运行
复制
saveBtn = document.getElementsByClassName('saveBtn');

//  creates a new instance for time and date.
const currentDay = luxon.DateTime.now().toLocaleString(luxon.DateTime.DATETIME_MED);
//  format's the date and time.
var newFormat = {
    //  The selected date format from luxon documentation.
    ...luxon.DateTime.DATETIME_MED,
    //  added the day of the week.
    weekday: 'long'
};

// creates a new instance for time and date.
const newCurrentDay = luxon.DateTime.now().toLocaleString(newFormat);
//  checks if time works.


//  creates a new instance to parse the date and time into the header html.
var ol = document.querySelector('header');
//  creates the ol element.
const list = document.createElement('ol');
//  styles the <ol> element.
list.setAttribute('style', 'color: white; background-color: black; display: flex; flex-direction: column; justify-content: center; align-items: center;');
//  displays newCurrentDay variable in the header as readable text.
list.innerText = newCurrentDay;
//  appends the newCurrentDay variable to the ol element.
ol.appendChild(list);

//  creates an array to change the blocks bases on their time in the text area element.
const timeBlocks = Array.from(document.getElementsByClassName('description'));
// assigning timeBlocks based on time.
for (var i = 0; i < timeBlocks.length; i++) {
    //  gets the current hour to connect the color changing feature in the text area element based on the 24hr format.
    const now = luxon.DateTime.now().hour.toLocaleString();
    //  if statement's to change the color of the blocks based on time using the 24hr time format.
    if (timeBlocks[i].id < now) {
        timeBlocks[i].classList.add('past');
    }
    if (timeBlocks[i].id > now) {
        timeBlocks[i].classList.add('future');
    }
    if (timeBlocks[i].id === now) {
        timeBlocks[i].classList.add('present');
    }
}


// // * testing
// saveBtn = document.getElementsByClassName('saveBtn');

// var save = addEventListener('click', function () {
//     //  creates a new instance to parse the date and time into the header html. 
//     var textarea = document.querySelector('textarea');

//     this.window.localStorage.setItem('textarea', textarea.value);

//     this.window.localStorage.getItem('textarea');
// });




//  TODO: can  use save button for event text to persist even when refreshed.

// $('.saveBtn').on("click", function () {
//     window.localStorage.setItem('task', $(this).siblings('.description').val());
// })

// loadTask = function () {
//     return window.localStorage.getItem('task');
// }


// let taskArray = [{
//     21: "",
//     22: "",
//     23: "",
// }];

// $('.saveBtn').on("click", function () {
//     window.localStorage.setItem('tasks', JSON.stringify(taskArray));

//     JSON.parse(window.localStorage.getItem('tasks'));

// var storedData = window.localStorage.getItem(taskArray);

// if (storedData) {
//     taskArray = JSON.parse(storedData);
//     alert(taskArray);
// }
// });


// TODO:  event is saved into time block using local storage


// TODO:  have my minutes actively change. 
代码语言:javascript
运行
复制
body {
  font-family: 'Open Sans', sans-serif;
  font-size: 16px;
  line-height: 1;
}

textarea {
  background: transparent;
  border: none;
  resize: none;
  color: #000000;
  border-left: 1px solid black;
  padding: 10px;
}

.jumbotron {
  text-align: center;
  background-color: transparent;
  color: black;
  border-radius: 0;
  border-bottom: 10px solid black;
}

.description {
  white-space: pre-wrap;
}

.time-block {
  text-align: center;
  border-radius: 15px;
}

.row {
  white-space: pre-wrap;
  height: 80px;
  border-top: 1px solid white;
  ;
}

.hour {
  background-color: #ffffff;
  color: #000000;
  border-top: 1px dashed #000000;
}

.past {
  background-color: #d3d3d3;
  color: white;
}

.present {
  background-color: #ff6961;
  color: white;
}

.future {
  background-color: #77dd77;
  color: white;
}

.saveBtn {
  border-left: 1px solid black;
  border-top-right-radius: 15px;
  border-bottom-right-radius: 15px;
  background-color: #06AED5;
  color: white;
}

.saveBtn i:hover {
  font-size: 20px;
  transition: all .3s ease-in-out;
}
代码语言:javascript
运行
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
    integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous" />
  <link href="https://fonts.googleapis.com/css?family=Open+Sans&display=swap" rel="stylesheet" />
  <link rel="stylesheet" href="./assets/css/style.css" />
  <title>Work Day Scheduler</title>
</head>

<body>
  <header class="jumbotron">
    <h1 class="display-3">Work Day Scheduler</h1>
    <p class="lead">A simple calendar app for scheduling your work day</p>
    <p id="currentDay" class="lead"></p>
  </header>
  <div class="container">
    <!-- Time blocks go here -->
    <div class="row">
      <div class="col-2 border-top border-dark">
        9:00PM
      </div>
      <textarea class="description col-8" id="21">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        10:00PM
      </div>
      <textarea class="description col-8" id="22">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        11:00PM
      </div>
      <textarea class="description col-8" id="23">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        12:00AM
      </div>
      <textarea class="description col-8" id="0">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <!-- <div class="row">
      <div class="col-2 border-top border-dark">
        1:00PM
      </div>
      <textarea class="description col-8" id="13">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        2:00PM
      </div>
      <textarea class="description col-8" id="14">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        3:00PM
      </div>
      <textarea class="description col-8" id="15">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        4:00PM
      </div>
      <textarea class="description col-8" id="16">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div>
    <div class="row">
      <div class="col-2 border-top border-dark">
        5:00PM
      </div>
      <textarea class="description col-8" id="17">Event</textarea>
      <button class="btn saveBtn col-2"><i class="fas fa-save"></i></button>
    </div> -->


    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> -->
    <script src="https://cdn.jsdelivr.net/npm/luxon@2.3.1/build/global/luxon.min.js"></script>
    <script src="/assets/javascript/script.js"></script>
</body>

</html>

EN

回答 1

Stack Overflow用户

发布于 2022-03-26 05:12:55

我不遵循您期望执行的这段代码中的哪一段,因此我将对每个代码的问题进行评论。

代码语言:javascript
运行
复制
this.window.localStorage.getItem('textarea');

这句话没什么用。你得到了价值,但不在任何地方使用。

代码语言:javascript
运行
复制
loadTask = function () {
    return window.localStorage.getItem('task');
}

没有调用此函数,因此它也不会执行任何操作。

代码语言:javascript
运行
复制
JSON.parse(window.localStorage.getItem('tasks'));

同样,在这里,您没有使用返回值来做任何事情,所以它不会有任何结果。

然而,

无法将其设置为将文本保存在本地存储中,并使其在您重新访问时保留在页面上。

在触发localStorage时,需要从DOMContentLoaded中加载数据。首先使用浏览器的开发工具确保您想要的数据被保存。然后,在加载页面时,获取数据并按您认为合适的方式填充表单。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71625432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档