如何将元素定义为 "this"
How to define element as "this"
class example
可以有不同的高度,因为带有 "lorem ipsum" 的中间一个可以跨两行,因为它有更多的文字。
<div class="example">lorem</div>
<div class="example">lorem ipsum</div>
<div class="example">lorem</div>
如何识别?
我想知道 .example
元素的高度是否大于一行(例如 30px)。所以我想要这样的东西:
if ($('.example').height() > 30) {
$(this).css('background', 'green');
}
因此每个 .example
高度大于 30px
的元素都应具有绿色背景色。我怎样才能做到这一点?谢谢!
你可以用 callback function 来实现,内部回调 this
指的是元素
$('.example').height(function(i, v) {
if(v > 30) {
$(this).css('background', 'green');
}
});
只需遍历所有 example
个 div。
$(".example").each(function()
{
if ($(this).height() > 30)
$(this).css("background", "green");
});
class example
可以有不同的高度,因为带有 "lorem ipsum" 的中间一个可以跨两行,因为它有更多的文字。
<div class="example">lorem</div>
<div class="example">lorem ipsum</div>
<div class="example">lorem</div>
如何识别?
我想知道 .example
元素的高度是否大于一行(例如 30px)。所以我想要这样的东西:
if ($('.example').height() > 30) {
$(this).css('background', 'green');
}
因此每个 .example
高度大于 30px
的元素都应具有绿色背景色。我怎样才能做到这一点?谢谢!
你可以用 callback function 来实现,内部回调 this
指的是元素
$('.example').height(function(i, v) {
if(v > 30) {
$(this).css('background', 'green');
}
});
只需遍历所有 example
个 div。
$(".example").each(function()
{
if ($(this).height() > 30)
$(this).css("background", "green");
});