2016-03-07 110 views
0

的孩子。如果我有一個DOM結構如下所示:如何獲取父div元素

$(function() { 
 
    $(".child-a").click(function() { 
 
    // change `.child-b` to green 
 
    
 
    // I can go higher into the chain 
 
    $(this).parents(".parent").css({ 
 
     "background-color": "black" 
 
    }); 
 
    
 
    // I can use css to get `.child-b` to blue 
 
    $(".parent .sub-parent-b .child-b").css({ 
 
     "background-color": "blue" 
 
    }); 
 
    }); 
 
});
div div div { 
 
    width: 100px; 
 
    height:100px; 
 
    background-color: red; 
 
    border:1px black dashed; 
 
} 
 

 
.child-a { 
 
    margin-bottom: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parent"> 
 
    <div class="sub-parent-a"> 
 
    <div class="child-a"></div> 
 
    </div> 
 
    <div class="sub-parent-b"> 
 
    <div class="child-b"></div> 
 
    </div> 
 
</div>

我可以在技術上正常使用CSS,但它不夠靈活。

$(function() { 
 
    $(".child-a").click(function() { 
 
    // change `.child-b` to green 
 
    
 
    // I can go higher into the chain 
 
    $(this).parents(".parent").css({ 
 
     "background-color": "black" 
 
    }); 
 
    
 
    // I can use css to get `.child-b` to blue 
 
    $(".parent .sub-parent-b .child-b").css({ 
 
     "background-color": "blue" 
 
    }); 
 
    
 
    /* 
 
     In this case, my first aproach of using `parent` made sure that the change only apeared on that specific css, or in #a, but not on #b 
 
     
 
     While the use of css could only get me to change things in both #a and #b 
 
     
 
     So how could I manage to do something like: 
 
     
 
     #a .sub-parent-a .child-a is clicked, so only #a .sub-parent-b .child-b changes, and not #b .sub-parent-b .child-b. 
 
    */ 
 
    }); 
 
});
div div div { 
 
    width: 100px; 
 
    height:100px; 
 
    background-color: red; 
 
    border:1px black dashed; 
 
} 
 

 
.child-a { 
 
    margin-bottom: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parent" id="a"> 
 
    <div class="sub-parent-a"> 
 
    <div class="child-a"></div> 
 
    </div> 
 
    <div class="sub-parent-b"> 
 
    <div class="child-b"></div> 
 
    </div> 
 
</div> 
 

 
<div class="parent" id="b"> 
 
    <div class="sub-parent-a"> 
 
    <div class="child-a"></div> 
 
    </div> 
 
    <div class="sub-parent-b"> 
 
    <div class="child-b"></div> 
 
    </div> 
 
</div>

在這種情況下,我使用parent確信,變化只apeared對特定的CSS,或在#a,但第一次的形式給出了不#b

雖然使用的CSS只能讓我改變東西#a#b

所以我怎麼能管理噸o做類似的事情:

#a .sub-parent-a .child-a被點擊,所以只有#a .sub-parent-b .child-b更改,而不是#b .sub-parent-b .child-b

回答

1

你可以上鍊,也可以沿着鏈條往回走。這樣選擇器就停留在「this」元素中。

$(function() { 
 
    $(".child-a").click(function() { 
 
    // change `.child-b` to green 
 
    
 
    // I can go higher into the chain 
 
    $(this).parents(".parent").css({ 
 
     "background-color": "black" 
 
    }); 
 
    
 
    // I can use css to get `.child-b` to blue 
 
    $(this).parents(".parent").find('.child-b').css({ 
 
     "background-color": "blue" 
 
    }); 
 
    
 
    /* 
 
     In this case, my first aproach of using `parent` made sure that the change only apeared on that specific css, or in #a, but not on #b 
 
     
 
     While the use of css could only get me to change things in both #a and #b 
 
     
 
     So how could I manage to do something like: 
 
     
 
     #a .sub-parent-a .child-a is clicked, so only #a .sub-parent-b .child-b changes, and not #b .sub-parent-b .child-b. 
 
    */ 
 
    }); 
 
});
div div div { 
 
    width: 100px; 
 
    height:100px; 
 
    background-color: red; 
 
    border:1px black dashed; 
 
} 
 

 
.child-a { 
 
    margin-bottom: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parent" id="a"> 
 
    <div class="sub-parent-a"> 
 
    <div class="child-a"></div> 
 
    </div> 
 
    <div class="sub-parent-b"> 
 
    <div class="child-b"></div> 
 
    </div> 
 
</div> 
 

 
<div class="parent" id="b"> 
 
    <div class="sub-parent-a"> 
 
    <div class="child-a"></div> 
 
    </div> 
 
    <div class="sub-parent-b"> 
 
    <div class="child-b"></div> 
 
    </div> 
 
</div>

+0

謝謝你,我不知道.find的'()' –

1

IMO你有辦法不多jQuery和沒有使用任何CSS的力量。

$(function() { 
 
    $(".child").click(function() { 
 
    // reset 
 
    $('.parents .selected').removeClass('selected'); 
 

 
    var $child = $(this); 
 
    var $subParent = $(this).closest('.sub-parent'); 
 

 
    $child.addClass('selected'); 
 
    $subParent.addClass('selected'); 
 
    }); 
 
});
div{ 
 
    padding: 5px; 
 
} 
 

 
.sub-parent.selected { 
 
    background-color: red; 
 
} 
 
.child{ 
 
    cursor: pointer; 
 
} 
 
.child.selected { 
 
    background-color: blue; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parents">Parents 
 
    <div class="parent">- Parent 
 
    <div class="sub-parent">- - Sub Parent 
 
     <div class="child">- - - Child</div> 
 
    </div> 
 
    <div class="sub-parent">- - Sub Parent 
 
     <div class="child">- - - Child</div> 
 
    </div> 
 
    </div> 
 
    <div class="parent">- Sub Parent 
 
    <div class="sub-parent">- - Sub Parent 
 
     <div class="child">- - - Child</div> 
 
    </div> 
 
    <div class="sub-parent">- - Sub Parent 
 
     <div class="child">- - - Child</div> 
 
    </div> 
 
    </div> 
 
</div>