【问题标题】:How to store output in localStorage?如何将输出存储在localStorage中?
【发布时间】:2022-01-16 18:32:16
【问题描述】:

我正在处理我的第一个 JavaScript 项目(回文检查器),但我有点卡住了。我已经了解了localStorage(),但我似乎无法在我自己的项目中实现它。如果有人能指出我正确的方向,或者编写伪代码来说明我应该怎么做才能让它工作,那就太棒了。我真的很想自己解决它,但是这里非常需要一点帮助。谢谢 :)。 我的 JavaScript 代码(以及 HTML 和 CSS 供参考:

编辑:我想在屏幕上使用localStorage() 显示结果(每个都在单独的行上),并再次使用它使用户能够在单击按钮时删除结果。

const checkBtn = document.getElementById("check-word");
const clearBtn = document.getElementById("clear-history");
const outputField = document.getElementById("word-checked");
let array = [];

checkBtn.addEventListener("click", () => {
    const str = document.getElementById("input").value;
    array = [...array, str];
    
    console.log(array);
    palindrome(str);
    renderResult();
    
    function palindrome(str) {
        const lettersOnly = str.toLowerCase().match(/[a-z0-9]/g);
        return lettersOnly.join("") === lettersOnly.reverse().join("");
    }
    
    renderResult();
    
    function renderResult() {
        if (palindrome(str) === true) {
            outputMessage = `︎✔︎ '${str}' is a palindrome!`
        } else {
            outputMessage = `???? '${str}' is not a palindrome!`
        }
        outputField.textContent = outputMessage;
    }
    
    document.getElementById("input").value = ""; // clear input field after clicking on checkBtn
})

// clearBtn.addEventListener("click", () => {
//     localStorage.clear();
//     array = [];
//     // render again!
// })
* {
    background-color: #121212;
    font-size: 16px;
    margin: 0;
    padding: 0;
}

h1 {
    font-family: "VT323", monospace;
    font-size: 5rem;
    color: #CE1212;
    text-align: center;
    margin-top: 50px;
}

.container {
    font-family: "Nanum Gothic Coding", monospace;
    color: white;
    width: 75%;
    height: 62.5vh;
    margin: 25px auto 25px auto;

    /* border: 2.5px solid; */
    border-color: white;
    padding: 15px;
}

.input {
    margin-left: 25px;
}

.input-field {
    margin-left: 25px;
    margin-top: 12.5px;
    padding: 7.5px;
    font-family: "Nanum Gothic Coding", monospace;
    color: white;
    border: 1px solid;
}

.input-field:focus::placeholder {
    color: transparent;
}

.check-word, .clear-history {
    padding: 7.5px;
    font-family: "Nanum Gothic Coding", monospace;
    color: white;
    border: 1px solid;
    border-color: white;
    cursor: pointer;
}

.check-word:hover, .clear-history:hover {
    color: #CE1212;
}

.child-1 {
    margin-top: 50px;
    margin-left: 25px;
    /* border: 1px solid; */
    padding: 15px;
}

#word-checked {
    margin-top: 15px;
}

.footer {
    font-family: "Nanum Gothic coding", monospace;
    color: white;
    text-align: center;
    margin-bottom: 25px;
}

a:link, a:visited {
    color: white;
    text-decoration: none;
}

a:hover, a:active {
    color: #CE1212;
    text-decoration: underline;
}








/* 
top, right, bottom, left
*/
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Palindrome Checker</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Nanum+Gothic+Coding&family=VT323&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <main>
        <h1>???? Palindrome Checker ????</h1>
        <div class="container">
            <label for="input" class="input">Type a word or phrase</label>
            <br>
            <input type="text" name="input" class="input-field" id="input" placeholder="Type something...">
            <button type="button" class="check-word" id="check-word">Check word</button>

            <button type="button" class="clear-history" id="clear-history">Clear history</button>

            <div class="child-1">
                <p id="word-checked">Results will be shown here!</p>
            </div>
        </div>
    </main>
    
    <footer class="footer">
        <p>Built by <a href="https://github.com/YinChuRijnaard" target="_blank">Yin Chu</a></p>
    </footer>

    <script src="index.js"></script>

</body>
</html>

【问题讨论】:

标签: javascript local-storage


【解决方案1】:

这真的很容易。 (至少比 cookie 容易得多)

localStorage.setItem('key', 'value'); // to store data
var myData = localStorage.getItem('key') // to get data
localStorage.removeItem('key') // to remove one data
localStorage.clear() // to remove all data

keyvalue分别替换为数据键和要存储的数据。

【讨论】:

  • 感谢您的回复。这很有帮助。然而,就像我上面提到的,我根本不知道我必须把代码放在哪里才能让它真正工作。这就是我正在努力解决的问题:')
  • @YinChu 放在按钮的click 事件监听器中即可。但是……你想在哪里显示本地存储数据?
  • 我想在屏幕上显示输出。就像,您单击一个按钮,输出显示在屏幕上。但是,当您再次单击该按钮时,先前的输出将被新输出替换。 (感谢您的帮助!)
  • @YinChu “输出”是什么?是本地存储的数据吗?
  • 见:imgur.com/a/KlFjLA8。 'teset' 正确显示为回文,但是当我键入另一个条目时,当前条目被替换。我实际上希望新条目显示在当前条目下方。很抱歉我的问题不清楚!
猜你喜欢
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 2011-03-06
相关资源
最近更新 更多