如何在两个链接之间使用toggleClass方法

时间:2018-05-16 13:17:26

标签: javascript jquery html css

我正在开发一个SIGN-IN和SIGN-UP表单,我必须从signin链接切换到注册页面。因此,对于用户来说,为了知道他在哪个链接,我想在点击它时为该特定链接着色。所以我使用toggleClass但它不能正常工作。我在下面的代码中发布了代码。

这是jsfiddle link

HTML

 <div class="container">
  <div class="frame">
  <div class="nav">
  <ul>
  <li><a class="btn">Sign in</a></li>
  <li><a class="btn1">Sign up </a></li>
  </ul>
    </div>


  </div></div>

CSS

body {
margin:0;
background-image: url("/resources/images/BackGroundSoccer.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
    background-position: center; 
}

/* Style the tab */
.lol {
    overflow: hidden;

    background-color: #e23e36;
    width:25%;
    position:fixed;
    height:62px;
    z-index: 1;
}


.empty {
    display:inline-block;
    background-color: #e23e36;
    width:10%;
    text-align:center;
    margin-left:35%;
}
.shy {
    display:inline-block;
    background-color: #e23e36;
    width:15%;
    text-align:center;
    margin-left:20%;
}

.tab {

    background-color: #e23e36;
    width:100%;
    position:fixed;
    z-index: 1;
    margin-left:24.9%;
    height:62px;
}

.tablinks3 {
    color:white;
    font-size: 22px;
    font-family:"Arial Black", Gadget, sans-fseri;
    margin-left:24.9%;


}

.container {
  width: 100%;
  padding-top: 100px;
  padding-bottom: 100px;
}

.frame {
  height: 575px;
  width: 430px;
  background:
    linear-gradient(
    rgba(35,43,85,0.75),
    rgba(35,43,85,0.95));
  background-size: cover;
  margin-left: auto;
  margin-right: auto;
  border-top: solid 1px rgba(255,255,255,.5);
  border-radius: 5px;
  box-shadow: 0px 2px 7px rgba(0,0,0,0.2);
  overflow: hidden;
  transition: all .5s ease;

}
.nav {
    padding-top:40px;
}

li {

  padding-left: 10px;
  font-size: 18px;
  display: inline;
  text-align: left;
  text-transform: uppercase;
  padding-right: 10px;
  color: #ffffff;

}

.btn {
                cursor : pointer;
                color: rgb(255,255,255);
                padding: 10px;


            }
.btn1 {
            cursor : pointer;
                color: rgb(255,255,255);
                padding: 10px;
            }


.form-signin {
    padding-left:30px;
    padding-top:50px;
    padding-right:30px;
}

label {
  font-weight: 400;
  text-transform: uppercase;
  font-size: 20px;
  padding-left: 15px;
  padding-bottom: 10px;
  color: rgba(255,255,255,.7);
  display: block;
}

.form-styling {
  width: 100%;
  height: 35px;
  border: none;
  padding-left: 15px;
  border-radius: 15px;
  margin-bottom: 10px;
  background: rgba(255,255,255,.2);
  color :rgb(255,255,255);
}

.btn-animate {
  float: left;
  font-weight: 400;
  text-transform: uppercase;
  font-size: 20px;
  text-align: center;
  color: rgba(255,255,255, 1);
  padding-top: 8px;
  width: 100%;
  height: 35px;
    border: none;
    border-radius: 20px;
  margin-top: 30px;
  margin-bottom:20px;
  background-color: rgba(16,89,255, 1);
  left: 0px;
  top: 0px;
  cursor: pointer;
  transition: all .5s ease, top .5s ease .5s, height .5s ease .5s, background-color .5s ease .75s; 
}

.check
{
    font-size : 20px;
    padding-right: 10px;
    font-weight: 400;
  text-transform: uppercase;
  color: rgba(255,255,255,.7);
  padding-left :15px;

}

.forgot
{
    color: rgba(255,255,255,.7);
    font-size:15px;
    font-weight:400px;
    cursor:pointer;
    text-decoration:underline;
    padding-left : 165px;

}

:focus {outline: none;
}
.toggle {
    background-color: rgb(0,0,0);
}
.toggle1 {
    background-color: transparent;
}

JQuery的

$(document).ready(function(){
      $(".signin-show").show();
      $(".signup-show").hide();
      $(".btn").toggleClass('toggle');
        $(".btn1").click(function(){
            $(".signin-show").hide();
            $(".signup-show").show();
            $(".btn1").toggleClass('toggle');
            $(".btn").toggleClass('toggle1');
        });
        $(".btn").click(function(){
            $(".signin-show").show();
            $(".signup-show").hide();
            $(".btn").toggleClass('toggle');

        });

    });

我们将不胜感激。

2 个答案:

答案 0 :(得分:4)

您的代码可以归结为这三行代码

  1. 从所有元素中删除toggle
  2. 将切换类添加到单击的元素

    $(".btn").click(function(){
     $(".btn").removeClass("toggle")
     $(this).addClass("toggle")
    })
    
  3. 以下是snippet

答案 1 :(得分:2)

$('.button-wrapper a').on('click', function() {
  var buttonActive = $(this).hasClass('active');
  // Remove the active class from all elements.
  $('.button-wrapper a').removeClass('active');
  // Add active state if this clicked button doesn't have.
  if (!buttonActive) {
    $(this).addClass('active');
  }
});
div {
  margin-top: 20px;
}

a {
  padding: 10px;
  border: 1px solid black;
  text-decoration: none;
}

a.active {
 background-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button-wrapper">
  <a href="#">Button 1</a>
  <a href="#">Button 2</a>
</div>