jQuery将css应用于类的父类?

时间:2014-05-19 13:46:56

标签: jquery css

所以这是我目前的jQuery代码:

$('.aProduct .inner').each(function() {
    var length = $.trim($(this).text()).length;    
    if(length > 20){
        $(this).css("max-height", "20px");
    }
    else {
        $(this).css("background", "blue");
    }
});

它说:如果字符的长度大于20,请将此CSS应用于.aProduct .inner

$(this).css("max-height", "20px");

但是,是否可以将此max-height赋予此类的父级?我并不只是想用.aProduct .omschrijving来替换它,因为那么所有.aProduct .omschrijving的最大高度都是20.我只希望父类的父类.aProduct .omschrijving字符数超过20,最大高度为20px。

HTML:

<div class="aProductHeader">
    <div class="omschrijving">
        <h3>omschrijving</h3>
    </div>
</div>
<div class="aProduct">
    <div class="omschrijving">
        <span class="entry inner">
            Toddler bestek blauw
        </span>
    </div>
</div>

这听起来真的很混乱,但我不知道怎么解释这个。

1 个答案:

答案 0 :(得分:3)

变化:

if(length > 20){
    $(this).css("max-height", "20px");
}

为:

if(length > 20){
    $(this).closest('.omschrijving').css("max-height", "20px");
}
相关问题