用原生js实现一个同样功能的简单登录页面。
同样的功能用js实现,可以看出dom操作是十分繁琐的。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>拖曳式列表排序</title>
  <!-- 简单加点样式 -->
  <style>
  </style>
</head>
<body>
    <div id="Application" style="text-align:center;">
        <h1>欢迎您,未登录</h1>
        <div>账号:<input type="text" name="userName" id="userName"></input></div>
        <div>密码:<input type="password" name="passWord" id="passWord"></input></div>
        <div style="border-radius: 30px;width:100px;margin: 20px auto;color:white;background-color: blue;" id="loginBtn">登录</div>
    </div>  
<script>
    let application=document.getElementById("Application");
    let h1=document.getElementsByTagName("h1")[0];
    let loginBtn=document.getElementById("loginBtn");
    let userName=document.getElementsByName("userName")[0];
    let passWord=document.getElementsByName("passWord")[0];
    let noLogin=true;
    loginBtn.onclick=function(){
        if(noLogin){
            if(userName.value.length>0&&passWord.value.length>0){
                alert(`userName:${userName.value}
                    password:${passWord.value}`);
                noLogin=false;
                h1.innerHTML=`欢迎您:${userName.value}`;
                loginBtn.innerHTML="注销";
                userName.value="";
                passWord.value="";
                //console.log(application);
                application.removeChild(h1.nextElementSibling);
                application.removeChild(h1.nextElementSibling);
            }else{
                alert("请输入账号密码");
            }
        }else{
                noLogin=true;
                h1.innerHTML=`欢迎您:未登录`;
                loginBtn.innerHTML="登陆";
                let user=document.createElement("div");
                let usertext=document.createTextNode("账号:");
                user.appendChild(usertext);
                user.appendChild(userName);
                let pass=document.createElement("div");
                let passtext=document.createTextNode("密码:");
                pass.appendChild(passtext);
                pass.appendChild(passWord);
                application.insertBefore(user,loginBtn);
                application.insertBefore(pass,loginBtn);
        }
    }
</script>
</body>
</html>

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐