【发布时间】:2012-01-11 16:25:12
【问题描述】:
我正在尝试设置一个旋转面板,但是我有三种状态:活动、非活动、禁用。
我只想在活动和非活动面板之间旋转并跳过禁用的面板。如果没有更多非活动面板旋转回第一个面板。
但是,使用下面的代码单击按钮,它将选择面板 1,然后选择面板 2,然后返回面板 1,而不是选择面板 5。如果我从下面的粗体部分删除非选择器,它会按预期工作。我认为这是我对下一个运营商的理解(或缺乏理解)。有什么想法吗?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#rotate").click(function () {
var ActivePanel = $('.active'); //Get the current active panel
var NextPanel = $('.active').next('.inactive:not(.disabled)'); //Get the next panel to be active.
if (NextPanel.length == 0) NextPanel = $("#panel1"); //check for null next. if so, rotate back to the first panel
console.log("Active Panel: ", ActivePanel);
console.log("Next Panel: ", NextPanel);
$(ActivePanel).removeClass("active").addClass("inactive");
$(NextPanel).removeClass("inactive").addClass("active");
});
});
</script>
</head>
<body>
<button id="rotate">Rotate Active Panel</button>
<div id="panel1" class="active"><p>Some Content</p></div>
<div id="panel2" class="inactive"></div>
<div id="panel3" class="inactive disabled"></div>
<div id="panel4" class="inactive disabled"></div>
<div id="panel5" class="inactive"></div>
</body>
</html>
【问题讨论】: