我有一个index.html
我设置的页面userid
在localStorage
如下
Index.html
<div>
<input type="text" id='user_id' required tabindex="1" name="user_id" autofocus autocomplete=off placeholder="User Name">
</div>
<input class="user_login_submit" type="submit" id='btnLogin' tabindex="3" name="login_btnSubmit" tabindex='3' onclick='validateLogin();'value="LOGIN" />
<script>
function validateLogin() {
var userid = document.getElementById('user_id').value;
localStorage.setItem("userID", "Welcome, " + userid);
}
</script>
现在我想展示这个 userid
在navbar
即nav.html
页面,请注意它仅包含 navbar
代码中没有任何其他 header 或正文部分,
nav.html
<li><a href="#" id="profileDDL"style="text-transform:capitalize;opactiy:0.4;font-weight:bolder">
</a></li>
<script>
$(function() {
$('#profileDDL').innerHTML=localStorage.getItem("userID");
});
</script>
现在,我想加载这个 nav.html
内页user_home.html
或任何其他带有 userID
的 html 页面来自localStorage
, navbar
加载正常,但不是 localStorage
值。
user_home.html
<body>
<!--Navigation bar-->
<div id="nav-placeholder">
</div>
<!--end of Navigation bar-->
<script>
$(function(){
$("#nav-placeholder").load("nav.html");
});
</script>
</body>
我希望你明白我的问题是什么。我被困在这里了。
最佳答案
换行
$('#profileDDL').innerHTML=localStorage.getItem("userID");
到
$('#profileDDL').html(localStorage.getItem("userID"));
关于javascript - LocalStorage 值未显示在其他 html 页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47283581/