[英]HTML DOM - spreading the string to multiple lines
I'm currently trying to spread the string to multiple lines at HTML/JS. 我目前正在尝试在HTML / JS上将字符串扩展到多行。 I'm using Repl.It (maybe it some bug/feature on this page) but I cannot spread it to multiple lines using "\\n".
我正在使用Repl.It(可能是此页面上的一些错误/功能),但是我无法使用“ \\ n”将其分散到多行中。 Please advise.
请指教。
HTML div: HTML div:
<div id="stats">
<script src="script.js"></script>
</div>
JS side: JS端:
function send_stats(message) {
document.querySelector("#stats").innerHTML = message;
}
send_stats("Stats:\nPlayer hp:" + player_hp + "\nMonster hp:" + monster_hp")
You should use <br>
instead of \\n
. 您应该使用
<br>
而不是\\n
。 And use Template Strings rather than using +
again and again. 并使用模板字符串,而不是一次又一次地使用
+
。
let monster_hp = 100.00 let player_hp = 90.00 function send_stats(message) { document.querySelector("#stats").innerHTML = message; } send_stats(`Stats:<br>Player hp:${player_hp}<br>Monster hp:${monster_hp}`)
<div id="stats"> <script src="script.js"></script> </div>
Use .innerText
instead of innerHTML
使用
.innerText
而不是innerHTML
var playerHp = 1; var monsterHp = 30; var msg = "Stats:\\nPlayer hp: " + playerHp + "\\nMonster hp: " + monsterHp; function sendStats(sel, msg) { document.querySelector(sel).innerText = msg; } sendStats('#stats', msg);
<output id='stats'></output>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.