如何将document.write()与文件一起使用而不是文本?

发布于 2025-02-10 18:54:58 字数 423 浏览 1 评论 0原文

我正在使用socket.io创建类似于针对人类卡的派对游戏。我只是想知道如何在新游戏开始时不必将所有数据发送到新页面,而无需将所有数据发送到新页面。我当时想只是更改HTML的身体,但请留在同一页面上,以便JavaScript无法重置,我仍然可以访问所有播放器信息。

我遇到使用document.write(),但这似乎只接受字符串参数。有没有办法做这样的事情document.write(game.html)

关于如何解决这个问题的任何想法,也许是在JavaScript文件之间传递信息的一种更好的方法,而无需每次加载新页面时都会发布数据?我尝试通过帖子发送数据,但是这使得所有内容保持一致变得更加困难。

ps这是我第一次使用node.js,socket.io,javascript等正确地做任何事情,所以我可以问一个愚蠢的问题。

I am using socket.io to create a party game similar to cards against humanity. I am just wondering how I can keep the players name and score etc without having to send all the data to a new page when new games begin. I was thinking to just change the body of the html but stay on the same page so the javascript doesnt reset and I can still access all the players information.

I came across using document.write() however this appears to only accept a string parameter. Is there a way to do something like this document.write(game.html).

Any ideas on how to go about this, maybe a better way to pass information between javascript files without having to post the data each time a new page is loaded? I have tried sending the data with post however it makes it far more difficult to keep everything consistent.

P.S This is the first time I have properly made anything with node.js, socket.io, javascript etc. So I could be asking a stupid question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-02-17 18:54:58

从事件侦听器中获取socket.io数据后,您可以使用QuerySelector()或任何其他DOM操纵方法来显示DOM中的数据。这样的东西?

HTML

<div id="panel"></div>

JS

const socket = io("ws://localhost:3000" )
const panel = document.querySelector("#panel")
// receive a message from the server
socket.on( "hello", (arg) => {
    panel.innerText = arg
});

Once you get socket.io data from an event listener, you can use querySelector() or any other DOM manipulation method to show the data in the DOM. Something like this?

HTML

<div id="panel"></div>

JS

const socket = io("ws://localhost:3000" )
const panel = document.querySelector("#panel")
// receive a message from the server
socket.on( "hello", (arg) => {
    panel.innerText = arg
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文