简体   繁体   English

如何使用jQuery一次又一次地更改文本?

[英]How to change text after time using jQuery?

I found this code on stackoverflow HERE , but it's not working for me... 我在这里的 stackoverflow上找到了这个代码,但它对我不起作用......

I can see this only: 我只能看到这个:

Hello world!
Here is a message: 

,but I don't see the messages that should be changing after time... ,但我没有看到时间之后应该改变的消息......

I copy/paste the entire code from my file index.html: 我从我的文件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>

Thanks in advance :) 提前致谢 :)

Just change the order dont execute javascript at the beginning of your code always at the end and/or use document ready to be sure dom is loaded before executing js 只需更改顺序,不要在代码开头始终在最后执行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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM