vscode实现登录界面的跳转

一.需要软件:VSCODE

二.制作一个简单的登陆界面,详细说明见:https://blog.csdn.net/l7_804829222/article/details/137770922?spm=1001.2014.3001.5502

css源码如下:

<style>
  p {
    font-size: 40px;
    text-align: center;
    margin: auto;
  }

  label {
    font-size: 15px;
  }

  input {
    height: 25px;
    width: 250px;
    margin-top: 5px;
  }

  .sel {
    margin-top: 10px;
    display: block;
    font-size: 13px;
  }

  .sel input {
    width: 10px;
    height: 10px;
    margin: 2px;
  }

  a {
    color: blue;
    text-decoration: none;
  }

  a:hover {
    cursor: pointer;
    color: rgb(55, 85, 138);
  }

  button {
    width: 200px;
    height: 45px;
    margin-left: 70px;
    font-size: large;
    background-color: rgb(88, 84, 156);
    color: #fff;
  }

  button:hover {
    cursor: pointer;
    background-color: rgb(94, 123, 174);
  }

  #vcode {
    height: 35px;
    width: 150px;
    font-size: 15px;
    border-radius: 5px;
    border: 1;
    padding-left: 8px;
  }

  #code {
    color: #ffffff;
    background-color: #000000f7;
    font-size: 20px;
    padding: 5px 35px 10px 35px;
    margin-left: 5%;
    cursor: pointer;
  }

  #search_pass_link {
    width: 70%;
    text-align: right;
    margin: 0 auto;
    padding: 5px;
  }

  .btns {
    width: 30%;
    margin-left: 13%;
    height: 40px;
    border: 0;
    font-size: 14pt;
    cursor: pointer;
    border-radius: 20px;
    border: 0;
  }
</style>

界面主要代码:

<body style="
      background: url(1.png);
      background-repeat: no-repeat;
      background-size: 100%;
    " leftmargin="0" onload="changeImg()">
  <div style="
        background-color:rgb(245, 243, 243,0.7);
        margin: auto;
        height: 350px;
        width: 450px;
        margin-top: 200px;
      ">
    <p>登录</p>
    <div style=" height: 250px; width: 350px;margin: auto;">
      <form action="">
        <label for="user">用户名/username:</label><br />
        <input type="text" name="user" id="user"><br>
        <label for="password">密码/password:</label><br />
        <input type="password" name="password" id="password">
      </form>
      <a href="">忘记密码?</a><br />
      <div class="main_bar">
        <form action="www.baidu.com" onsubmit="check()">
          <input type="text" id="vcode" placeholder="验证码" value="验证码" onfocus="this.value=''"
            onblur="if(this.value=='')this.value='验证码'" /><span id="code" title="看不清,换一张"></span>
          <div id="search_pass_link">
          </div>
        </form>
      </div>
      <a href="#">登录相关服务条款和隐私政策</a><br />
      <label for="" class="sel"><input type="checkbox" />我已阅读并同意服务提条款和隐私政策</label><br />
      <button onclick="login()">登录</button>
    </div>
  </div>

此时运行出来的界面:

如图可以看出,验证码未显示,以及登录无法跳转。此时我们用到Javascript。

三.简介

JavaScript是一门解释型语言,所谓解释型指语言是指不需要被编译为机器码在执行,而是直接浏览器|node中执行。
JavaScript 组成:

四.验证码

当验证码输入错误,提示界面如图:

源码:

  <script type="text/javascript">
    var code; //声明一个变量用于存储生成的验证码
    document.getElementById("code").onclick = changeImg;
    function changeImg() {
      var arrays = new Array(
        '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X', 'Y', 'Z'
      );
      code = '';
      for (var i = 0; i < 4; i++) {
        var r = parseInt(Math.random() * arrays.length);
        code += arrays[r];
      }
      document.getElementById('code').innerHTML = code;
    }
    function check() {
      var input_code = document.getElementById('vcode').value;
      if (input_code.toLowerCase() == code.toLowerCase()) {
        return true;
      }
        alert("请输入正确的验证码!");
        return false;
      }
    </script>

五.登录跳转

源码:

 <script>
    function login() {
      var user_value = document.querySelector('#user').value;
      var password_value = document.querySelector('#password').value;
      if (user_value == '123' && password_value == '123') {
        alert('登录成功');
       location.href = "https://www.baidu.com";
      } 
      else {
        alert('用户名或密码错误!!!')
      }
    }
  </script>
注意:实现页面跳转,使用location.href属性可以设置或获取当前页面的URL。通过将其值设置为新的URL,可以实现页面跳转。

当密码输入错误时的界面:

当输入正确密码时,直接跳转:

六.总结

学习页面跳转的时候,摸索了挺久,刚开始认为a标签实现超链接,加上密码输入正确的前提条件可行,但后面运行不起来。最后找到了这种简单的方法:使用location.href属性。

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的手写登录界面的 HTML 和 JavaScript 代码示例: ```html <!DOCTYPE html> <html> <head> <title>Login</title> <meta charset="utf-8"> </head> <body> <h2>Login</h2> <form> <label for="username">Username:</label> <input type="text" id="username" name="username" required><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required><br><br> <input type="submit" value="Login"> </form> <script> // 获取表单元素 const form = document.querySelector('form'); const usernameInput = document.querySelector('#username'); const passwordInput = document.querySelector('#password'); // 监听表单提交事件 form.addEventListener('submit', (event) => { event.preventDefault(); // 阻止表单默认提交行为 // 获取输入的用户名和密码 const username = usernameInput.value; const password = passwordInput.value; // TODO: 进行登录验证 if (username === 'admin' && password === '123456') { // 登录成功,到主页 window.location.href = 'home.html'; } else { // 登录失败,提示错误信息 alert('Incorrect username or password.'); } }); </script> </body> </html> ``` 在上面的代码中,我们创建了一个简单的表单,包含了用户名和密码的输入框,以及一个登录按钮。在 JavaScript 部分,我们监听了表单的提交事件,当用户点击登录按钮时,会阻止表单默认的提交行为,而是进行自定义的登录验证。如果用户名和密码正确,就会到主页 `home.html`,否则会提示错误信息。 需要注意的是,这里的登录验证只是一个示例,实际情况下需要更加严谨的验证方式来确保用户信息的安全性。另外,这里的方式是通过修改 `window.location.href` 来实现的,也可以使用其他方式来实现,比如 JavaScript 的 `location.replace()` 或者 HTML 的 `<meta>` 标签等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值