简体   繁体   中英

jQuery - Selecting a parent that has a child

I have a Div which may contain an INPUT with a class of "datepicker"

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

I need to apply a Style="clear" to the top div. However I don't want to if that div doesn't contain a datepicker child.

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()

$('.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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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