js制作点击跳转页面

首先我们上效果图:

我们第一步制作html和css部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .wrep {
            width: 400px;
            height: 400px;
        }
        .top {
            width: 400px;
            height: 50px;
            line-height: 50px;
           background-color: aquamarine;
        }
        .tab {
            float: left;
            width: 80px;
            text-align: center;
        }
        .ta {
            background-color: rgb(0, 207, 17);
            color: #fff;
        }
        .tap {
            width: 400px;
            height: 350px;
            line-height: 350px;
            background-color: rgb(0, 207, 17);
            color: #fff;
            text-align: center;
        }

    </style> 
</head>
<body>
    <div class="wrep">
        <div class="top">
            <div class="tab ta">Tab1</div>
            <div class="tab">Tab2</div>
            <div class="tab">Tab3</div>
            <div class="tab">Tab4</div>
            <div class="tab">Tab5</div>
        </div>
        <div class="bottom">
            <div class="tap">这是第1个页面</div>
            <div class="tap">这是第2个页面</div>
            <div class="tap">这是第3个页面</div>
            <div class="tap">这是第4个页面</div>
            <div class="tap">这是第5个页面</div>
        </div>
    </div>
</body>
</html>

对跳转页面进行隐藏
并且使第一个页面显示

	   .tap {
            width: 400px;
            height: 350px;
            line-height: 350px;
            background-color: rgb(0, 207, 17);
            color: #fff;
            text-align: center;
            display: none;
        }
        .tap:first-of-type {
            display: block;
        }

接下来开始来开始写点击事件的部分:
首先获取top部分和bottom部分的class名

 	var tab = document.querySelectorAll(".tab");
    var tap = document.querySelectorAll(".tap");

利用for循环遍历top的每个内容,然后给每个内容添加点击事件

  for(let i=0;i<tab.length;i++){
        tab[i].addEventListener("click", function(){}
        }

在点击事件内添加top部分的css样式变换以及bottom部分的样式变换来达到点击切换的效果

    for(let i=0;i<tab.length;i++){
        tab[i].addEventListener("click", function(){
            this.classList.add('ta');
            tap[i].style.display = 'block';      
            }
        })
    }

最后使其他内容恢复原来的样式
我们用if语句做一个判断:

    for(let i=0;i<tab.length;i++){
        tab[i].addEventListener("click", function(){
            this.classList.add('ta');
            tap[i].style.display = 'block';
            for(let j=0; j<tap.length; j++){
                if(i !== j){
                    tab[j].classList.remove('ta');
                    tap[j].style.display = 'none'; 
                }
            }
        })
    }

以上就是利用js做的一个点击跳转页面的全部页面了。

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,关于登陆页面的制作以及跳转,我可以给您一些简单的指导。 首先,您需要设计一个页面,包括用户名、密码输入框和登录按钮。您可以使用HTMLCSS来创建这个页面,其中HTML用于结构,CSS用于样式设计。 接下来,您需要编写一些JavaScript代码来实现跳转功能。您可以在登录按钮的点击事件中编写代码,当用户点击登录按钮时,JavaScript代码会对输入的用户名和密码进行验证,如果验证通过,就跳转到您想要跳转的页面。 这里给您一个简单的示例代码供参考: ```html <!DOCTYPE html> <html> <head> <title>Login Page</title> <style type="text/css"> input[type=text], input[type=password] { padding: 10px; margin: 10px; border: 1px solid #ccc; border-radius: 4px; } input[type=submit] { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; } </style> </head> <body> <h2>Login Page</h2> <form> <label for="username">Username:</label> <input type="text" id="username" name="username"> <label for="password">Password:</label> <input type="password" id="password" name="password"> <input type="submit" value="Login" onclick="validate()"> </form> <script type="text/javascript"> function validate() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; if (username == "admin" && password == "admin123") { window.location.href = "welcome.html"; } else { alert("Invalid username or password!"); } } </script> </body> </html> ``` 在这个示例中,当用户输入用户名和密码并点击登录按钮时,JavaScript代码会验证用户名和密码是否正确,如果正确,就跳转到`welcome.html`页面。如果用户名或密码不正确,就弹出一个提示框,告诉用户输入的用户名或密码错误。 希望这个示例能够帮助您实现您的登录页面和跳转功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AR.K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值