简体   繁体   English

我如何定位最接近的元素jQuery

[英]How can i target closest element jQuery

i'm trying to target the closest element of input field on keyup. 我试图针对键盘输入字段中最接近的元素。

  <div class="form-group">
     <input type="text" class="date_of_birth" name="date[]" value="">
     <span class="stepbirthVal"></span>
   </div>

i want to target stepbirthVal i tried by doing this in JS 我想针对我在JS中尝试过的stepbirthVal

var item = $(this).find('.stepbirthVal')

i also tried this 我也尝试过

var item = $(this).closest('.form-group').find('stepbirthVal');

Can you please help me how can i target closest element. 您能帮我怎样定位最近的元素吗?

Thanks 谢谢

Try This: 尝试这个:

If you want to find out by class 如果要按班级查找

$('.date_of_birth').next('.stepbirthVal');

If you want to find it on input event lets say onFocus: 如果要在输入事件中找到它,请说onFocus:

$(this).next('span.stepbirthVal');

You can be more precise by finding element with class ('span.stepbirthVal') like this. 通过像这样找到具有类('span.stepbirthVal')的元素,可以更加精确。 There are various other ways depending upon situation. 根据情况,还有其他各种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM