【问题标题】:Expand all and collapse all in jQuery not working as per requirement在jQuery中展开全部并全部折叠不按要求工作
【发布时间】:2014-09-04 02:40:19
【问题描述】:

我是 jQuery 新手,我在下面创建了一个页面,并具有用于展开折叠的 jquery 功能。除了以下事实之外,一切都运行良好展开,我正在展开所有然后它正在展开其余的 div,但折叠已经展开的元素,反之亦然。请建议如何解决这个问题。谢谢。

javascript:

    $(function(){
          $('[id^=hideAns],[id^=showAns]').click(function(){
                $(this).hide();
                $(this).siblings().show();
                $(this).closest('.qbox').find('[id^=ans]').toggle(500);
          });
        });

    $(function(){
          $('[id^=showall],[id^=hideall]').click(function(){
                $(this).hide();
                $(this).siblings().show();
                $('.qbox').find('[id^=ans]').toggle(500);
                $('.qbox').find('.icons').toggle();
          });
        });

html:

<form name="contenttemp" id="contenttemp" method="post">

<div id="contentDiv" class="wrapperBox"><br>
<span class="showHideTxt" id="showall" style="float:right; cursor:pointer;">Show all</span>
<span class="showHideTxt" id="hideall" style="float:right; display:none; cursor:pointer;">Hide all</span><br>


    <div class="qbox">

        <span class="qclass">Q. No.1)
            <img id ="showAns" class="icons" src="img/expand.png" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns" class="icons" src="img/collapse.png" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <span class="qclass">This is test question 109</span>
        <hr />
        <span style="display:none;"  id="ans">This is test answer 109</span>
    </div>
    <br>

    <div class="qbox">

        <span class="qclass">Q. No.2)
            <img id ="showAns" class="icons" src="img/expand.png" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns" class="icons" src="img/collapse.png" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <span class="qclass">This is test question 108</span>
        <hr />
        <span style="display:none;"  id="ans">This is test answer 108</span>
    </div>
    <br>

    <div class="qbox">

        <span class="qclass">Q. No.3)
            <img id ="showAns" class="icons" src="img/expand.png" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns" class="icons" src="img/collapse.png" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <span class="qclass">This is test question 105.     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
        <hr />
        <span style="display:none;"  id="ans">This is test answer 105</span>
    </div>
    <br>

</div>
</form>

【问题讨论】:

  • 您多次使用相同的 id 值。停止。改用类
  • @Anthony :我知道,但我添加了一个生成的 HTML,它是由结果集创建的。但您的建议不会解决问题。
  • 另外,您使用 span 并不是最佳选择,并且会迫使您过度样式化,最好使用控件的锚点和 CSS 来覆盖图像。我会为 html5 中的摘要/详细信息元素使用定义列表或 polyfil:mathiasbynens.be/notes/html5-details-jquery
  • @Anoop Joshi:这是小提琴jsfiddle.net/aJvUg/2
  • 说你不能解决这个问题,因为它已经坏了,这会让你有点卡住。即使你解决了这个问题,它也可能有其他问题。数据模型越复杂,问题就越复杂。

标签: javascript jquery html


【解决方案1】:

使用.hide().show() 比使用.toggle() 更好...

    $(function(){
      $('[id^=showAns]').click(function(){
            $(this).hide();
            $(this).siblings().show();
            $(this).closest('.qbox').find('[id^=ans]').show(500);
      });
     $('[id^=hideAns]').click(function(){
            $(this).hide();
            $(this).siblings().show();
            $(this).closest('.qbox').find('[id^=ans]').hide(500);
      });
     $('[id^=showall]').click(function(){
            $(this).hide();
            $(this).siblings().show();
            $('.qbox').find('[id^=ans]').show(500);
            $('.qbox').find('[id^=showAns]').hide();
            $('.qbox').find('[id^=hideAns]').show();
      });
    $('[id^=hideall]').click(function(){
            $(this).hide();
            $(this).siblings().show();
            $('.qbox').find('[id^=ans]').hide(500);
            $('.qbox').find('[id^=showAns]').show();
            $('.qbox').find('[id^=hideAns]').hide();
      });
    });

请检查..jsfidle

这可能是你的答案,你正在寻找什么..!!

【讨论】:

  • 谢谢。这就是我一直在寻找的。​​span>
猜你喜欢
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多