简体   繁体   中英

If DIV has class toggle a SPAN

I have this issue where I have two pieces of JavasScript that I'd like to combine.

There is a part that does this:

$('div.more-content:has(.requiredError)').fadeIn(0);

and then a separate item that I'd like to execute if the above condition is true:

$('.show-more').find('span').fadeToggle(0);

Now I tried combining them in a IF, ELSE statement but that didn't work.

You can try this:

$('div.more-content:has(.requiredError)')
    .fadeIn(0, function(){
        $('.show-more span')
            .fadeToggle(0);
});

You don't have a condition, you have a jQuery object which may or may not be empty. You detect that by measuring its length:

if ($('div.more-content:has(.requiredError)').fadeIn(0).length) {
    $('.show-more').find('span').fadeToggle(0);
}

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