我的下面的代码与我的文字与下一个&上一个按钮。我希望它在页面加载时重复循环,并在两者之间暂停一个按钮。所有三个按钮完全位于显示文本的同一行的右侧。请支持我..谢谢......
<!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;
function nextMsg(direction) {
msgPtr = msgPtr + direction;
if (msgPtr < 0) { msgPtr = messages.length-1; }
if (msgPtr > messages.length-1) { msgPtr = 0; }
document.getElementById('msg').innerHTML = messages[msgPtr];
}
window.onload = function () {
nextMsg(0);
}
</script>
<style type="text/css">
em { color:orange; }
#msg {
font-family:monospace;
background-color:yellow;
font-size:1em;
width:890px;
border:1px dotted red;
overflow:hidden;}
</style>
</head>
<body>
<div id="msg"></div><br>
<button onclick="nextMsg(-1)"><</button>
<button onclick="nextMsg(-1)">||</button>
<button onclick="nextMsg(1)">></button>
</body>
</html>
答案 0 :(得分:1)
一种解决方案是以递归方式调用nextMsg(),如
function nextMsg(direction){
//snip
nextMsg(direction);
}
我用jQuery写了一个非常快速的概念证明,应该以更清晰的方式重写: http://jsfiddle.net/5NUPn/5/
现在,要在同一行显示消息和按钮,您需要使用float。
#msg { float: left; }
答案 1 :(得分:0)
使用&#39; setInterval(code,millisec)&#39;功能。所以你的代码应该是这样的:
var msgPtr =0;
var set_interval_id;
function pause() {
clearInterval(set_interval_id)
}
function play() {
set_interval_id = setInterval(function() {
nextMsg(msgPtr);
msgPtr++;
}, 2000 /* how often to execute the code (2 sec) */);
}
window.onload = function () {
play();
}
...
<button onclick="nextMsg(-1)"><</button>
<button onclick="pause()">||</button>
<button onclick="nextMsg(1)">></button>
....