【发布时间】:2020-04-05 13:53:12
【问题描述】:
我正在编写一个带有 HTML 网络套接字的 MUD 客户端,我从 MUD 服务器接收 HTML。 本质上将其视为聊天客户端。
当我从 MUD 服务器接收到以两列水平布局呈现的数据时,只有当我一次接收整个数据时它才会起作用,而如果我一次接收一行,它会变得很好将两列格式转换为单列布局。
重现问题的示例代码在这里。我希望有某种解决方案,不需要在将整个字符串添加到 innerHTML 之前组装整个字符串,如第三个解决方案所示。
FF 和 Chrome 中的错误格式相同,这让我希望有解决方案 :-)
<html>
<body>
<br/><hr/>
<!-- *** Conversation Output *** -->
<div id="static" role="log" aria-live="polite" width="100%">
what it should look like: <br/>
<table class='twocolhor'>
<tr><td><wielded></td><td><div class='wielded'>a hand mace</div></td></tr>
<tr><td><worn about body></td><td><div class='wielded'>white robes of a healer</div></td></tr>
<tr><td><worn on feet></td><td><div class='wielded'>a pair of sandals</div></td></tr>
</table>
</div>
<br/><hr/>
<div id="incomingMsgOutput2" role="log" aria-live="polite" width="100%">
Sample, this will NOT work<br/>
</div>
<br/><hr/>
<div id="incomingMsgOutput3" role="log" aria-live="polite" width="100%">
Sample, this will work<br/>
</div>
</body>
<script>
var obj = document.getElementById("incomingMsgOutput2");
obj.innerHTML += "<table class='twocolhor'>"
obj.innerHTML += "<tr><td><wielded></td><td><div class='wielded'>a hand mace</div></td></tr>"
obj.innerHTML += "<tr><td><worn about body></td><td><div class='wielded'>white robes of a healer</div></td></tr>"
obj.innerHTML += "<tr><td><worn on feet></td><td><div class='wielded'>a pair of sandals</div></td></tr>"
obj.innerHTML += "</table>"
var obj = document.getElementById("incomingMsgOutput3");
obj.innerHTML += "<table class='twocolhor'>" + "<tr><td><wielded></td><td><div class='wielded'>a hand mace</div></td></tr>" +
"<tr><td><worn about body></td><td><div class='wielded'>white robes of a healer</div></td></tr>" +
"<tr><td><worn on feet></td><td><div class='wielded'>a pair of sandals</div></td></tr>" +
"</table>"
</script>
</html>
【问题讨论】:
-
我最终在服务器端组装了连贯的 HTML 字符串,而不是尝试在客户端组装它。
标签: javascript html