JQuery Toggle not working as it should

前端 未结 5 758
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 17:08

I have a problem with JQuery Toggle.

I have the following code:

var $_ = jQuery;
$_(document).ready(function(){

$_(\"#sh-zone-buttons\").delegate(\"#sh-         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-23 17:21

    I've simplified it to:

    var $_ = jQuery;
    
    $_(document).ready(function() {
    
        $_("#sh-zone-buttons")
            .delegate("#sh-zone-button-login-menu", "click", function(event) {
    
            $_("#sh-zone-login-menu").toggle();
            $_(this).toggleClass("current");       
            return false;
    
        });
    
    });
    

    You don't need to define var elem_core if you are only going to use it once, so simply use $_(this).toggleClass("current");

    You don't need all that hide/show mess, simply use toggle(), as that's what it does for you.

    You don't need event.stopPropagation() and event.preventDefault(), simply do return false, as that does both.

提交回复
热议问题