HTML代码
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
用户名 : <input type="text" name="username" id="username_input"></br>
密码 : <input type="password" name="passwd" id="passwd_input"></br>
<input type="button" value="登录" onclick="login()">
</div>
</body>
</html>
Js代码
<script type="text/javascript">var accountAll = [
{'username' : 'admin','passwd' : 'admin123'},
{'username' : 'jack','passwd' : 'jack'}
] // 存储账户json数据的数组
function login(){ //登陆判断
var username = document.getElementById('username_input').value // 获取username输入框内容
var passwd = document.getElementById('passwd_input').value // 获取passwd输入框内容
var account = accountAll.filter(function(e){return e.username == username})[0] // 筛选账号返回数组,不存在则返回空数组
if(!account){
console.log('账户不存在')
} else {
if(account.username == username && account.passwd == passwd){
console.log('登陆成功')
} else {
console.log('登陆失败')
}
}
}
</script>
function login(){
var username=document.getElementById("userName")
var password=document.getElementById("password")
if(username.value=="admin"&&password.value="123456")
{
window.location.href="success.html"
}
}
为登录按钮注册onclick=“login()”
注册页面代码:
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form method="post" onsubmit="return false" id="form">
<input type="text" name="username" placeholder="用户名"><br>
<input type="password" name="password" placeholder="密码"><br>
<input type="password" name="rePassword" placeholder="再次输入密码"><br>
<input type="submit" onclick="register_user()" value="注册"><br>
</form>
<a href="login.html">去登录</a>
<script>
var localDB = openDatabase('localDB', '1.0', 'Test DB', 2 * 1024 * 1024)
localDB.transaction(function (ts) {
ts.executeSql('CREATE TABLE IF NOT EXISTS user(username,password)')
})
function register_user() {
var username = document.getElementsByTagName("input")[0].value
var password = document.getElementsByTagName("input")[1].value
var rePassword = document.getElementsByTagName("input")[2].value
if(password != rePassword)
{
alert("两次输入的密码不同,请重新输入")
}else
{
localDB.transaction(function(ts){
ts.executeSql("INSERT INTO user (username,password) VALUES ('"+username+"','"+password+"')")
alert("注册成功")
})
}
}
</script>
</body>
</html>
登录页面代码:
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form onsubmit="return false" method="post">
<input type="text" placeholder="用户名"><br>
<input type="password" placeholder="密码"><br>
<input type="submit" value="登录" onclick="login()"><br>
</form>
<a href="register.html">去注册</a>
<script>
var localDB = openDatabase('localDB', '1.0', 'Test DB', 2 * 1024 * 1024)
localDB.transaction(function (ts) {
ts.executeSql("SELECT * FROM user",[],function (tx, results) {
var len = results.rows.length
if(len<=0)
{
localDB.transaction(function (ts) {
ts.executeSql('CREATE TABLE IF NOT EXISTS user(username,password)')
})
}
})
})
function login() {
var username = document.getElementsByTagName("input")[0].value
var password = document.getElementsByTagName("input")[1].value
localDB.transaction(function (ts) {
ts.executeSql("SELECT * FROM user WHERE username='"+username+"'",[],function (ts, res) {
if(res.rows.length<=0)
{
alert("登录失败,用户未注册")
}else if (password == res.rows[0].password)
{
alert("登录成功,三秒后跳转到百度")
setInterval(function () {
location.href = "https://www.baidu.com"
},3000)
}else
{
alert("登录失败,密码错误")
}
})
})
}
</script>
</body>
</html>
以上代码使用了WEB SQL,还请选择合适的浏览器查看。