gt 的条件 if/then:在 jQuery 中

发布于 2024-09-14 13:20:59 字数 168 浏览 3 评论 0原文

你怎么说:

if there are more than 2 .quick_fact then

我的尝试:

if $(".quick_fact:gt(2)") { }

这显然总是正确的。

How do you say :

if there are more than 2 .quick_fact then

My attempt:

if $(".quick_fact:gt(2)") { }

Which evidently is always true.

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

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

发布评论

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

评论(2

柒七 2024-09-21 13:20:59

jQuery 对象有一个 length 属性,表示匹配元素的数量。因此,只需进行典型的“大于”比较即可。

if( $('.quick_fact').length > 2 ) {
    // There were more than two
}

A jQuery object has a length property representing the number of matched elements. So simply do a typical "greater than" comparison.

if( $('.quick_fact').length > 2 ) {
    // There were more than two
}
新人笑 2024-09-21 13:20:59
if ($(".quick_fact").size() > 2 ) { }

或者:

if ($(".quick_fact").length > 2 ) { }
if ($(".quick_fact").size() > 2 ) { }

Or:

if ($(".quick_fact").length > 2 ) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文