gpt4 book ai didi

javascript - 如何使用 jQuery 在一段时间后更改文本?

转载 作者:太空狗 更新时间:2023-10-29 15:11:13 26 4
gpt4 key购买 nike

我在 stackoverflow 上找到了这段代码 HERE ,但它对我不起作用...

我只能看到这个:

Hello world!
Here is a message:

,但我没有看到应该随时间变化的消息...

我从我的文件 index.html 中复制/粘贴了整个代码:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">


</script>
</head>
<body>
<script type="text/javascript">
function nextMsg() {
if (messages.length == 0) {
alert("redirecting");
} else {
$('#message').html(messages.pop()).fadeIn(500).delay(1000).fadeOut(500, nextMsg);
}
};

var messages = [
"Hello!",
"This is a website!",
"You are now going to be redirected.",
"Are you ready?",
"You're now being redirected..."
].reverse();

$('#message').hide();
nextMsg();
</script>

<h1>Hello world!</h1>
<p>Here is a message: <span id="message"></span></p>
</body>
</html>

提前致谢:)

最佳答案

只需更改顺序,不要在代码的开头总是在末尾执行 javascript 和/或使用文档就绪以确保在执行 js 之前加载 dom

<h1>Hello world!</h1>
<p>Here is a message: <span id="message"></span></p>

<script>
$(document).ready(function() {
function nextMsg() {
if (messages.length == 0) {
alert("redirecting");
} else {
$('#message').html(messages.pop()).fadeIn(500).delay(1000).fadeOut(500,nextMsg);
}
};


var messages = [
"Hello!",
"This is a website!",
"You are now going to be redirected.",
"Are you ready?",
"You're now being redirected..."
].reverse();

$('#message').hide();
nextMsg();
});
</script>

关于javascript - 如何使用 jQuery 在一段时间后更改文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492987/

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