2012-02-01 29 views
0

我正在寫一個jQuery插件,我試圖做到這一點:jQuery插件if else子句不工作?

$(this).height>=options.maxHeight ? 
$(this).css({overflow:"auto"}) : 
$(this).css({overflow:"hidden"}) 

但它不工作,我已經在我的JavaScript之前使用此方法。

這工作應該如何,只是我試圖儘可能使用小代碼。

if($(this).height()>=options.maxHeight) 
{ 
    $(this).css({overflow:"auto"}); 
} 
else 
{ 
    $(this).css({overflow:"hidden"}); 
} 
+3

沒忘了()在$(本).height? – 2012-02-01 01:13:44

+0

馬里奧說什麼。此外,「儘可能少的代碼」不會自動更好。我認爲原始版本更清晰。 – 2012-02-01 01:14:54

+0

啊!我甚至沒有注意到,謝謝!我不知道我是如何刪除它的。 – 2012-02-01 01:15:58

回答

1

將其更改爲:

$(this).height() >= options.maxHeight ? 
$(this).css({overflow:"auto"}) : 
$(this).css({overflow:"hidden"}) 

您在.height()忘了()

你也可以使用此:

$(this).css({overflow: $(this).height() >= options.maxHeight ? 'auto' : 'hidden'}); 
+0

注意:第二個例子也缺少()的高度 – 2012-02-01 01:26:37

+0

@DylanCross - 我修正了thx。 – jfriend00 2012-02-01 01:27:37