我以http://jsfiddle.net/5NUPn/5/的形式得到了我上一个问题的答案,但是当我将代码放到我的普通 html 页面时,它的播放效果不一样。请让我知道我在哪里做错了...
下面是我的 HTML 中的代码
<!DOC html>
<html>
<head>
<script type="text/javascript">
var messages = [
'Messages for <em>kindergarden</em> class',
'Message for <em>1st grade</em>',
'Message for <em>2nd grade</em>',
'Message for <em>3rd grade</em>',
'Message for <em>4th grade</em>',
'Message for <em>5th grade</em>',
'Message for <em>6th grade</em>'
];
var msgPtr = 0;
var player;
function nextMsg(direction, loop) {
msgPtr = msgPtr + direction;
if (msgPtr < 0) { msgPtr = messages.length-1; }
if (msgPtr > messages.length-1) { msgPtr = 0; }
document.getElementById('msg').innerHTML = messages[msgPtr];
if(loop){
player = setTimeout(function(){ nextMsg(1, true); }, 1000);
}
}
function toggleMsg()
{
clearTimeout(player);
}
nextMsg(0, true);
$(function(){
$('.prev').click(function(){
clearTimeout(player);
nextMsg(-1, false);
$('.playpause').removeClass('playing').addClass('stopped').text('play');
});
$('.next').click(function(){
clearTimeout(player);
nextMsg(1, false);
$('.playpause').removeClass('playing').addClass('stopped').text('play');
});
$('.playpause').click(function(){
if($(this).hasClass('stopped'))
{
nextMsg(0, true);
$(this).removeClass('stopped').addClass('playing').text('pause');
}
else
{
clearTimeout(player);
$(this).removeClass('playing').addClass('stopped').text('play');
}
});
});
</script>
<style type="text/css">
em { color:orange; }
#msg {
font-family:monospace;
background-color:yellow;
font-size:1em;
border:1px dotted red;
overflow:hidden;
/*float: left;*/
}
#buttons { /*float: right;*/ }
</style>
</head>
<body>
<div id="msg"></div>
<div id="buttons">
<button class="prev"><</button>
<button class="playpause playing">pause</button>
<button class="next">></button>
</div>
</body>
</html>