jQuery使用类获取最接近的div

jQuery使用类获取最接近的div,jquery,jquery-selectors,Jquery,Jquery Selectors,Hii我想用类showchars将数字字符显示到最近的div 我试着这样做: $('#maina').focus(function() { $(this).closest("div.showchars").find(".countchars").html("KKK"); $("#error-1").html(""); $(this).closest(".showchars").html("hmm"); }); 使用此html: <tr> <td

Hii我想用类
showchars
将数字字符显示到最近的div

我试着这样做:

$('#maina').focus(function() {
$(this).closest("div.showchars").find(".countchars").html("KKK");
    $("#error-1").html("");
    $(this).closest(".showchars").html("hmm");
});
使用此html:

    <tr>
    <td>
    Main Alliase/Current Alliase:
    </td>
    <td><input type="text" id="maina" class="countchars"/><br />
    <div class="showchars">o</div>
    </td>
<td><span id="handle-1" class="selector">?</span></td>
    <td><div id="error-1" class="errors"></div></td>
    </tr>

主要蒜氨酸酶/当前蒜氨酸酶:

o ?
当我聚焦到输入框中时,它会重置errors类的内容

但不会将div中的文本更改为:
hmm

我哪里出错了,或者获取最近的div然后实现api的更好方法是什么:
.html(“hmm”)

谢谢

查看元素本身及其父元素以进行匹配

在您的示例中,
.showchars
是文本框的同级,因此您可以使用它


.

它起作用了,但它会改变班上所有同级同学的内容吗?感谢
.sibbins()
可以选择接受筛选器。您可以指定
“.showchars”
来过滤您确切需要的元素。是的,我知道,但我的意思是,它将把它放在使用类showchars找到的所有同级元素中?只是问我是否可以在其他地方实现。是和否,取决于你的要求。如果源元素与类
showchars
有多个同级,则所有同级元素都将包含该文本。你可以使用
.showchars:first
作为过滤器,以第一个兄弟姐妹为目标,第二个兄弟姐妹为目标,依此类推。我不认为获得所有下一个然后选择第一个是一个好的选择。思想感谢您的尝试:DIll宁愿使用。sibling:D
sibles
也将获得所有sibling并使用选择器对其进行筛选
$(this).siblings(".showchars").html("hmm");
$(this).nextAll(".showchars:first").html("hmm");