如果您将所有元素 ID 放入一个数组中,然后使用它来获取下一个项目,您可以消除对 ID 编号确定它们旋转顺序的依赖,并且还可以防止它们需要遵循这种严格的格式.
//add all element IDs to this array
var elements = ["firstElementID","div2","someConentsID","lastElementID"];
var currentIndex = 0;
//ensure that the first item is visible at the start.
function next()
{
//hide current item.
document.getElementById(elements[currentIndex]).Style = "display:none";
//move up the current index by one, wrapping so as to stay within array bounds.
currentIndex = (currentIndex + 1) % elements.length;
//show the new current item.
document.getElementById(elements[currentIndex]).Style = "display:inline";
}
您可以调整显示/隐藏代码以使用 JQuery 或您想要的任何其他机制。