kri*_*tya 6 jquery jquery-selectors
Hii我想显示与班级最近的div的数字字符showchars.
我试着这样做:
$('#maina').focus(function() {
$(this).closest("div.showchars").find(".countchars").html("KKK");
$("#error-1").html("");
$(this).closest(".showchars").html("hmm");
});
Run Code Online (Sandbox Code Playgroud)
有了这个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>
Run Code Online (Sandbox Code Playgroud)
当我专注于输入框时,它会重置错误类的内容.
但是没有将文本更改为:hmm在div中.
如果已经我会错或者有什么更好的方式来获得最接近格,然后实现API: .html("hmm")?
谢谢
.closest() 查看元素本身及其父母的匹配.
在您的示例中,.showchars是文本框的兄弟,因此您可以使用它.siblings().
$(this).siblings(".showchars").html("hmm");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20286 次 |
| 最近记录: |