我想使用HTML,CSS和JavaScript做一個登錄表單,它工作正常。但問題是,它沒有顯示我的html內容,它只顯示另一頁上的用戶名。誰能幫幫我嗎?我希望target.html頁面顯示我的html內容和用戶名。如何使用onload javascript顯示html內容?
的index.html
<html>
<head>
<title>Login page</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>
Simple Login Page
</h1>
<form name="myform">
Username<input type="text" name="userid" id="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Reset"/>
</form>
<script language="javascript">
function check(form)
{
if(form.userid.value && form.pswrd.value)
{
window.location="target.html"
alert("Welcome to this page")
var userid = document.getElementById("userid").value;
localStorage.setItem("userid", userid);
}
else
{
alert("Error Password or Username")
}
}
</script>
</body>
</html>
target.html上
<html>
<head>
<title>Login page</title>
<style>
h1.text{
color: red;
}
</style>
<script>
function init(){
var userid = localStorage.getItem("userid");
document.write("Welcome "+userid);
}
window.onload=init;
</script>
<body>
<h1 class="text">Simple Login Page</h1>
<a href="confirm.html">Confirm.html</a>
</body>
</html>
不要使用'document.write'。 – SLaks
您有多個問題。最糟糕的是document.write。另一種情況是,在位置更改後,當前頁面無法執行任何操作 – mplungjan