gpt4 book ai didi

Javascript 引号循环 - 完成后跳转到第一个

转载 作者:行者123 更新时间:2023-11-30 09:47:35 26 4
gpt4 key购买 nike

我的脚本有问题,因为一旦所有子元素都循环遍历,我就无法返回到第一个元素。

https://jsfiddle.net/pad5bp0o/2/

$(document).ready(function() {
var length = $("#tips li").length;
var old = 0;

function show() {
var news = getRan();
$("#tips li").hide();
$("#tips li:nth-child(" + news + ")").fadeIn("fast");
old++;
};

function getRan() {
var news = old + 1;
if (news < length) {
return news;
} else {
var news = 0 + 1;
return news;
old++;
}
};

show();
$("#something").html(length);
setInterval(show, 1000);

});
#tips,
#tips li {
margin: 0;
padding: 0;
list-style: none;
}

#tips {
width: 250px;
font-size: 16px;
line-height: 120%;
}

#tips li {
padding: 20px;
background: #e1e1e1;
display: none;
/* hide the items at first only */
}

.active {
opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="tips">
<li>... if you want to become a better coder you need to eat your vegetables?</li>
<li>... it takes more time to code a web page then to make a pizza?</li>
<li>... you should validate your code?</li>
<li>... jQuery is your friend? For real!</li>
<li>... no matter what some people claim, you can't learn CSS in 3 hours?</li>
</ul>
<div id="something"></div>

最佳答案

到达最后一个元素后,您需要重置 old 变量。

function getRan() {
var news = old + 1;
if (news <= length) {
return news;
}

news = 1;
old = 0;
return news;
};

DEMO

关于Javascript 引号循环 - 完成后跳转到第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38181569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com