简体   繁体   English

使用JQuery切换多个DIV:包含的代码

[英]Toggling Multiple DIVs with JQuery: Codes Included

I need help toggling multiple divs with the codes below. 我需要以下代码来帮助切换多个div。 It works well with one div but I will multiple divs would be looped with php so it will be impossible to target each loop to give unique IDs or classes. 它可以很好地与一个div一起工作,但是我将用php循环多个div,因此不可能将每个循环作为目标以提供唯一的ID或类。

Below are the codes, please help out with the correct JQuery 以下是代码,请提供正确的JQuery帮助

function clickHandler() {
$('#hide').toggle();
$('#show').toggle();
$('.more-content').toggle('slow');

}

$(document).ready(function(){
    $('#hide, .more-content').hide();

    $('#hide, #show').on('click', clickHandler);
});

HTML HTML

<div class="more-content">
      <!--These are the contents-->               
</div>

<button id="hide" class="more_arrow">Collapse <span class="fa fa-chevron-up"></span></button>

<button id="show" class="more_arrow">Expand <span class="fa fa-chevron-down"></span></button>

When working with repeating components you don't want to use ID's you want to use common classes for common elements. 使用重复组件时,您不想使用ID,而要对公共元素使用公共类。 Then you use an outer container for each repeating "module". 然后,对每个重复的“模块”使用外部容器。

Then you can isolate components within each "module" by only searching within that module instance 然后,您可以通过仅在该模块实例中进行搜索来隔离每个“模块”中的组件

HTML - can repeat as many <article> as you want, the script will work for all instances independently HTML-可以根据需要重复任意多个<article> ,该脚本将独立地适用于所有实例

<article>
    <div class="content"></div>
    <div class="more-content"></div>
    <button class="hide" class="more_arrow">Collapse</button>
    <button class="show" class="more_arrow">Expand</button>
</article>

JS JS

$('.hide, .more-content').hide();// should be done with css not js

$('.hide, .show').on('click', clickHandler);

function clickHandler() {
   $(this).toggle()
           .siblings('.hide, .show')
           .toggle()
           // only toggle more-content within this instance of <article>
           .parent().find('.more-content')
           .toggle('slow');
}

You must give to your divs the same class, in example 'my-class' and hide or show them depend on button clicked. 您必须给div相同的类,例如“ my-class”,然后根据单击的按钮隐藏或显示它们。

$(document).ready(function(){
    $('#hide').on('click', function(){
         // Hide all elements which have my-class class
         $('.my-class').hide();
    });

    $('#show').on('click', function(){
         // Show all elements which have my-class class
         $('.my-class').show();
    });
});

You can too get elements by attribute value. 您也可以通过属性值获取元素。 In example by id value 以ID值为例

$('[id=test]')

If you give to your elements ids prefix, you should be able to get all of them by this expression 如果给元素ids前缀,则应该可以通过此表达式获取所有元素

$('[id^=test]')

By changing ^ mark to $ you can search elements with specified postfix 通过将^标记更改为$,可以搜索具有指定后缀的元素

You can target the currently clicked div using $(this) within the handler, so 您可以在处理程序中使用$(this)定位当前单击的div,因此

function clickHandler() { 
   $(this).toggle(); 
 }

Will toggle the currently clicked div. 将切换当前单击的div。

You can toggle a nearby div to the one clicked using this 您可以使用此将附近的div切换为点击的div

 $(this).nearest('.someclass').toggle(); 

Use classes where you have repeating elements you want to access. 在要访问重复元素的地方使用类。

Try this: 尝试这个:

jsFiddle Demo jsFiddle演示

First, surround each content DIV + buttons in a container DIV. 首先,将每个内容DIV +按钮包围在容器DIV中。

Then, use jQuery traversal methods siblings() and prevAll() to toggle the appropriate DIVs. 然后,使用jQuery遍历方法siblings()prevAll()来切换适当的DIV。


HTML: HTML:

<div class="container">
    <div class="more-content">
          Once upon a midnight dreary, while I pondered, weak and weary, Over many a quaint and curious volume of forgotten lore — While I nodded, nearly napping, suddenly there came a tapping, As of some one gently rapping, rapping at my chamber door. "'Tis some visiter," I muttered, "tapping at my chamber door — Only this and nothing more."
    </div>
    <button class="hide more_arrow">Collapse <span class="fa fa-chevron-up"></span></button>
    <button class="show more_arrow">Expand <span class="fa fa-chevron-down"></span></button>
</div>
<div class="container">
    <div class="more-content">
          The quick brown fox jumped. The quick brown fox jumped. The quick brown fox jumped. The quick brown fox jumped. The quick brown fox jumped. The quick brown fox jumped. 
    </div>
    <button class="hide more_arrow">Collapse <span class="fa fa-chevron-up"></span></button>
    <button class="show more_arrow">Expand <span class="fa fa-chevron-down"></span></button>
</div>
<div class="container">
    <div class="more-content">
          Whatsoever things are true, whatsoever things are honest, whatsoever things are just, whatsoever things are pure, whatsoever things are lovely, whatsoever things are of good report; if there be any virtue, and if there be any praise, think on these things. 
    </div>
    <button class="hide more_arrow">Collapse <span class="fa fa-chevron-up"></span></button>
    <button class="show more_arrow">Expand <span class="fa fa-chevron-down"></span></button>
</div>

jQuery: jQuery的:

$(document).ready(function(){
    $('#hide, .more-content').hide();

    $('#hide, #show').on('click', function(){
        $(this).prevAll('.more-content').toggle('slow');
        $(this).siblings('button').toggle();
        $(this).toggle();
    });

});

Hmmm... looks like i took too much time trying to write a response. 嗯...看起来我花了太多时间试图写回应。 Anyway here's another just not to let it go to waste. 无论如何, 这是另一个不让它浪费的方法。

js: JS:

function clickHandler() {
  console.log($(this).data('link'))
  var link = $(this).data('link');
  $('.hide[data-link=' + link + ']').toggle();
  $('.show[data-link=' + link + ']').toggle();
  $('.more-content[data-link=' + link + ']').toggle('slow');

}

$(document).ready(function(){
    $('.hide, .more-content').hide();
    $('.hide, .show').on('click', clickHandler);
});

HTML: HTML:

    <div class="more-content" data-link="0">
      <!--These are the contents-->               
    </div>
    <button class="hide" class="more_arrow" data-link="0">Collapse <span class="fa fa-chevron-up"></span></button>

<button class="show" class="more_arrow" data-link="0">Expand <span class="fa fa-chevron-down"></span></button>
    <div class="more-content" data-link="1">
      <!--These are the contents-->               
    </div>
    <button class="hide" class="more_arrow" data-link="1">Collapse <span class="fa fa-chevron-up"></span></button>

<button class="show" class="more_arrow" data-link="1">Expand <span class="fa fa-chevron-down"></span></button>
    <div class="more-content" data-link="2">
      <!--These are the contents-->               
    </div>
    <button class="hide" class="more_arrow" data-link="2">Collapse <span class="fa fa-chevron-up"></span></button>

<button class="show" class="more_arrow" data-link="2">Expand <span class="fa fa-chevron-down"></span></button>

Essentially, you'll need a variable to specify which button should link to which div. 本质上,您将需要一个变量来指定哪个按钮应链接到哪个div。 Then you need to put that variable in an attribute of some sort. 然后,您需要将该变量放入某种属性中。 I chose a data attribute named 'link' but you could also use a class name. 我选择了一个名为“ link”的数据属性,但您也可以使用一个类名。

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

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