jQuery使用类获取最接近的div [英] jQuery getting the closest div with a class
本文介绍了jQuery使用类获取最接近的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示最接近div类别为showchars
的字符数.
Hii i want to show the number chars to the closest div with the class showchars
.
我试图这样做:
$('#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>
当我专注于输入框时,它会重置错误类的内容.
When i focus into the input boxes it does resets the content of the errors class.
但不会将文本更改为div中的hmm
.
But doesnt changes text to : hmm
in the div.
我哪里出错了,或者是获得最接近的div然后实现api的更好方法是什么:.html("hmm")
?
Where have been i going wrong or what is the better way to get the closest div and then implement the api : .html("hmm")
?
谢谢
推荐答案
.closest()
查看元素本身及其父元素是否匹配.
在您的示例中,.showchars
是文本框的同级,因此您可以使用 .siblings()
.
In your example, .showchars
is a sibling of the textbox so you could use .siblings()
instead.
$(this).siblings(".showchars").html("hmm");
此处演示.
这篇关于jQuery使用类获取最接近的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文