简体   繁体   English

jQuery与主开关切换

[英]jquery toggle with a master switch

I am trying to use jquery toggling to show and hide features of a specific product. 我正在尝试使用jquery切换来显示和隐藏特定产品的功能。 I have it working, however it's not perfect and wondered if anyone could help please? 我可以使用它,但是它并不完美,想知道是否有人可以帮助您?

Basically what I'm having problems with is that when you use the master open all and then close all of the individual items on their own, I need the master switch to revert back to show all text. 基本上,我遇到的问题是,当您使用母版打开全部然后自己关闭所有单个项目时,我需要使用母版开关来还原以显示所有文本。

In addition I want to have a + and - icon on each of the items but can't figure out how to only replace the clicked image and not all of them in the list! 另外,我希望每个项目上都有一个+和-图标,但无法弄清楚如何仅替换单击的图像,而不替换列表中的所有图像!

Any help would be greatly appreciated, thanks. 任何帮助将不胜感激,谢谢。

Script 脚本

$(document).ready(function() {
    $('.toggle').hide();
    $('.toggler').click(function() {
        var target = this.id + '_content';
        var imgtarget = this.id + '_expand';
        $('#' + target).slideToggle();
        $('.toggleall').text('Hide all');
        $('<img src="images/collapse.gif">').prependTo('.toggleall');
    });

    $('.toggleall').click(function() {
        if ($('.toggle').is(':visible')) {
            $('.toggle').slideUp();
            $('.toggleall').text('Show all');
            $('<img src="images/expand.gif">').prependTo('.toggleall');
        } else {
            $('.toggle').slideDown();
            $('.toggleall').text('Hide all');
            $('<img src="images/collapse.gif">').prependTo('.toggleall');
        }
    });
});

Html HTML

<div class="toggleall"><img src="images/expand.gif">Show all</div>
        <br><br>
        <div class="toggler" id="toggle1"><img src="images/expand.gif" class="toggle1_expand">Toggle 1</div>
        <div class="toggle" id="toggle1_content">only toggle1</div>
        <div class="toggler" id="toggle2"><img src="images/expand.gif" class="toggle2_expand">Toggle 2</div>
        <div class="toggle" id="toggle2_content">only toggle2</div>
        <div class="toggler" id="toggle3"><img src="images/expand.gif" class="toggle3_expand">Toggle 3</div>
        <div class="toggle" id="toggle3_content">only toggle3</div>

Here is the jfiddle of the code (thanks François Wahl): jsfiddle.net/GUYfG 这是代码的jfiddle(感谢FrançoisWahl): jsfiddle.net/GUYfG

若要展开折叠,可以在DIV中切换具有不同背景图像的类,或使用无序列表(UL / LI)。

Here is the working version in a proper format :- 这是正确格式的工作版本:-

$(document).ready(function() {

   $('.toggle').hide();

   $('.toggler').click( function() {
      var target = this.id + '_content';
      var imgtarget = this.id + '_expand';
          $('#' + target).slideToggle(function(){
                if( $('.toggle').is(':visible') ) {
                  $('.toggleall').text('Hide all');
                  $('<img src="images/collapse.gif">').prependTo('.toggleall');
              } else {
                  $('.toggleall').text('Show all');
                  $('<img src="images/expand.gif">').prependTo('.toggleall');
              }
          });

          if( $('.toggle').is(':visible') ) {
              $('.toggleall').text('Hide all');
          }
    });

    $('.toggleall').click(function() {
         if ($('.toggle').is(':visible')) {
                $('.toggle').slideUp();
                $('.toggleall').text('Show all');
                $('<img src="images/expand.gif">').prependTo('.toggleall');
            } else {
                $('.toggle').slideDown();
                $('.toggleall').text('Hide all');
                $('<img src="images/collapse.gif">').prependTo('.toggleall');
            }
     });

 });

EDIT: 编辑:

Here is the fiddle 这是小提琴

I have edited the code check now. 我已经编辑了代码检查。 Also check the fiddle. 还要检查小提琴。

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

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