document.getElementById("form1").username.focus();

本文介绍了如何使用JavaScript的focus方法来使表单中的特定元素获得焦点。通过两种方式实现:一是通过表单ID和元素name属性结合的方式;二是直接利用元素ID进行聚焦。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设有一表单,ID属性为form1,其中包含有一个文本框,name属性为abc,那么下面语句:
document.getElementById('form1').abc.focus();
表示将输入焦点定位到该表单的该文本框中.
focus方法用于使对象获得焦点.
再假设文本框有id属性,且值为abc,那么
document.getElementById('abc').focus();
可以直接操作该文本框,而无需通过表单的父子关系.
<!DOCTYPE html> <!--suppress ALL --> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>安全登录系统</title> <style> :root { /* 合并后的统一变量 */ --white: #e9e9e9; --gray: #333; --blue: #0367a6; --lightblue: #008997; --button-radius: 0.7rem; --max-width: 758px; --max-height: 420px; font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; } body { align-items: center; background: url("https://res.cloudinary.com/dbhnlktrv/image/upload/v1599997626/background_oeuhe7.jpg"); background-attachment: fixed; background-position: center; background-repeat: no-repeat; background-size: cover; display: grid; height: 100vh; place-items: center; margin: 0; } .form__title { font-weight: 300; margin: 0; margin-bottom: 1.25rem; } .link { color: var(--gray); font-size: 0.9rem; margin: 1.5rem 0; text-decoration: none; } .container { background-color: var(--white); border-radius: var(--button-radius); box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25), 0 0.7rem 0.7rem rgba(0, 0, 0, 0.22); height: var(--max-height); max-width: var(--max-width); overflow: hidden; position: relative; width: 100%; } .container__form { height: 100%; position: absolute; top: 0; transition: all 0.6s ease-in-out; } .container--signin { left: 0; width: 50%; z-index: 2; } .container.right-panel-active .container--signin { transform: translateX(100%); } .container--signup { left: 0; opacity: 0; width: 50%; z-index: 1; } .container.right-panel-active .container--signup { animation: show 0.6s; opacity: 1; transform: translateX(100%); z-index: 5; } .container__overlay { height: 100%; left: 50%; overflow: hidden; position: absolute; top: 0; transition: transform 0.6s ease-in-out; width: 50%; z-index: 100; } .container.right-panel-active .container__overlay { transform: translateX(-100%); } .overlay { background: url("https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg"); background-attachment: fixed; background-position: center; background-repeat: no-repeat; background-size: cover; height: 100%; left: -100%; position: relative; transform: translateX(0); transition: transform 0.6s ease-in-out; width: 200%; } .container.right-panel-active .overlay { transform: translateX(50%); } .overlay__panel { align-items: center; display: flex; flex-direction: column; height: 100%; justify-content: center; position: absolute; text-align: center; top: 0; transform: translateX(0); transition: transform 0.6s ease-in-out; width: 50%; } .overlay--left { transform: translateX(-20%); } .container.right-panel-active .overlay--left { transform: translateX(0); } .overlay--right { right: 0; transform: translateX(0); } .container.right-panel-active .overlay--right { transform: translateX(20%); } .btn { background-color: var(--blue); background-image: linear-gradient(90deg, var(--blue) 0%, var(--lightblue) 74%); border-radius: 20px; border: 1px solid var(--blue); color: var(--white); cursor: pointer; font-size: 0.8rem; font-weight: bold; letter-spacing: 0.1rem; padding: 0.9rem 4rem; text-transform: uppercase; transition: transform 80ms ease-in; } .form > .btn { margin-top: 1.5rem; } .btn:active { transform: scale(0.95); } .btn:focus { outline: none; } .form { background-color: var(--white); display: flex; align-items: center; justify-content: center; flex-direction: column; padding: 0 3rem; height: 100%; text-align: center; } .input { background-color: #fff; border: none; padding: 0.9rem 0.9rem; margin: 0.5rem 0; width: 100%; box-sizing: border-box; } @keyframes show { 0%, 49.99% { opacity: 0; z-index: 1; } 50%, 100% { opacity: 1; z-index: 5; } } /* 新增功能样式 */ .loader { border: 4px solid rgba(0, 0, 0, 0.1); border-top: 4px solid #0367a6; border-radius: 50%; width: 30px; height: 30px; animation: spin 1s linear infinite; display: none; margin: 10px auto; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } .error-message { color: #ff3860; font-size: 0.85rem; margin-top: 0.5rem; height: 20px; display: flex; align-items: center; justify-content: center; } .hidden { display: none; } .password-container { position: relative; width: 100%; } .toggle-password { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); background: none; border: none; color: var(--gray); cursor: pointer; font-size: 0.8rem; } .success-message { color: #23d160; font-size: 0.85rem; margin-top: 0.5rem; display: none; } </style> </head> <body> <!-- 登录页面容器 --> <div id="loginPage" class="container right-panel-active"> <!-- 注册表单 --> <div class="container__form container--signup"> <form class="form" id="signupForm"> <h2 class="form__title">注册账号</h2> <input type="text" placeholder="用户名" class="input" required id="signupUsername"/> <input type="email" placeholder="邮箱" class="input" required id="signupEmail"/> <div class="password-container"> <input type="password" placeholder="密码" class="input" required id="signupPassword"/> <button type="button" class="toggle-password">👁️</button> </div> <div class="error-message" id="signupError"></div> <div class="success-message" id="signupSuccess"></div> <button class="btn" type="submit">注册</button> </form> </div> <!-- 登录表单 --> <div class="container__form container--signin"> <form class="form" id="loginForm"> <h2 class="form__title">用户登录</h2> <input type="email" placeholder="邮箱" class="input" required id="loginEmail"/> <div class="password-container"> <input type="password" placeholder="密码" class="input" required id="loginPassword"/> <button type="button" class="toggle-password">👁️</button> </div> <div class="loader" id="loginLoader"></div> <div class="error-message" id="loginError"></div> <button class="btn" type="submit">登录</button> </form> </div> <!-- 覆盖层 --> <div class="container__overlay"> <div class="overlay"> <div class="overlay__panel overlay--left"> <button class="btn" id="signInBtn">去登录</button> </div> <div class="overlay__panel overlay--right"> <button class="btn" id="signUpBtn">去注册</button> </div> </div> </div> </div> <!-- 仪表盘页面 (登录成功后显示) --> <div id="dashboardPage" class="hidden"> <div class="container" style="background-color: #ffffff; padding: 15rem; max-width: 1000px;"> <h2>欢迎, <span id="userDisplayName"></span>!</h2> <p>您已成功登录!这是您的个人控制面板。</p> <!-- 文本输入区域 --> <div style="margin-top: 2rem;"> <h3>内容编辑区</h3> <textarea id="userContent" rows="6" style="width: 100%; padding: 10px; font-family: inherit; font-size: 1rem;"></textarea> <button id="saveContentBtn" class="btn" style="margin-top: 1rem; padding: 0.7rem 2rem;">保存内容</button> <div class="success-message" id="saveSuccess" style="text-align: left; margin-top: 0.5rem;"></div> </div> <div style="margin-top: 2rem;"> <p>电子邮箱: <span id="userEmailDisplay"></span></p> <p>注册时间: <span id="registrationDate"></span></p> <button id="logoutBtn" class="btn" style="margin-top: 2rem;">退出登录</button> </div> </div> </div> <script> // ======== 页面元素 ======== const signInBtn = document.getElementById("signInBtn"); const signUpBtn = document.getElementById("signUpBtn"); const container = document.querySelector(".container"); const loginForm = document.getElementById("loginForm"); const signupForm = document.getElementById("signupForm"); const loginLoader = document.getElementById("loginLoader"); const loginError = document.getElementById("loginError"); const signupError = document.getElementById("signupError"); const signupSuccess = document.getElementById("signupSuccess"); const loginPage = document.getElementById("loginPage"); const dashboardPage = document.getElementById("dashboardPage"); const logoutBtn = document.getElementById("logoutBtn"); const userDisplayName = document.getElementById("userDisplayName"); const userEmailDisplay = document.getElementById("userEmailDisplay"); const registrationDate = document.getElementById("registrationDate"); const togglePasswordBtns = document.querySelectorAll(".toggle-password"); // ======== 密码可见性切换 ======== togglePasswordBtns.forEach(btn => { btn.addEventListener("click", function () { const input = this.previousElementSibling; const type = input.getAttribute("type") === "password" ? "text" : "password"; input.setAttribute("type", type); this.textContent = type === "password" ? "👁️" : "🔒"; }); }); // ======== 面板切换功能 ======== signInBtn.addEventListener("click", () => { container.classList.remove("right-panel-active"); clearMessages(); }); signUpBtn.addEventListener("click", () => { container.classList.add("right-panel-active"); clearMessages(); }); // ======== 注册功能 ======== signupForm.addEventListener("submit", function (e) { e.preventDefault(); const username = document.getElementById("signupUsername").value.trim(); const email = document.getElementById("signupEmail").value.trim(); const password = document.getElementById("signupPassword").value; // 验证用户名(至少3个字符) if (username.length < 3) { showSignupError("用户名至少需要3个字符"); return; } // 验证邮箱格式 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { showSignupError("请输入有效的电子邮箱"); return; } // 验证密码强度(至少6个字符) if (password.length < 6) { showSignupError("密码长度至少为6个字符"); return; } // 检查用户是否已注册(伪代码) if (localStorage.getItem(email)) { showSignupError("该邮箱已注册"); return; } // 保存注册信息 const userData = { username, email, password, // 实际应用中应存储哈希值而非明文 registrationDate: new Date().toLocaleDateString() }; localStorage.setItem(email, JSON.stringify(userData)); // 注册成功 signupSuccess.textContent = "注册成功!"; signupSuccess.style.display = "block"; signupError.style.display = "none"; // 3秒后切换到登录面板 setTimeout(() => { container.classList.remove("right-panel-active"); signupForm.reset(); signupSuccess.style.display = "none"; }, 3000); }); // ======== 登录功能 ======== loginForm.addEventListener("submit", function (e) { e.preventDefault(); const email = document.getElementById("loginEmail").value; const password = document.getElementById("loginPassword").value; // 显示加载状态 loginLoader.style.display = 'block'; loginError.style.display = 'none'; // 模拟登录请求延迟 setTimeout(() => { const authResult = authenticateUser(email, password); if (authResult.success) { // 保存登录状态 localStorage.setItem("isLoggedIn", "true"); localStorage.setItem("currentUser", JSON.stringify(authResult.userData)); // 更新仪表盘信息 updateDashboard(authResult.userData); // 切换到仪表盘 loginPage.classList.add("hidden"); dashboardPage.classList.remove("hidden"); } else { showLoginError(authResult.message); } loginLoader.style.display = 'none'; }, 1500); }); // ======== 退出功能 ======== logoutBtn.addEventListener("click", function () { // 清除登录状态 localStorage.removeItem("isLoggedIn"); localStorage.removeItem("currentUser"); // 返回登录页面 dashboardPage.classList.add("hidden"); loginPage.classList.remove("hidden"); // 重置表单 loginForm.reset(); clearMessages(); }); // ======== 辅助函数 ======== function authenticateUser(email, password) { // 从本地存储获取用户数据 const userData = localStorage.getItem(email); if (!userData) { return { success: false, message: "邮箱未注册" }; } const user = JSON.parse(userData); // 实际应用中应使用密码哈希值比较 if (password !== user.password) { return { success: false, message: "邮箱或密码错误" }; } return { success: true, userData: user }; } function updateDashboard(userData) { userDisplayName.textContent = userData.username; userEmailDisplay.textContent = userData.email; registrationDate.textContent = userData.registrationDate; } function showLoginError(message) { loginError.textContent = message; loginError.style.display = 'block'; } function showSignupError(message) { signupError.textContent = message; signupError.style.display = 'block'; signupSuccess.style.display = "none"; } function clearMessages() { loginError.style.display = 'none'; signupError.style.display = 'none'; signupSuccess.style.display = 'none'; } // 页面加载时检查登录状态 window.addEventListener('DOMContentLoaded', () => { const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'; const currentUser = localStorage.getItem('currentUser'); if (isLoggedIn && currentUser) { updateDashboard(JSON.parse(currentUser)); loginPage.classList.add("hidden"); dashboardPage.classList.remove("hidden"); } }); </script> </body> </html> 登陆成功后加一个上下滑动翻页功能
最新发布
06-11
以下源码的用户和密码分别是:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN"> <head> <meta HTTP-EQUIV="Pragma" CONTENT="no-cache"> <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=GBK"> <title>安邦系统平台入口-登录验证</title> <style> *{margin:0px;padding:0px;} html,body{margin:0px; padding:0px;height:100%;width:100%;border:0px;} body{background:#6c6c6c;height:100%;width:100%;overflow:hidden;} .LoginDIV{width:700px;margin:0px auto;;padding:0px;overflow:hidden;} .FootDIV {width:700px;margin:0px auto;;padding:0px;overflow:hidden;text-align:center;} .TextInput{padding:3px 0px 3px 5px;width:100px;border-left:solid 1px #444444;border-top:solid 1px #444444;border-right:solid 1px #ffffff;;border-bottom:solid 1px #ffffff;font-size:13px;text-align:center;} </style> <script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script> <script language='JavaScript'> function getObjByID(Id){if('object'==typeof(Id))return Id;else if('string'==typeof(Id))return document.getElementById(Id);else return null;} function SetCookie(inc_Name,INC_Value,INC_Hours) {   var TheEXP = new Date();   TheEXP.setTime(TheEXP.getTime() + 60*60*1000*INC_Hours);   document.cookie= inc_Name + "=" + INC_Value + ";expires="+ TheEXP.toGMTString(); } function GetCookie(inc_Name) {   var cookieString = new String(document.cookie);   var cookieHeader = inc_Name + "=";   var beginPosition = cookieString.indexOf(cookieHeader);   if (beginPosition != -1) { cookieString=cookieString.substring(beginPosition + cookieHeader.length); beginPosition=cookieString.indexOf(";") if (beginPosition != -1){cookieString=cookieString.substring(0,beginPosition);} return cookieString; }   else {return "";} } function GetAllCookie() { var CookieUserName=GetCookie('CKUserName'); var CookieUserPass=GetCookie('CKUserPass'); var CookieKeepMyInfo=GetCookie('CKKeepMyInfo'); document.getElementById('UserName').value=CookieUserName; document.getElementById('UserPass').value=CookieUserPass; if (CookieKeepMyInfo.replace(/\s*/,'')=='1') {document.getElementById('KeepMyInfo').value==1;} else {document.getElementById('KeepMyInfo').value==0;} } function CheckForm(inc_form) { var FOBJ=document.getElementById(inc_form); var UserName=FOBJ.UserName.value.replace(/\s*/g,''); var UserPass=FOBJ.UserPass.value.replace(/\s*/g,''); if (UserName==''){alert('请输入帐号');FOBJ.UserName.value='';FOBJ.UserName.focus();return false;} if (UserPass==''){alert('请输入密码');FOBJ.UserPass.value='';FOBJ.UserPass.focus();return false;} if (UserPass==''){alert('请输入密码');FOBJ.UserPass.value='';FOBJ.UserPass.focus();return false;} if (document.getElementById('KeepMyInfo').value==1) { SetCookie("CKUserName",UserName,48); SetCookie("CKUserPass",UserPass,48); SetCookie("CKKeepMyInfo",1,48); } else { SetCookie("CKUserName","",48); SetCookie("CKUserPass","",48); SetCookie("CKKeepMyInfo",0,48); } return true; } function CheckKeydown(inc_event,inc_NextOBJ,inc_FS) { if (inc_event.keyCode==13) { if (inc_FS==0) {document.getElementById(inc_NextOBJ).focus();} else { if(CheckForm(inc_NextOBJ)){document.getElementById(inc_NextOBJ).submit();} } } } function resizeWindow() { var BodyHeight=0; var TopDIVHeight=0; var LoginDIVHeight=0; var IMGLaoYingHeight=0; var IMGLaoYingWidth=0; BodyHeight=$("body").height(); LoginDIVHeight=$("#LoginDIV").height(); IMGLaoYingHeight=$("#IMGLaoYing").height(); TopDIVHeight=BodyHeight-LoginDIVHeight; var IMGMarginTop= (TopDIVHeight-IMGLaoYingHeight)/2.5 + 'px'; $("#TopDIV").height(TopDIVHeight); $("#IMGLaoYing").css("margin-top",IMGMarginTop); } $(document).ready(function(){resizeWindow();}); $(window).resize(function() {resizeWindow();}); </script> </head> <body onload="GetAllCookie();document.getElementById('UserName').focus();"> <div id="TopDIV" style="overflow:hidden;text-align:center;background:#000000;margin:0px auto;width:100%;background:#000000;height:70%"> <img id='IMGLaoYing' src="/images/laoying.png" style="height:55%;max-width:650px;"> </div> <div id="LoginDIV" style="overflow:hidden;text-align:center;background:#000000;margin:0px auto;width:100%;background:#c07814;height:200px"> <div style="overflow:hidden;height:50px;"> </div> <div style="overflow:hidden;margin:0px auto;width:500px;text-align:center;border:solid 0px #000000"> <form method='post' id='LogForm' name='LogForm' action='CheckPWD.php' style='margin:0px;padding:0px;overflow:hidden;'> <div style="overflow:hidden;float:left;margin-left:5px;"></div> <div style="overflow:hidden;float:left;font-size:13px;padding-top:3px;color:#222222;font-weight:bold;font-family:'Microsoft YaHei' ! important;">用  户:</div> <div style="overflow:hidden;float:left;margin-left:5px;"><Input Type="Text" class="TextInput" name="UserName" id="UserName" value="" maxlength="30" onkeydown="CheckKeydown(event,'UserPass',0);"></div> <div style="overflow:hidden;float:left;width:60px;"> </div> <div style="overflow:hidden;float:left;font-size:13px;padding-top:3px;color:#222222;font-weight:bold;font-family:'Microsoft YaHei' ! important;">密  码:</div> <div style="overflow:hidden;float:left;margin-left:5px;"><Input Type="password" class="TextInput" name="UserPass" id="UserPass" value="" maxlength="30" onkeydown="CheckKeydown(event,'LogForm',1);"></div> <div style="overflow:hidden;float:left;width:55px;"> </div> <div style="overflow:hidden;float:left;"> <Input Type="button" value=" 确 认 " onclick="if(CheckForm('LogForm')){LogForm.submit();}" style="width:80px;height:25px;background:#900a12;border:solid 0px #ffffff;font-size:13px;color:#ffffff;font-weight:bold;font-family:'Microsoft YaHei' ! important;"> <input type="hidden" id="KeepMyInfo" name="KeepMyInfo" value="1"> </div> </form> </div> <div style="clear:both;overflow:hidden;height:50px;"></div> <div style="overflow:hidden;text-align:center;font-size:12px;color:#333333">安邦咨询(ANBOUND)内部管理系统,&copy 2025</div> </div> </body> </html>
05-28
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>安全登录系统</title> <style> /* 原样保留所有样式不变 */ /* 添加加载动画 */ .loader { border: 4px solid rgba(0, 0, 0, 0.1); border-top: 4px solid #0367a6; border-radius: 50%; width: 30px; height: 30px; animation: spin 1s linear infinite; display: none; margin: 0 auto; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } /* 错误提示 */ .error-message { color: #ff3860; font-size: 0.85rem; margin-top: 0.5rem; display: none; } /* 隐藏页面 */ .hidden { display: none; } </style> </head> <body> <!-- 登录页面容器 --> <div id="loginPage" class="container right-panel-active"> <!-- 注册表单 --> <div class="container__form container--signup"> <form class="form" id="signupForm"> <h2 class="form__title">注册账号</h2> <input type="text" placeholder="用户名" class="input" required id="signupUsername"/> <input type="email" placeholder="邮箱" class="input" required id="signupEmail"/> <input type="password" placeholder="密码" class="input" required id="signupPassword"/> <div class="error-message" id="signupError"></div> <button class="btn" type="submit">注册</button> </form> </div> <!-- 登录表单 --> <div class="container__form container--signin"> <form class="form" id="loginForm"> <h2 class="form__title">用户登录</h2> <input type="email" placeholder="邮箱" class="input" required id="loginEmail"/> <input type="password" placeholder="密码" class="input" required id="loginPassword"/> <div class="loader" id="loginLoader"></div> <div class="error-message" id="loginError"></div> <button class="btn" type="submit">登录</button> </form> </div> <!-- 覆盖层 --> <div class="container__overlay"> <div class="overlay"> <div class="overlay__panel overlay--left"> <button class="btn" id="signInBtn">去登录</button> </div> <div class="overlay__panel overlay--right"> <button class="btn" id="signUpBtn">去注册</button> </div> </div> </div> </div> <!-- 仪表盘页面 (登录成功后显示) --> <div id="dashboardPage" class="hidden"> <div class="container" style="background-color: #ffffff; padding: 2rem; max-width: 800px;"> <h2>欢迎来到...</h2> <p>您已成功登录!这是您的个人控制面板。</p> <button id="logoutBtn" class="btn">退出登录</button> </div> </div> <script> // ======== 页面元素 ======== const signInBtn = document.getElementById("signInBtn"); const signUpBtn = document.getElementById("signUpBtn"); const container = document.querySelector(".container"); const loginForm = document.getElementById("loginForm"); const signupForm = document.getElementById("signupForm"); const loginLoader = document.getElementById("loginLoader"); const loginError = document.getElementById("loginError"); const loginPage = document.getElementById("loginPage"); const dashboardPage = document.getElementById("dashboardPage"); const logoutBtn = document.getElementById("logoutBtn"); // ======== 页面切换功能 ======== signInBtn.addEventListener("click", () => { container.classList.remove("right-panel-active"); }); signUpBtn.addEventListener("click", () => { container.classList.add("right-panel-active"); }); // ======== 登录功能 ======== loginForm.addEventListener("submit", async function(e) { e.preventDefault(); const email = document.getElementById("loginEmail").value; const password = document.getElementById("loginPassword").value; // 显示加载状态,隐藏错误信息 loginLoader.style.display = 'block'; loginError.style.display = 'none'; try { // 模拟登录请求(真实环境中应发送到服务器) const loginSuccessful = await authenticateUser(email, password); if (loginSuccessful) { // 切换到仪表盘页面 loginPage.classList.add("hidden"); dashboardPage.classList.remove("hidden"); loginError.style.display = 'none'; } else { showLoginError("邮箱或密码错误"); } } catch (error) { showLoginError("登录服务暂时不可用"); } finally { loginLoader.style.display = 'none'; } }); // ======== 退出功能 ======== if(logoutBtn) { logoutBtn.addEventListener("click", function() { // 清理会话(真实环境中应该调用后端API) localStorage.removeItem("isLoggedIn"); // 返回登录页面 dashboardPage.classList.add("hidden"); loginPage.classList.remove("hidden"); }); } // ======== 辅助函数 ======== async function authenticateUser(email, password) { return new Promise(resolve => { // 模拟网络请求延迟 setTimeout(() => { // 简单逻辑:密码需要至少6个字符 resolve(email.includes('@') && password.length >= 6); }, 1500); }); } function showLoginError(message) { loginError.textContent = message; loginError.style.display = 'block'; } // 页面加载时检查登录状态 window.addEventListener('DOMContentLoaded', () => { // 真实环境中应该检查用户会话 const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'; if (isLoggedIn) { loginPage.classList.add("hidden"); dashboardPage.classList.remove("hidden"); } }); </script> </body> </html> 和 <!DOCTYPE html> <!--suppress ALL --> <html lang="en"> <!-- https://codepen.io/danielkvist/pen/LYNVyPL --> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> :root { /* COLORS */ --white: #e9e9e9; --gray: #333; --blue: #0367a6; --lightblue: #008997; /* RADII */ --button-radius: 0.7rem; /* SIZES */ --max-width: 758px; --max-height: 420px; font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; } body { align-items: center; background-color: var(--white); background: url("https://res.cloudinary.com/dbhnlktrv/image/upload/v1599997626/background_oeuhe7.jpg"); /* 决定背景图像的位置是在视口内固定,或者随着包含它的区块滚动。 */ /* https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-attachment */ background-attachment: fixed; background-position: center; background-repeat: no-repeat; background-size: cover; display: grid; height: 100vh; place-items: center; } .form__title { font-weight: 300; margin: 0; margin-bottom: 1.25rem; } .link { color: var(--gray); font-size: 0.9rem; margin: 1.5rem 0; text-decoration: none; } .container { background-color: var(--white); border-radius: var(--button-radius); box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25), 0 0.7rem 0.7rem rgba(0, 0, 0, 0.22); height: var(--max-height); max-width: var(--max-width); overflow: hidden; position: relative; width: 100%; } .container__form { height: 100%; position: absolute; top: 0; transition: all 0.6s ease-in-out; } .container--signin { left: 0; width: 50%; z-index: 2; } .container.right-panel-active .container--signin { transform: translateX(100%); } .container--signup { left: 0; opacity: 0; width: 50%; z-index: 1; } .container.right-panel-active .container--signup { animation: show 0.6s; opacity: 1; transform: translateX(100%); z-index: 5; } .container__overlay { height: 100%; left: 50%; overflow: hidden; position: absolute; top: 0; transition: transform 0.6s ease-in-out; width: 50%; z-index: 100; } .container.right-panel-active .container__overlay { transform: translateX(-100%); } .overlay { background-color: var(--lightblue); background: url("https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg"); background-attachment: fixed; background-position: center; background-repeat: no-repeat; background-size: cover; height: 100%; left: -100%; position: relative; transform: translateX(0); transition: transform 0.6s ease-in-out; width: 200%; } .container.right-panel-active .overlay { transform: translateX(50%); } .overlay__panel { align-items: center; display: flex; flex-direction: column; height: 100%; justify-content: center; position: absolute; text-align: center; top: 0; transform: translateX(0); transition: transform 0.6s ease-in-out; width: 50%; } .overlay--left { transform: translateX(-20%); } .container.right-panel-active .overlay--left { transform: translateX(0); } .overlay--right { right: 0; transform: translateX(0); } .container.right-panel-active .overlay--right { transform: translateX(20%); } .btn { background-color: var(--blue); background-image: linear-gradient(90deg, var(--blue) 0%, var(--lightblue) 74%); border-radius: 20px; border: 1px solid var(--blue); color: var(--white); cursor: pointer; font-size: 0.8rem; font-weight: bold; letter-spacing: 0.1rem; padding: 0.9rem 4rem; text-transform: uppercase; transition: transform 80ms ease-in; } .form>.btn { margin-top: 1.5rem; } .btn:active { transform: scale(0.95); } .btn:focus { outline: none; } .form { background-color: var(--white); display: flex; align-items: center; justify-content: center; flex-direction: column; padding: 0 3rem; height: 100%; text-align: center; } .input { background-color: #fff; border: none; padding: 0.9rem 0.9rem; margin: 0.5rem 0; width: 100%; } @keyframes show { 0%, 49.99% { opacity: 0; z-index: 1; } 50%, 100% { opacity: 1; z-index: 5; } } </style> </head> <body> <div class="container right-panel-active"> <!-- Sign Up --> <div class="container__form container--signup"> <form action="#" class="form" id="form1"> <h2 class="form__title">Sign Up</h2> <input type="text" placeholder="User" class="input" /> <input type="email" placeholder="Email" class="input" /> <input type="password" placeholder="Password" class="input" /> <button class="btn">Sign Up</button> </form> </div> <!-- Sign In --> <div class="container__form container--signin"> <form action="#" class="form" id="form2"> <h2 class="form__title">Sign In</h2> <input type="email" placeholder="Email" class="input" /> <input type="password" placeholder="Password" class="input" /> <button class="btn">Sign In</button> </form> </div> <!-- Overlay --> <div class="container__overlay"> <div class="overlay"> <div class="overlay__panel overlay--left"> <button class="btn" id="signIn">Sign In</button> </div> <div class="overlay__panel overlay--right"> <button class="btn" id="signUp">Sign Up</button> </div> </div> </div> </div> <script> const signInBtn = document.getElementById("signIn"); const signUpBtn = document.getElementById("signUp"); const fistForm = document.getElementById("form1"); const secondForm = document.getElementById("form2"); const container = document.querySelector(".container"); signInBtn.addEventListener("click", () => { container.classList.remove("right-panel-active"); }); signUpBtn.addEventListener("click", () => { container.classList.add("right-panel-active"); }); fistForm.addEventListener("submit", (e) => e.preventDefault()); secondForm.addEventListener("submit", (e) => e.preventDefault()); loginLoader.style.display = 'block'; loginError.style.display = 'none'; </script> </body> </html> 合一起
06-08
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>智能制造赛登录系统</title> <style> /* 智能制造主题配色 */ :root { --tech-blue: #1a73e8; --industrial-gray: #2d3748; } body { background: linear-gradient(135deg, var(--industrial-gray), #4a5568); height: 100vh; display: flex; justify-content: center; align-items: center; font-family: 'Segoe UI', sans-serif; } .login-container { background: rgba(255, 255, 255, 0.95); padding: 2.5rem; border-radius: 12px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); width: 380px; transition: transform 0.3s ease; } .login-container:hover { transform: translateY(-5px); } h2 { color: var(--tech-blue); text-align: center; margin-bottom: 2rem; font-size: 1.8rem; text-transform: uppercase; letter-spacing: 2px; } .form-group { margin-bottom: 1.5rem; position: relative; } input { width: 100%; padding: 12px 16px; border: 2px solid #e2e8f0; border-radius: 6px; font-size: 1rem; transition: border-color 0.3s ease; } input:focus { outline: none; border-color: var(--tech-blue); box-shadow: 0 0 8px rgba(26, 115, 232, 0.3); } button { width: 100%; padding: 12px; background: var(--tech-blue); color: white; border: none; border-radius: 6px; font-size: 1.1rem; cursor: pointer; transition: background 0.3s ease; } button:hover { background: #1557b0; } .error-message { color: #e53e3e; font-size: 0.9rem; margin-top: 0.5rem; display: none; } </style> </head> <body> <div class="login-container"> <h2>智能制造赛登录</h2> <form id="loginForm"> <div class="form-group"> <input type="text" id="username" placeholder="工号/用户名" required> </div> <div class="form-group"> <input type="password" id="password" placeholder="密码" required> </div> <button type="submit">登录系统</button> <div class="error-message" id="errorMsg"></div> </form> </div> <script> document.getElementById('loginForm').addEventListener('submit', function(e) { e.preventDefault(); const username = document.getElementById('username').value; const password = document.getElementById('password').value; const errorMsg = document.getElementById('errorMsg'); // 基础验证(示例) if (!username || !password) { errorMsg.textContent = "工号和密码不能为空!"; errorMsg.style.display = 'block'; return; } // 模拟登录请求 if (username === "admin" && password === "tech2023") { // 登录成功处理 alert('登录成功!即将跳转至控制面板...'); window.location.href = '/task3.html'; } else { errorMsg.textContent = "工号或密码错误!"; errorMsg.style.display = 'block'; } }); </script> </body> </html>页面无法跳到同目录下task3.html
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值