jquery:最近的(';h3';)选择器?

jquery:最近的(';h3';)选择器?,jquery,jquery-selectors,closest,Jquery,Jquery Selectors,Closest,我有: 是否有更好的方法选择最近的h3元素。它确实适用于3倍父对象(),但不适用于最近的('h3) 为什么?因为h3不是计数器的父项,所以这不起作用。在.rating上使用.nestest(),找到它的h3: $('.iLikeThis .counter').each(function() { $(this).parent().parent().parent().children('h3').text('You like this'); $(this).paren

我有:

是否有更好的方法选择最近的h3元素。它确实适用于3倍父对象(),但不适用于最近的('h3)


为什么?

因为
h3
不是
计数器的父项,所以这不起作用。在
.rating
上使用
.nestest()
,找到它的
h3

$('.iLikeThis .counter').each(function() {
        $(this).parent().parent().parent().children('h3').text('You like this');
        $(this).parent().addClass('like');
});

这是无效的HTML。你不能把
h3
作为
ul
的孩子。像这样的坏HTML,不可能知道浏览器是否做了正确的事情。谢谢!作为李的孩子,你能拥有它吗?
$('.iLikeThis .counter').each(function() {
        $(this).parent().parent().parent().children('h3').text('You like this');
        $(this).parent().addClass('like');
});
$(this).closest('.rating').children('h3').text('You like this');