如何在jQuery中通过最接近的标记选择子标记

时间:2011-02-09 05:28:27

标签: javascript jquery html

我已经选择了当前点击过的jQuery中的元素。我尝试父,但这没有用,所以我尝试jQuery中最接近的标签

$(this).closest('.posts').children('.cbl h1').html('1')

我试试这个并且选择工作正常意味着我成功选择了标签.cbl h1。现在告诉我如何将文本更改为1。

结构

   <div class='posts'>
    <div class='cbl'>
    </div>
<div><div>$(this) element is here</div></div>
    </div>

1 个答案:

答案 0 :(得分:2)

您可以使用.text()方法。

这样的东西
$(this).closest('.posts').children('.cbl h1').text('1');