如何将输出存储在 localStorage 中?

How to store output in localStorage?

我正在做我的第一个 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>

真的很简单。 (至少比cookies简单多了)

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分别替换为数据键和要存储的数据。