简体   繁体   English

jQuery-选择有孩子的父母

[英]jQuery - Selecting a parent that has a child

I have a Div which may contain an INPUT with a class of "datepicker" 我有一个Div,其中可能包含带有“ datepicker”类的INPUT

<div class="rereg-input180">
     ... some layers
         <input class="one two datepicker three">
               ....

I need to apply a Style="clear" to the top div. 我需要将Style =“ clear”应用于顶部d​​iv。 However I don't want to if that div doesn't contain a datepicker child. 但是,我不希望该div不包含datepicker子级。

How do I select it? 如何选择呢?

You can do this - 你可以这样做 -

$div = $('.rereg-input180');

if($div.find('.datepicker').length){
  // apply style
}

您也可以这样做:

$('.datepicker').closest('.rereg-input180').addClass('clear');

You can use .has() http://api.jquery.com/has/ assuming you want the clear both, if it's a clear class, the you can just .addClass() instead of .css() 您可以使用.has() http://api.jquery.com/has/假定您都希望清除两者,如果是清除类,则可以只使用.addClass()而不是.css()

$('.rereg-input180').has('.datepicker').css('clear', 'both');

if you want to be more specific you can do 如果您想更具体一点,可以做

$('.rereg-input180').has('input.datepicker').css('clear', 'both');

您可以使用以下选择器:

$('div.rereg-input180:has(:text.datepicker)').css('clear', 'both')

您可以使用: jQuery的 .parent()

$(".datepicker").parent().css("clear","both");

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

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