我有几个div,访问者可以通过"上一个"answers"下一个"按钮进行切换。这是我写的代码,运行良好:
$("#next").click(function(){
$(".open").removeClass("open").fadeOut(200, function(){ // remove .open from current opened detail and fade out
$(this).next(".outsourcing").addClass("open").fadeIn(400); // add .open to next detail and fade in
if ($(".open").hasClass(".item<?php echo $outsourcing_count-1; ?>")) {
$("#next").hide();
}
})
});
关于最后一项,我想隐藏"下一步"按钮。最后一项不是包含div中的最后一个子项,所以我使用PHP用一个特殊的类标记它,正如您在上面的if语句中看到的那样。但if的声明似乎并没有开火,有什么想法吗?谢谢
删除项..之前的"."。。当你使用hasClass 时,你不必给出类标识符
if ($(".open").hasClass("item<?php echo $outsourcing_count-1; ?>")) {
$("#next").hide();
}
您不需要使用PHP注入的值来测试它是否是最后一项
if ($(".open").is(':last-child'))
{
// must be the last item
}
将执行,或者(如果它不是该容器中的最后一个子项)只检查是否有next
项:
if (!$(".open").next(".outsourcing").length)
{
// must be the last item
}
不管怎样,你会明白的。只需测试特定的DOM配置,而不是打乱面板编号。如果不需要,就不应该添加依赖项。