所以这是我目前的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>
这听起来真的很混乱,但我不知道怎么解释这个。
答案 0 :(得分:3)
变化:
if(length > 20){
$(this).css("max-height", "20px");
}
为:
if(length > 20){
$(this).closest('.omschrijving').css("max-height", "20px");
}