【问题标题】:Jquery 2 click Function not working as expectedJquery 2单击功能未按预期工作
【发布时间】:2015-07-20 11:33:50
【问题描述】:

我试图隐藏和显示我的页脚导航栏,但我想更改 V 形图标,当导航栏隐藏 V 形向上显示时,当导航栏显示 V 形向下显示,因此我尝试删除一个类 .bottom-action 和添加一个类 .bottom-action2 并单击第二个类时反转所有设置,但它没有按预期工作。

$(document).ready(function(){
    $(".bottom-action").click(function(){
        $(".bottom-navbar").slideToggle("slow");
       $(".down").css("display","none");
       $(".up").css("display","block");
       $(".bottom-action").removeClass('bottom-action').addClass("bottom-action2");
         
    });

$(".bottom-action2").click(function(){
        $(".bottom-navbar").slideToggle("slow");
       $(".up").css("display","none");
       $(".down").css("display","block");
       $(".bottom-action2").removeClass('bottom-action2').addClass("bottom-action");
         
    });



});
.bottom-navbar{
  position: fixed;
width: 100%;
bottom: 0px;
left: 0px;
background: #E4E4E4;
padding-top: 3px;
border-top: 1px solid #C9C9C9;
z-index: 7000;
}

.bottom-navbar .item{

  margin-left: 22px;
margin-right: 22px;
display: inline-block;
border-bottom: 2px solid transparent;
cursor: pointer;
}

.bottom-action{position: fixed;
bottom: 0px;
right: 10%;
background-color: #EEE;
color: #000;
z-index: 7002;
background: transparent;

}

.bottom-action2{position: fixed;
bottom: 0px;
right: 10%;
background-color: #EEE;
color: #000;
z-index: 7002;
background: transparent;

}

.bottom-action .down{}
.bottom-action .up{display: none;}
<head>
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>

<div class="bottom-navbar">
    <div class='container'><div class='item'>Compare</div></div>
</div>

<div class='bottom-action'><span class='glyphicon glyphicon-chevron-down down'></span> <span class='glyphicon glyphicon-chevron-up up'></span></div>

【问题讨论】:

    标签: javascript jquery html css twitter-bootstrap


    【解决方案1】:

    试试:

    $(document).on('click', '#myButton', function(e){
        //jour code
    });
    

    而不是:

    $('myButton').click(...)
    

    【讨论】:

      【解决方案2】:

      问题在于,在绑定事件时,您的 DOM 中没有 bottom-action2 元素。 有趣的是,这会起作用:

      $(".bottom-action").click(function(){
        if($(this).hasClass("bottom-action")){
            // do one thing
        }
        else if if($(this).hasClass("bottom-action2")){
           // do the other thing
        }
      }
      

      但正如@Béranger 指出的那样,使用更高级别的处理程序将是更清洁的解决方案。在你的情况下,像

      $(document).on('click', '.bottom-action', function(e){
         // do one thing
      });
      
      $(document).on('click', '.bottom-action2', function(e){
         // do the other thing
      });
      

      【讨论】:

      • 投了赞成票
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2020-04-28
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      相关资源
      最近更新 更多