jQuery最近()不适合我(或者我不是为它工作) [英] jQuery closest() not working for me (or i'm not working for it)

查看:67
本文介绍了jQuery最近()不适合我(或者我不是为它工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此jQuery:

$('div.MvcFieldWrapper :input').focus(function() {
      $(this).closest('label.MvcDynamicFieldError').fadeOut();
});

鉴于此HTML:

<div class="MvcFieldWrapper">
    <label class="MvcDynamicFieldPrompt">Enter your email address:</label>
    <label class="MvcDynamicFieldError">Required</label>
    <input type="text" value="" />
</div>

为什么当我专注于输入时标签不会淡出?我确定焦点事件正在发生。

Why is the label not fading out when I focus on the input? I know for sure that the focus event is happening.

谢谢

推荐答案

最近看通过父母而不是兄弟姐妹。你想要的是 prevAll

Closest looks through the "parents" not siblings. What you want is prevAll:

$('div.MvcFieldWrapper :input').focus(function() {
      $(this).prevAll('label.MvcDynamicFieldError').fadeOut();
});

最近的实际上意味着找到最近的祖先匹配选择器,包括已满足要求的已选元素。

closest actually means "find the closest ancestor that matches the selector, including the already selected element if it meets the requirements."

这篇关于jQuery最近()不适合我(或者我不是为它工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆