简体   繁体   English

jQuery显示隐藏多个Div不起作用

[英]jQuery show hide multiple Div not work in action

I have this jQuery code for show/hide multiple div in my page: 我的页面中有用于显示/隐藏多个div的jQuery代码:

$(function() {
    $('#toggle').click(function(event) {
        event.preventDefault();
        var target = $(this).attr('href');
        $(target).toggleClass('hidden show');
    });
});
<span class="add-btn">
    <a href="#options" id="toggle">
        <span class="btn btn-primary btn-sm">
            <i class="fa fa-filter"></i>
            Filter
        </span>
    </a>
</span>
<div id="options" class="well hidden">hehe</div>    
<span class="add-btn">
    <a href="#options2" id="toggle">
        <span class="btn btn-primary btn-sm">
            <i class="fa fa-filter"></i>
            Filter2
        </span>
    </a>
</span>
<div id="options2" class="well hidden">hoho</div>
.show {
    display: block !important;
}
.hidden {
    display: none !important;
    visibility: hidden !important;
}

But in Action Not work! 但是在行动中不起作用! how do fix this?! 如何解决这个问题?

Demo 演示版

You need to make your id triggers unique identifiers, they can't be the same. 您需要使id触发器具有唯一的标识符,它们不能相同。 The code will only evaluate the first instance and ignore any others. 该代码只会评估第一个实例,而忽略其他任何实例。

$(function() {

       $('#toggle1, #toggle2').click(function(event) {
         event.preventDefault();
         var target = $(this).attr('href');
         $(target).toggleClass('hidden show');
       });
});

Example: 例:

  $(function() { $('#toggle1, #toggle2').click(function(event) { event.preventDefault(); var target = $(this).attr('href'); $(target).toggleClass('hidden show'); }); }); 
 .show { display: block !important; } .hidden { display: none !important; visibility: hidden !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="add-btn"><a href="#options" id="toggle1"> <span class="btn btn-primary btn-sm"><i class="fa fa-filter"></i>Filter</span></a> </span> <div id="options" class="well hidden">hehe</div> <span class="add-btn"><a href="#options2" id="toggle2"> <span class="btn btn-primary btn-sm"><i class="fa fa-filter"></i>Filter2</span></a> </span> <div id="options2" class="well hidden">hoho</div> 

You can not assign the same id, use class instead: 您不能分配相同的ID,请改用class:

HTML 的HTML

<span class="add-btn"><a href="#options" class="toggle">
          <span class="btn btn-primary btn-sm"><i class="fa fa-filter">     </i>Filter</span></a>
 </span>

<div id="options" class="well hidden">hehe</div>

<span class="add-btn"><a href="#options2" class="toggle">
          <span class="btn btn-primary btn-sm"><i class="fa fa-filter">     </i>Filter2</span></a>
</span>

<div id="options2" class="well hidden">hoho</div>

JS JS

 $(function() {

   $('.toggle').click(function(event) {
     event.preventDefault();
     var target = $(this).attr('href');
     $(target).toggleClass('hidden show');
   });
})

CSS 的CSS

.show {
    display: block !important;
}
.hidden {
    display: none !important;
    visibility: hidden !important;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM