[英]Add class to a dynamic height div
I have a "div#area" with a dynamic contents ,so div height is also dynamic ,what I want is when it's height is greater than 650px add class ".over". 我有一个带有动态内容的“ div#area”,所以div的高度也是动态的,当高度大于650px时,我想要的是添加类“ .over”。 I use this script and it's not working: 我使用此脚本,但无法正常工作:
<script>
if ($('div#area').height() > 650) {
$(this).addClass('over');
}
</script>
this
doesn't refers to $('div#area')
when you execute the addClass
statement. 当执行addClass
语句时, this
并不指向$('div#area')
。 You can store the reference in a variable and use it. 您可以将引用存储在变量中并使用。
var elem = $('div#area'); //Store a refrence to element
if (elem.height() > 650) {
elem.addClass('over');
}
Try this 尝试这个
<script>
$(document).ready(function(){
if ($('#area').height() > 650) {
$('#area').addClass('over');
}
})
</script>
<script>
var area = $('div #area');
if (area.height() > 650) {
area.addClass('over');
}
</script>
<script>
if ($('#area').height() > 650) {
$(this).addClass('over');
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.