jQuery 插件 if else 子句不起作用?

发布于 2025-01-01 01:10:27 字数 401 浏览 4 评论 0原文

我正在编写一个 jQuery 插件,我正在尝试这样做:

$(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"});
}

I'm writing a jQuery plugin and I am trying to do this:

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

however it's not working, I have used this method in my javascript before.

This works how it should, just i was trying to use little code as possible.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2025-01-08 01:10:27

将其更改为:

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

您忘记了 .height() 中的 ()

你也可以使用这个:

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

Change it to:

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

You forgot the () in .height().

You could also use this:

$(this).css({overflow: $(this).height() >= options.maxHeight ? 'auto' : 'hidden'});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文