我想让uinput
后面的h1
标签中出现Hello
。它应该显示他们在上一页中添加的用户名。
<body>
<div class="Header_2">
<div class="Head-Text-Box">
<script>
var userinput = localStorage.getItem("uinput");
</script>
<h1>Hello uinput <br> please choose from the options below</h1>
</div>
</div>
</body>
答案 0 :(得分:1)
获取标题标签的初始innerHTML并将innerHTML替换为您的变量值:
<div class="Header_2">
<div class="Head-Text-Box">
<h1>Hello {uinput} <br> please choose from the options below.</h1>
<script>
var userinput = "Sixpathsage6";//localStorage.getItem("uinput");
var h = document.querySelector("h1");
h.innerHTML = h.innerHTML.replace("{uinput}",userinput);
</script>
</div>
</div>
使用正则表达式替换多个{uinput}
:
h.innerHTML.replace(/\{uinput\}/g, userinput);