【问题标题】:Is there a better way to detect if nextUntil stopped regularly?有没有更好的方法来检测 nextUntil 是否定期停止?
【发布时间】:2014-07-18 17:18:04
【问题描述】:

我想知道是否有一种性能良好的方法来确定.nextUntil() 是否在未匹配指定选择器的情况下停止。

如果选择器不匹配任何元素,.nextUntil() 将返回与 .nextAll() 相同的结果。

所以可以使用:

if ($("#id").nextUntil(".someclass").length === $("#id").nextAll().length) {
  //do some stuff if there was no match
}

但是有更好的解决方案吗?

【问题讨论】:

  • 你已经有一个错字nextUnil...所以它可能不匹配任何东西:D

标签: javascript jquery performance nextuntil


【解决方案1】:

如果它没有匹配就停止,这意味着你的对象中有最后一个孩子。您可以使用以下内容:

if (!$("#id").nextUntil(".someclass").is(':last-child'))

Test it here

【讨论】:

  • 谢谢,这肯定比使用nextAll
猜你喜欢
  • 2019-09-03
  • 2021-03-01
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2014-08-31
相关资源
最近更新 更多