简体   繁体   English

在jQuery中选择父母的下一个兄弟的孩子

[英]Selecting parent's next sibling's children in jQuery

This is an illustration of a part of my DOM: 这是我的DOM的一部分的说明:

在此输入图像描述

I want to trigger an effect on div with class des (which is in an odd div ) on click event of div with class but (which is in the previous even div ) 我想触发带班格的效果des (这是一个奇怪的div上的点击事件) div带班but (这是在以前甚至div

How can I select des from but ? 我如何选择des but

Trivially, if this is the clicked .but element: 平凡的,如果this是点击的.but元素:

$(this).parent().next().children('.des')

This does assume that there are no other elements in the way, ie that the .even and .odd elements are immediate siblings, and that .but and .des are immediately children of their respective .even and .odd ancestors. 这并假设有在路上没有其他的元素,即在.even.odd元素是直接的兄弟姐妹,和.but.des立即各自的孩子.even.odd祖先。

Less efficiently, but guaranteed to work with any arbitrary number of additional (non-matching) elements in the tree would be: 效率较低,但保证与树中任意数量的其他(非匹配)元素一起使用将是:

$(this).closest('.even').nextAll('.odd').first().find('.des')

It depends on your markup. 这取决于你的标记。 Something like this should work, though: 但是这样的事情应该有效:

$('.but').on('click', function () {
  $(this).closest('.even').next().find('.des');
});

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

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