2017-08-07 66 views
0

在我看來,我做錯了什麼。所以我做了一個由JS代碼操作的下拉菜單。JQuery .hasClass()不工作?

所以它切換綠色類,這使得div塊。現在我想通過再次點擊相同的按鈕關閉下拉菜單。我正在驗證是否有通過hasClass()方法切換綠色類。但它總是返回false。總是。爲什麼?

JFiddle:https://jsfiddle.net/bg4ev09k/3/ 實際原始代碼:

$('#loginform').click(function(){ 
 
    var container = $(".login"); 
 
    if ($(this).hasClass('green')) 
 
    { 
 
    console.log("ere"); 
 
    container.hide(); 
 
    $(this).removeClass('green'); 
 
    } else { 
 
    $('.login').fadeToggle('slow'); 
 
    $(this).toggleClass('green'); 
 
    } 
 
}); 
 

 
$(document).mouseup(function (e) 
 
{ 
 
    var container = $(".login"); 
 

 
    if (!container.is(e.target) // if the target of the click isn't the container... 
 
     && container.has(e.target).length === 0) // ... nor a descendant of the container 
 
    { 
 
     container.hide(); 
 
     $('#loginform').removeClass('green'); 
 
    } 
 
});
<h2><a href="#" id="loginform"><i class="fa fa-sign-in" aria-hidden="true"></i> Enter</a></h2> 
 
<div class="login"> 
 
    <div class="arrow-up"></div> 
 
    <div class="formholderauth"> 
 
    <div class="randompad"> 
 
     <h1>Auth</h1> 
 
     <form action="core/auth/login.php" method="POST"> 
 
     <p> 
 
      <input type="text" name="login" placeholder="Логин..." value="<?php echo @$data['login']; ?>"> 
 
     </p> 
 
     <p> 
 
      <input type="password" name="password" placeholder="Пароль..."> 
 
     </p> 
 
     <p> 
 
      <button type="submit" name="do_login">Войти</button> 
 
     </p> 
 
     </form> 
 
     <p><a href="/core/auth/signup.php"><i class="fa fa-user-plus" aria-hidden="true"></i> Register</a></p> 
 
    </div> 
 
    </div> 
 
</div>

enter image description here

+0

請提供相關的HTML,以便我們重現您的情況。 –

+3

更好的是,在jsfiddle或codepen.io上重現 – Difster

+2

爲什麼在其他條件下創建'removeClass'時不會'addClass' – Sysix

回答

-1

這種 「破壞」 邪惡的代碼太:)

$(document).on('click','#loginform',function(){ 
 
    var container = $(".login"); 
 
    if ($(this).hasClass('green')){ 
 
    container.stop().fadeToggle('slow'); 
 
    $(this).removeClass('green'); 
 
    } else { 
 
    container.stop().fadeToggle('slow'); 
 
    $(this).addClass('green'); 
 
    } 
 
}); 
 

 
// evil code 
 
$('body').prepend('<div id="loginform"></div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="#" class="green" id="loginform"><i>☺</i> Click me</a> 
 
<div class="login">LOGIN CONTAINER</div>

希望這會有所幫助!

更新

對不起,我的帖子..我是草率的。

與所有的JavaScript代碼我發現這個問題

$(document).mouseup(function (e){ 
    var container = $(".login"); 

    if (!container.is(e.target) // if the target of the click isn't the container... 
    && container.has(e.target).length === 0) // ... nor a descendant of the container 
    { 
     container.hide(); 
     $('#loginform').removeClass('green'); 
    } 
}); 

這將與此復位綠色類

$(document).on('click','#loginform',function(){ 
    var container = $(".login"); 
    if ($(this).hasClass('green')){ 
     container.stop().fadeToggle('slow'); 
     $(this).removeClass('green'); 
    } else { 
     container.stop().fadeToggle('slow'); 
     $(this).addClass('green'); 
    } 
}); 

因此,通過簡單的增加&& !$('#loginform').is(e.target)衝突您解決問題

這裏是代碼

$('button[type="submit"]').mousedown(function(){ 
 
    $(this).css('background', '#2ecc71'); 
 
}); 
 

 
$('button[type="submit"]').mouseup(function(){ 
 
    $(this).css('background', '#1abc9c'); 
 
}); 
 

 
$(document).on('click','#loginform',function(){ 
 
    var container = $(".login"); 
 
    if ($(this).hasClass('green')){ 
 
    container.stop().fadeToggle('slow'); 
 
    $(this).removeClass('green'); 
 
    } else { 
 
    container.stop().fadeToggle('slow'); 
 
    $(this).addClass('green'); 
 
    } 
 
}); 
 

 

 
$(document).mouseup(function (e) 
 
{ 
 
    var container = $(".login"); 
 

 
    if (!container.is(e.target) // if the target of the click isn't the container... 
 
    && container.has(e.target).length === 0 
 
    && !$('#loginform').is(e.target) 
 
    ) // ... nor a descendant of the container 
 
    { 
 
     container.hide(); 
 
     $('#loginform').removeClass('green'); 
 
    } 
 
});
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    background: #2E3192; 
 
    background: -webkit-linear-gradient(left top, #2E3192, #1BFFFF); 
 
    background: -o-linear-gradient(bottom right, #2E3192, #1BFFFF); 
 
    background: -moz-linear-gradient(bottom right, #2E3192, #1BFFFF); 
 
    background: linear-gradient(to bottom right, #2E3192, #1BFFFF); 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    font-family: OpenSans-Regular; 
 
} 
 

 
.menu { 
 
    width: 100%; 
 
    height: 40px; 
 
    line-height: 40px; 
 
    background-color: #34495e; 
 
    padding-left: 20px; 
 
    padding-right: 20px; 
 
    position:absolute; 
 
    bottom:0; 
 
} 
 

 
.menu h2 { 
 
    text-align: right; 
 
} 
 

 
.menu h2 a { 
 
    color: #ecf0f1; 
 
    text-decoration: none; 
 
} 
 

 
.menu h2 a:hover { 
 
    color: #1abc9c; 
 
} 
 

 
.auth { 
 
    float: right; 
 
} 
 

 
.signup { 
 
    margin: auto; 
 
    width: 50%; 
 
} 
 

 
.login { 
 
    position: relative; 
 
    display: none; 
 
} 
 

 
.arrow-up { 
 
    width: 0; 
 
    height: 0; 
 
    border-left: 20px solid transparent; 
 
    border-right: 20px solid transparent; 
 
    border-bottom: 15px solid white; 
 
    right: 0; 
 
    position: absolute; 
 
    top: -10px; 
 
} 
 

 
.reg { 
 
    width: 400px; 
 
} 
 

 
/*Auth*/ 
 

 
.formholderauth { 
 
    background: white; 
 
    width: 300px; 
 
    border-radius: 5px; 
 
    padding-top: 5px; 
 
} 
 

 
.formholderauth input[type="text"], .formholderauth input[type="password"] { 
 
    padding: 7px 5px; 
 
    margin: 10px 0; 
 
    width: 100%; 
 
    display: block; 
 
    font-size: 16px; 
 
    border-radius: 5px; 
 
    border: none; 
 
    -webkit-transition: 0.3s linear; 
 
    -moz-transition: 0.3s linear; 
 
    -o-transition: 0.3s linear; 
 
    transition: 0.3s linear; 
 
    background-color: #ececec; 
 
    box-shadow: 0 0 1px 1px #A7A9A9; 
 
} 
 

 
.formholderauth input[type="text"]:focus, .formholderauth input[type="password"]:focus { 
 
    outline: none; 
 
    box-shadow: 0 0 1px 1px #1abc9c; 
 
} 
 

 
.formholderauth button[type="submit"] { 
 
    background: #1abc9c; 
 
    padding: 6px; 
 
    font-size: 20px; 
 
    display: block; 
 
    width: 70%; 
 
    margin: auto; 
 
    border: none; 
 
    color: #fff; 
 
    border-radius: 5px; 
 
} 
 

 
.formholderauth button[type="submit"]:hover { 
 
    background: #1bc6a4; 
 
} 
 

 
.formholderauth p { 
 
    margin-bottom: 13px; 
 
    text-align: center; 
 
} 
 

 
.formholderauth p:last-child { 
 
    margin-bottom: 0px; 
 
} 
 

 
.formholderauth a { 
 
    color: #1abc9c; 
 
    text-decoration: none; 
 
} 
 

 
.formholderauth a:hover { 
 
    color: 
 
    color: #10EE30; 
 
} 
 

 
.randompad { 
 
    padding: 10px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h2><a href="#" id="loginform"><i class="fa fa-sign-in" aria-hidden="true"></i> Enter</a></h2> 
 
<div class="login"> 
 
    <div class="arrow-up"></div> 
 
    <div class="formholderauth"> 
 
     <div class="randompad"> 
 
      <h1>Auth</h1> 
 
      <form action="login.php" method="POST"> 
 
       <p> 
 
        <input type="text" name="login" placeholder="Login..."> 
 
       </p> 
 
       <p> 
 
        <input type="password" name="password" placeholder="Password..."> 
 
       </p> 
 
       <p> 
 
        <button type="submit" name="do_login">Auth</button> 
 
       </p> 
 
      </form> 
 
      <p><a href="signup.php"><i class="fa fa-user-plus" aria-hidden="true"></i> Reg</a></p> 
 
     </div> 
 
    </div> 
 
</div>

我還會急於道歉。祝你有個美好的一天

+4

請添加對問題的解釋以及如何解決問題,而不僅僅是將代碼傾倒在我們身上。 – Barmar

+3

沒有OP的HTML,我們不能說你的答案有用或沒有。 –