简体   繁体   English

jQuery:获取父元素,然后将CSS选择器应用于它

[英]Jquery : Get parent element then apply css selector to it

I am trying to figure out the best way to go about this. 我正在尝试找出解决此问题的最佳方法。 I am trying to expand/collapse all hide-able divs on a page. 我正在尝试扩展/折叠页面上所有可隐藏的div。 Which generally is no problem, however, I have four separate sections which require the expand/collapse to work independently. 通常这没问题,但是,我有四个独立的部分,需要展开/折叠才能独立工作。 So, I am hoping to not have to write four separate scripts using four separate ids as selectors. 因此,我希望不必使用四个单独的ID作为选择器来编写四个单独的脚本。

I found that this 我发现这个

$(function() {
    $('.expand').click(function() {
        $('#test *').nextAll('.silent').slideToggle();
    });
});

Works beautifully, so long as I only need to expand one section. 只要我只需要扩展一个部分,效果就很好。 So, my thought is this : If I can use jquery to get the parent div ( parent containing the expand button) class or id (as they'll each be different by necessity) and then apply the selector * class to it before executing a nextAll, that should solve my problem. 因此,我的想法是这样的:如果我可以使用jquery来获取父div(包含扩展按钮的父类)类或id(因为它们彼此之间必然有所不同),然后在执行nextAll,那应该解决我的问题。

Here's a simple fiddle of what I have so far, in case you'd like further clarification. 如果您想进一步说明一下,这只是到目前为止我的小提琴。 The goal is to be able to use the "expand" button for each section to expand the sections underneath, without expanding the other. 目的是能够为每个部分使用“扩展”按钮来扩展下面的部分,而无需扩展其他部分。 (With a single script) jsfiddle (使用单个脚本) jsfiddle

Thank you in advance for any assistance you might be able to provide. 预先感谢您可能提供的任何帮助。

Yes. 是。 Use: 采用:

$('.expand').click(function(){
    $(this).parent().find('.silent').slideToggle();
});

Demo 演示版

I've played around and created something quickly for you now :) http://jsfiddle.net/fedmich/6Rxzg/ 我已经玩过,现在为您快速创建了一些东西:) http://jsfiddle.net/fedmich/6Rxzg/

It's different from your code but you could see a simple one. 它与您的代码不同,但是您可以看到一个简单的代码。

You can use closest() to get the parent matching your CSS pattern. 您可以使用closest()获取与您的CSS模式匹配的父级。 and you should probably use classnames instead of id 并且您应该使用类名而不是id

<div class="expander">
   ...content...
</div>
<div class="expander">
   ...content...
</div>

Some tips: 一些技巧:

  1. Indent your code and put comments on it. 缩进您的代码并在其上添加注释。
  2. rather than do a YELL or SILENT, use simple meaningful names like "shown" 而不是使用YELL或SILENT,而是使用简单有意义的名称,例如“ shown”
  3. Please read the jsfiddle carefully, so you could apply styles like cursor:hand, don't forget to put enough spacing, 请仔细阅读jsfiddle,以便您可以应用cursor:hand之类的样式,不要忘记留出足够的间距,
  4. only expand those that aren't expanded yet, etc 仅扩展尚未扩展的那些,等等

Some more tips: 更多提示:

  1. You could also put icons like + or - to show collapsed or NOT like what I did on my resume page before http://fedmich.com/resume/#work4 您还可以将+或-之类的图标显示为折叠或不像我在http://fedmich.com/resume/#work4之前的履历页上所做的那样

Sorry for the long answer. 很抱歉,答案很长。 Doing accordions like this is always FUN to do ! 像这样做手风琴总是很有趣的 So don't forget to have lots of it! 因此,不要忘记拥有很多!

Add some class on div's with id test and then it will be more simple. 在具有id测试的div上添加一些类,然后它将更加简单。

<div id="test" class="SomeClass">
content here
</div>

<div id="test2" class="SomeClass">
content
</div>

try with closest() , it will get the parent element whose id is test and then get child element of test with class silent and slide it: 尝试使用closet() ,它将获取其id为test的父元素,然后获取带有silent类的test的子元素并将其滑动:

$('.expand').click(function() {
        $(this).closest(".SomeClass").find('.silent').slideToggle();
    });

WORKING FIDDLE 工作场所

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

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