【问题标题】:jQuery parent slicejQuery 父切片
【发布时间】:2013-02-09 07:24:39
【问题描述】:

您好,我为某些 div 中的某些元素制作了更多的假负载。我的问题是当我点击 Load More jquery 切片并在点击我的所有元素时加载(来自我的第一个容器和第二个容器)。

这是我的代码和here is jsFiddle

$(".tohide").hide();
$(".tohide").slice(0, 3).show();

$(".more-comments").click(function(){
    var showing = $(".tohide:visible").length;
    $(".tohide").slice(showing - 1, showing + 999).show(); 
}); 

【问题讨论】:

  • 预期的行为是什么?
  • 为每个容器工作,所以当我点击第一个容器上的加载更多按钮时,只显示第一个容器中的元素,我想对每个容器的元素进行切片而不是全部。
  • ids 应该始终是唯一的

标签: javascript jquery css jquery-selectors


【解决方案1】:

使用next()children(),像这样:

$(this).next("#comments").children(".tohide").slice(showing, showing + 999).show(); 

DEMO

【讨论】:

  • 谢谢,但如果我点击第一个加载更多按钮,另一个按钮不起作用。
  • showing 变量也应限制在特定的 .comments 容器中。
  • 更新小提琴以包括#comments 特异性:jsfiddle.net/zaknotzak/327xr/4
  • 抱歉,我不确定你的意思。您能否尝试更清楚地了解它需要做什么?
  • 所以在第一个容器和第二个容器上我需要有 3 个项目可见,当我单击加载更多时,将显示与我单击的加载更多按钮相同的容器中的项目.太棒了!
【解决方案2】:

您不需要在click 中使用slice,试试这个:

Here is working jsFiddle.

$(".more-comments").click(function(){
    $(this).next('.comments').children('.tohide').show();
}); 

还要注意id 是唯一的,不要在不同的元素上使用它们。在您的示例中,它是#comments

【讨论】:

  • showing 也应限制在特定的 .comments 容器中。
  • 谢谢,但如果我点击第一个加载更多按钮,另一个按钮不起作用。
  • 谢谢 barlas,但他 100% 正常工作,第二个容器元素没有出现
猜你喜欢
  • 2014-05-06
  • 2011-01-03
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多