javascript - node.js 和 pug 中用户单击按钮和删除前一个问题时如何显示新问题?
问题描述
我想做一个简单的测验。当用户单击按钮时,向他显示新问题。我不知道该怎么做。
index.js
const express = require('express')
const app = express()
app.set('view engine', 'pug')
app.use(express.urlencoded({ extended: false }))
app.get('/', (req, res) => {
questions = ['firstQuestion', 'secondQuestion']
for (i = 0;i<questions.length;i++) {
res.render('index', {question: question[i]})
}
})
app.listen(3000)
指数.pug
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 Document
body
h1#a
button(onclick='func()')
script.
function func() {
document.getElementById("a").innerHTML = #{question}
}
解决方案
我不确定哈巴狗是如何工作的,但我有一些一般的想法给你
- 您可以在将 ip 映射到问题编号的服务器/数据库中保存字典。
- 您可以将 cookie 推送到用户客户端并将问题索引作为标题发送
- 您可以使用实时网络套接字,例如 Socket.io
请记住,您必须将数据保存在某处(userId 到 userQuestionIndex).. 这取决于您希望应用程序的安全程度。
推荐阅读
- javascript - 获取表中的下一个元素 javascript
- node.js - NPM 包锁版本属性
- c# - .Net 应用程序中带有 STATUS_STACK_OVERFLOW 的应用程序崩溃是否意味着堆栈溢出发生在本机代码中?
- android - 将数据从活动传递到android中的片段
- mysql - MySQL 从具有确切子行数的连接表中选择查询
- java - 在java中将具有多个数字的字符串转换为整数
- php - 我可以防止未经授权的用户访问文件而不将文件存储在文档根目录之外吗?
- python - 使用 String.split() 设计 Python
- string - strcpy() 函数的第二个参数中没有空字符会返回意外结果。为什么?
- python - 如何制作一个字典/ collections.counter 考虑到 Python 中的索引?