简体   繁体   English

使用JQuery隐藏兄弟姐妹和父母的兄弟姐妹,直到给定的id

[英]Hide siblings and parent's siblings until the given id using JQuery

I am trying to expand the inner div due to space issues on a button click. 我试图扩大内部div由于按钮点击空间问题。 When the user clicks on a button, I want to hide its siblings and parent's siblings until a specified id. 当用户点击按钮时,我想隐藏其兄弟姐妹和父母的兄弟姐妹,直到指定的id。 How to do this in javascript using jquery? 如何使用jquery在javascript中执行此操作?

Following is an example: 以下是一个例子:

Dom: DOM:

<div id="p">
    <div id="p1-1">
        <div id="c1-1-1">
            <button id="expand"></button>
        </div>
        <div id="c1-1-2">
        </div>
    </div>
    <div id="p1-2">
        <div id="c1-2-1">
        </div>
    </div>
    <div id="p1-3">
        <div id="c1-3-1">
        </div>
    </div>
</div>

Result Dom: 结果Dom:

<div id="p">
    <div id="p1-1">
        <div id="c1-1-1">
            <button id="expand"></button>
        </div>      
    </div>  
</div>

Find the elements you want to keep visible using parentsUntil() , loop through them using each() , then hide their siblings(). 找到你想要使用,以保持可见的元素parentsUntil() ,通过循环利用它们每() ,然后隐藏他们的兄弟姐妹()。

$(button).click(function(){
    var elementsToKeep = $(this).parentsUntil('#stop_element_id');
    elementsToKeep.each(function(){
        $(this).siblings().hide();
    });
});

Not enough rep to post link to siblings(). 没有足够的代表发布链接到兄弟姐妹()。

If I understand the question correctly: 如果我正确理解了这个问题:

$("#expand").on("click", function () {
    // Toggle text
    var text = $(this).text() == "Expand" ? "Collapse" : "Expand";
    $(this).text(text);
    // Toggle proximal element state
    $(this).parent().siblings().toggle();
    $(this).parent().parent().siblings().toggle();
});

See this live example 看到这个实例

A 'while-until' loop should do the trick: 'while-until'循环应该可以解决问题:

var str_stop_id = "p",
    $el = $(this).parent();
while ($el.attr('id') !== str_stop_id) {
    $el = $el.siblings().hide().end().parent();
}

http://jsfiddle.net/mblase75/Wc2Pf/ http://jsfiddle.net/mblase75/Wc2Pf/

This is working fine: 这很好用:

$(button).click(function(){
    var elementsToKeep = $(this).parentsUntil('#stop_element_id');
    elementsToKeep.each(function(){
        $(this).siblings().hide();
    });
});

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

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