遍历(父级)然后向下(子级)

时间:2014-10-27 11:12:59

标签: jquery traversal

我希望得到像这样的东西

var argument = $(this).parent().parent().parent().prev().child().attr('ctryid');

这是我想要遍历的部分的剪辑。 我想从newProvButton转到ctryid的元素。

一团糟,对不起。

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以使用closest()查找具有给定选择器的相关父元素。试试这个:

var argument = $(this).closest('.expandListContent').prev().find('.expandListHeaderRow').attr('ctryid');

您还应注意,在HTML中使用非规范属性会导致页面无效。请改为使用data-*属性:

<div class="expandListHeaderRow" data-ctryid="your-guid">Foo</div>
var argument = $(this).closest('.expandListContent').prev().find('.expandListHeaderRow').data('ctryid');

答案 1 :(得分:1)

您可以使用.closest()

缩小选择范围
$(this).closest('.expandListContent').prev().find('div').attr('ctryid');

$(this).closest('.expandListContent').prev().find('.expandListHeaderRow').attr('ctryid');
相关问题