3

I have these elements;

<div class="preview">
    <div class="title" style="display:block">
</div>

<div class="preview">
    <div class="title" style="display:none">
</div>

<div class="preview">
    <div class="title" style="display:none">
</div>

I want to add a class to the preview, if the title is 'display:none'.

Please help me short this.

Thanks & Regards.

4

2 回答 2

6
$('.preview').has('.title:hidden').addClass('yournewclass');
于 2013-02-09T12:09:08.887 回答
4

Try this:

$('.title').is(':not(:visible)').closest('.preview').addClass('foo');

Note the :not(:visible) will catch any .title elements who are hidden due to the visibility or display properties. If you only want to catch display: none try this:

$('.title').each(function() {
    if ($(this).css('display') == 'none') {
        $(this).closest('.preview').addClass('foo');
    }
});
于 2013-02-09T12:08:58.947 回答