jQuery click函数仅在页面上显示第一个值

时间:2018-10-22 09:24:26

标签: php jquery

我的jQuery代码有问题,我使用“显示”和“隐藏”按钮来显示/隐藏类,但是jQuery仅适用于我的第一个类。我的代码是:

$(document).ready(function(){
  $("#hide").click(function(){
    $(".menu_sub_index").hide(500);
  });
  $("#show").click(function(){
    $(".menu_sub_index").show(500);
  });
});
<div class="danhmuc">
  <button id="hide">&#9940;</button>
  <button id="show">Category</button>
</div>

有任何改进我的代码的建议吗?谢谢!

2 个答案:

答案 0 :(得分:0)

假设您的html元素如下所示。

<div class="menu_sub_index">Div Content</div>

<div class="danhmuc">
  <button id="hide">&#9940;</button>
  <button id="show">Category</button>
</div>

点击功能

$(document).ready(function(){
    $("#hide").click(function(){
        $(".menu_sub_index").hide();
    });
    $("#show").click(function(){
        $(".menu_sub_index").show();
    });
});

答案 1 :(得分:0)

尝试

 $(document).ready(function(){
   $("#hide").on("click", function(e){
    $(".menu_sub_index").hide();
   });

   $("#show").on("click", function(e){
    $(".menu_sub_index").show();
    });
});
<div class="danhmuc">
 <button id="hide">&#9940;</button>
 <button id="show">Category</button>
 </div>