简体   繁体   English

jQuery 在具有相同类的多个 div 上切换

[英]jQuery toggle on multiple divs with same class

I am trying to get a jQuery toggle up and running on my site.我正在尝试在我的网站上启动并运行 jQuery。

So far my code is:到目前为止,我的代码是:

<script>
$(document).ready(function(){
  $(".flip").click(function(){
    $(".flippanel").toggle();
  });
});
</script>

I then in the HTML have:然后我在 HTML 中有:

<p class="flip">Flip it!</p>
<p class="flippanel">This is a paragraph with little content.</p>
<p class="flippanel">This is another small paragraph.</p>
<p class="flip 2">Flip it!</p>
<p class="flippanel 2 ">This is a paragraph with little content.</p>
<p class="flippanel 2">This is another small paragraph.</p>

How do I go about getting it to toggle the two panels independently?我如何让它独立切换两个面板? Right now, each panel toggles at the same time.现在,每个面板同时切换。

So it looks like you want the flippanel class items to hide themselves when you click "flip it"?因此,当您单击“翻转”时,您似乎希望翻转面板类项目隐藏自己? I would then recommend you instead put these inside of a div so that when you click on it, the whole div collapses.然后我会建议你把这些放在一个 div 中,这样当你点击它时,整个 div 就会崩溃。

That way you can do something like:这样你就可以做这样的事情:

$(".flip").click(function(){
  $(this).children().toggle();
});

If you think this is the code for you, but don't like that everything inside of the div is clickable, you can add the following code Additionally, if you think this is the solution for you, you could add the following code which will make it such that only the "Flip it!"如果您认为这是适合您的代码,但不喜欢 div 中的所有内容都是可点击的,您可以添加以下代码此外,如果您认为这是适合您的解决方案,您可以添加以下代码让它这样,只有“翻转它!” part is clickable.部分是可点击的。 I've updated my fiddle below to include this code.我已经更新了下面的小提琴以包含此代码。

$(".flippanel").click(function( event ) {
  event.stopPropagation();
});

I've made a JSFiddle for you if you click this line to look.如果您单击此行查看,我已经为您制作了一个 JSFiddle。

What you need is to use .nextUntil() .你需要的是使用.nextUntil() It will get every next element until you hit the passed selector.它将获取每个下一个元素,直到您点击传递的选择器。

$(".flip").click(function(){
  $(this).nextUntil('.flip').toggle();
});

Note that the passed selector is excluded from the object.请注意,传递的选择器被排除在对象之外。

Select the next two p .flippanel of the one you click each time.选择您每次单击的下两个p .flippanel

nextAll select all following siblings nextAll选择所有以下兄弟姐妹

slice(0, 2) narrows to the first two slice(0, 2)缩小到前两个

Try:尝试:

$(".flip").click(function(){
    $(this).nextAll(".flippanel").slice(0, 2).toggle();
  });

DEMO演示

$(".flip").click(function(){
      $(this).next().toggle();
      $(this).next().next().toggle();
});

will work.将工作。

http://jsfiddle.net/xyqwmzhj/2/ http://jsfiddle.net/xyqwmzhj/2/

but you really should consider rewriting your html, use the same classes for the content and going with children or something like that.但你真的应该考虑重写你的 html,为内容使用相同的类并与孩子一起去或类似的东西。

[edit] if you want it to start closed, add [编辑] 如果您希望它开始关闭,请添加

style="display: none;"

to the div that should be hidden.到应该隐藏的div。

full html:完整的html:

<p class="flip" style="cursor:  pointer;">Flip it!</p>
<div style="display: none;">
<p class="flippanel">This is a paragraph with little content.</p>
<p class="flippanel">This is another small paragraph.</p>
</div>
<p class="flip" style="cursor:  pointer;">Flip it!</p>
<div style="display: none;">
<p class="flippanel">This is a paragraph with little content.</p>
<p class="flippanel">This is another small paragraph.</p>
</div>

Numbers are not valid as html class.数字作为 html 类无效。

Try it like this:像这样尝试:

<p class="flip flip_1">Flip it!</p>
<p class="flippanel flippanel_1">This is a paragraph with little content.</p>
<p class="flippanel flippanel_1">This is another small paragraph.</p>
<p class="flip flip_2">Flip it!</p>
<p class="flippanel flippanel_2 ">This is a paragraph with little content.</p>
<p class="flippanel flippanel_2">This is another small paragraph.</p>

This way your styling eg can attach to the flippanel class and your JS can look like this:这样你的样式就可以附加到flippanel类,你的JS看起来像这样:

<script>
$(document).ready(function(){
  $(".flip").click(function(){
    $(".flippanel_1").toggle();
  });
});
</script>

I think Your jQuery code is ok for the first flip it!我认为您的 jQuery 代码可以用于第一次翻转! I am suggesting you should create another for the second flip it!我建议你应该为第二次翻转创建另一个! And you need to change your second flip it HTML tag classes你需要改变你的第二个翻转 HTML 标签类

From:从:

<p class="flip 2">Flip it!</p>
<p class="flippanel 2 ">This is a paragraph with little content.</p>
<p class="flippanel 2">This is another small paragraph.</p>

To:到:

<p class="flip2">Flip it!</p>
<p class="flippanel2 ">This is a paragraph with little content.</p>
<p class="flippanel2">This is another small paragraph.</p>

Then the jQuery codes to add:然后添加 jQuery 代码:

$(document).ready(function(){ 
    $(".flip2").click(function(){
        $(". flippanel2").toggle();
    });
});

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

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