【问题标题】:jQuery :contains in .not() doesn't workjQuery :contains in .not() 不起作用
【发布时间】:2016-06-15 19:17:33
【问题描述】:

我有这个,但它似乎没有做任何事情,但我不明白为什么......语法似乎很好。

$('#entries li').not(function() {
    return $('span.date:contains("March 2016")', this);
}).hide();

在我使用的另一个函数中:

$('#entries li').not(function() {
    return $('.date', this).text() === formattedDate;
}).hide();

而且那个很好用。

任何帮助将不胜感激。

【问题讨论】:

  • 使用工作解决方案然后._.
  • 你的 HTML 是什么样的?
  • 那么您的 HTML 中是否有与此匹配的元素:span.date:contains("March 2016")
  • @Rajaprabhu Aravindasamy 展示了我需要做的是使用 is()。解决了。 jo8691 - 两者实际上在做微妙不同的事情并且不可互换:-) stackErr - 是的元素存在。下面的答案是我正在寻找的。​​span>

标签: jquery contains


【解决方案1】:

$('#entries li')这个很像

$('#entries li').not(function() {
    return $('span.date:contains("March 2016")', this);
}).hide();

因为您一直在回调中返回一个真实值。您必须在此上下文中使用 .is() 函数来完成您的任务。

$('#entries li').not(function() {
    return $('span.date', this).is(':contains("March 2016")');
}).hide();

或者简单地说,

$('#entries li span.date:contains("March 2016")').hide();

【讨论】:

  • 谢谢。 is() 工作。不确定你的最后一个例子,因为我试图隐藏所有但包含日期的条目。但另一个工作完美。
  • @Funktion 是的。您现在可以使用第二个变体。我只是在那里打错字了。您想隐藏包含数据“2016 年 3 月”的 span.date。对吗?
  • 是的...... is() 工作得很好。排序。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多