[英]How to change text after time using jQuery?
我在這里的 stackoverflow上找到了這個代碼,但它對我不起作用......
我只能看到這個:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.