【问题标题】:Is there any way to save HTML element class changes made by Jquery in a generated EJS file?有什么方法可以将 Jquery 所做的 HTML 元素类更改保存在生成​​的 EJS 文件中?
【发布时间】:2018-03-04 14:43:58
【问题描述】:

我有一个待办事项列表节点项目,其中每个待办事项列表都保存为 MongoDB 中的一个实体,如下所示:

var todoSchema = new mongoose.Schema({
  items: [String]
});

当用户单击待办事项列表中的一项时,它会通过更改所单击元素的 CSS 类将其删除。当用户刷新时,类会改回 EJS 文件的呈现方式。

我希望划掉是本地的,即只有划掉项目的人才能看到它被划掉。有什么方法可以不涉及为每个用户设置单独的数据库条目吗?

前条目:

{
    "_id": {
        "$oid": "59bf2fed71c3840508539b29"
    },
    "item": [
        "ayy",
        "yyaa",
        "yyaaaafyyy"
    ],
    "__v": 0
}

【问题讨论】:

  • 除了:['Item 1', 'Item 2', 'Item 3'] 之外,您的数据库中的项目是否可以带有 ID 或其他任何内容?
  • 向我展示一个示例对象/数组从数据库中出来的样子,包括 3 个待办事项。
  • 有一个ID,每个待办事项列表都有一个(自动生成但可访问的)ID。
  • @agm1984 我已编辑帖子以显示条目
  • 抱歉,我对单个 ID 和多个待办事项感到困惑。我们有点需要每个人都有一个可以在用户下次重新加载页面时检查的 ID。我正在为您准备一个使用 localStorage 的解决方案,但您也可以使用 cookie。

标签: jquery node.js mongoose ejs


【解决方案1】:

好的,这是一个功能性的解决方案:

后台

app.get('/testing', (req, res, next) => {
    const todoItems = {
        '_id': {
            '$oid': '59bf2fed71c3840508539b29'
        },
        'items': [
            { id: 0, todo: 'ayy' },
            { id: 1, todo: 'yyaa' },
            { id: 2, todo: 'yyaaaafyyy' }
        ],
        '__v': 0
    }

    res.render('testing', {
        todoItems
    })
})

前端

<style>
    .complete {
        text-decoration: line-through;
    }
</style>

将您的项目放入列表中。事件侦听器将添加到 ID 为 #todo

的元素内的每个 &lt;li&gt; 元素
<ul id="todo">
    <% todoItems.items.forEach((item) => { %>
        <li id="<%- item.id %>">
            <%= item.todo %>
        </li>
    <% }) %>
</ul>

这是一些放在&lt;/body&gt;上方的JavaScript

<script>
    // Define a function that takes input of an HTML element
    // and then toggles the CSS class
    // then checks localStorage to see if the item is in it
    // if not, add it
    // if so, remove it
    const toggleComplete = (item) => {
        item.classList.toggle('complete')
        const isComplete = localStorage.getItem(item.innerText)
        if (!isComplete) {
            localStorage.setItem(item.innerText, 'complete')
            return
        }
        localStorage.removeItem(item.innerText)
    }

    // When the page loads, add an event listener to each <li>
    const items = document.querySelectorAll('#todo li')
    items.forEach((item) => item.addEventListener('click', () => toggleComplete(item)))

    // If the user leaves and comes back, we reinitialize
    // We step through the same array we add event listeners to
    // if the todo item is in localStorage, add the CSS class
    const reinitializeSession = () => {
        items.forEach((item) => {
            const isComplete = localStorage.getItem(item.innerText)
            if (!isComplete) return
            item.classList.add('complete')
        })
    }
    reinitializeSession()
</script>

【讨论】:

  • 如果你想实现这个,我希望你把它全部输入而不粘贴任何东西。从 JavaScript 部分开始,编写 const items 部分并在控制台记录它,这样您就可以看到结果,然后编写 toggleComplete() 函数,从它开始只执行 `console.log('firing this'),然后添加休息一下,然后看看页面是怎么响应的,最后加上reinitializeSession()函数。 :)
  • 效果很好!我真的很感谢代码中的 cmets,所以我真的知道发生了什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
  • 2010-11-12
  • 2010-10-05
  • 2023-04-10
  • 2022-11-12
  • 1970-01-01
相关资源
最近更新 更多