2010-01-31 80 views
3

鉴于这种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> 

为什么标签不褪色当我专注于输入?我确定知道焦点事件正在发生。

感谢

回答

12

最近看起来通过 “父母” 没有兄弟姐妹。你想要的是prevAll

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

closest其实就是“发现,选择,包括是否符合要求,已经选定的元素相匹配的最近的祖先。”

+0

+1你是一个男人,Neiner :) – Sampson 2010-01-31 06:29:32

+2

@Jonathan我知道你在上,所以我不得不打字超快,经常发帖:) – 2010-01-31 06:31:00