当子div为“display:block”时,将类添加到父div,并在“display:none”时删除? [重复]

时间:2022-11-28 16:46:52

This question already has an answer here:

这个问题在这里已有答案:

I have a tabs setup, and I'm looking to accomplish the following:

我有一个标签设置,我希望完成以下任务:

I want child divs with class fifty to add class flex to their parent div DTE_Form_Content when their style is display: block, and remove class flex when their style is display: none.

我希望带有类50的子div在它们的样式为display:block时将class flex添加到它们的父div DTE_Form_Content,并在它们的样式为display:none时删除class flex。

The structure is like this:

结构是这样的:

<div class="DTE_Form_Content">
    <div class="DTE_Field_Name_ps-ceilings_amt" style="display: none">
    <div class="DTE_Field_Name_app-co-ds fifty" style="display: block">
</div>

This would allow me to change the format of the child div's layout. How could I accomplish this? What I was thinking may work:

这将允许我更改子div的布局格式。我怎么能做到这一点?我的想法可能有用:

if ( $('div.fifty').css('style="display: block"') ) {
  $('div.DTE_Form_Content').addClass('flex');
} else {
  $('div.DTE_Form_Content').removeClass('flex');
}

EDIT: This is not a duplicate question. My question pertains to children containing a specific class, whereas the marked duplicate does not.

编辑:这不是一个重复的问题。我的问题涉及包含特定类的儿童,而标记的副本则没有。

2 个解决方案

#1


1  

var $dte = $('.DTE_Form_Content');

$('.fifty').is(':visible') ? $dte.addClass('flex') : $dte.removeClass('flex')

You could try this solution, using a ternary also adds an element of cleanliness IMO. If this works for you consider accepting the answer :)

你可以尝试这个解决方案,使用三元也增加了清洁度IMO的元素。如果这对您有效,请考虑接受答案:)

#2


0  

Try this

尝试这个

if($("div.fifty").css("display")=="block")
{
 $("div.DTE_Form_Content").addClass("flex");
}
else
{
 $("div.DTE_Form_Content").removeClass("flex");
}

#1


1  

var $dte = $('.DTE_Form_Content');

$('.fifty').is(':visible') ? $dte.addClass('flex') : $dte.removeClass('flex')

You could try this solution, using a ternary also adds an element of cleanliness IMO. If this works for you consider accepting the answer :)

你可以尝试这个解决方案,使用三元也增加了清洁度IMO的元素。如果这对您有效,请考虑接受答案:)

#2


0  

Try this

尝试这个

if($("div.fifty").css("display")=="block")
{
 $("div.DTE_Form_Content").addClass("flex");
}
else
{
 $("div.DTE_Form_Content").removeClass("flex");
}