简体   繁体   English

如何在一页JQuery上使用多个切换?

[英]How can I use multiple toggle on one page JQuery?

How can use multiple toggle on one page? 如何在一页上使用多个切换?
I have two div in my HTML file and I want to hide or remove the child-1 div and then replace with another div child-2 when every time that clicking on parent title. 我的HTML文件中有两个div,我想隐藏或删除child-1 div,然后在每次单击父标题时替换为另一个div child-2
my HTML structure as below: 我的HTML结构如下:

 $(document).ready(function(){ $('.child_1').show(); $('.child_2').hide(); $('.parent').click( function() { $(this).each( function() { $('.child_1, .child_2').toggle(); }); }); }); 
 .child_1, .child_2 { background-color: red; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <h2>Title One</h2> <div class='child_1'>paragraph 1</div> <div class='child_2'>paragraph 2</div> </div> <div class="parent"> <h2>Title Two</h2> <div class='child_1'>paragraph 1</div> <div class='child_2'>paragraph 2</div> </div> 

This work perfectly but when I click in one element other element also change. 这项工作正常,但是当我单击一个元素时,其他元素也会更改。

Please see here: JSFiddle . 请在这里查看: JSFiddle

Bug in your code: 代码中的错误:

The following code calls .toggle() on every instance of $('parent') thus applying the .click() logic to all .parent elements rather than just the clicked one: 以下代码在$('parent')每个实例上调用.toggle() ,从而将.parent .click()逻辑应用于所有.parent元素,而不仅仅是单击的元素:

$('.parent').click( function() {
    $(this).each( function() {
      $('.child_1, .child_2').toggle();
    });
  });

In order to fix this problem one could rewrite it as follows: 为了解决此问题,可以将其重写如下:

  $('.parent').click( function() {
    $(this).children('div').toggle('.child_1, .child_2');
  });

Suggested approach: 建议的方法:

The best way to approach this is by using .toggleClass() on the .parent element when clicked. 解决此问题的最佳方法是在单击.parent元素时使用.toggleClass()

The class when present will dictate the state of specified children - when class show is active on .parent show .child_2 and when not show child_1 . 该类存在时,将指示指定子级的状态-当在.parent show .child_2.parent class show时,而不显示child_1 This way you set one state as the initial view avoiding the need to .hide() it on load. 这样,您可以将一个状态设置为初始视图,而无需在加载时.hide()

It's best practice to control visual states with when possible (ie class with display: none and/or display: block ) and dictating the styles of contained elements based on the parent element's state/class. 最佳实践是在可能的情况下使用控制视觉状态(例如,带有display: nonedisplay: none和/或display: block ),并根据父元素的state / class指示所包含元素的样式。

 $(document).ready(function(){ $('.parent').click( function() { $(this).toggleClass('show'); }); }); 
 .child_1, .show .child_2 { display: block; } .child_2, .show .child_1 { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <h2>Title</h2> <div class='child_1'>paragraph 1</div> <div class='child_2'>paragraph 2</div> </div> <div class="parent"> <h2>Title</h2> <div class='child_1'>paragraph 1</div> <div class='child_2'>paragraph 2</div> </div> 

just change your js function slightly: 只需稍微更改您的js函数:

 $(document).ready(function(){ $('.child_1').show(); $('.child_2').hide(); $('.parent').click( function() { $(this).each( function() { $('.child_1, .child_2',this).toggle(); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <h2>Title One</h2> <div class='child_1'>paragraph 1</div> <div class='child_2'>paragraph 2</div> </div> <div class="parent"> <h2>Title Two</h2> <div class='child_1'>paragraph 1</div> <div class='child_2'>paragraph 2</div> </div> 

Just toggle the div children. 只需切换div孩子。

$(function(){ 
  $('.parent').click( function() {
    $(this).children("div").toggle();
  });
});

http://jsfiddle.net/jakecigar/cc3bmxx8/18/ <-- http://jsfiddle.net/jakecigar/cc3bmxx8/18/ <-

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

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