//开始不跑,原因是函数必须放在调用函数之前!!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>练习</title> <script type="text/javascript"> onload = function () { var txt = document.getElementById("txt"); var txt2 = txt.innerHTML.trim(); // alert(txt.innerHTML.trim().charAt(0)); //alert(txt.innerHTML.trim().substr(0,2)); //函数放在前面!! var paoMaDeng = function () {
//两个获取字符的方法,charAt(0)和substr(0,1) var first = txt2.charAt(0); var rest = txt2.substr(1); txt2 = rest + first; txt.innerHTML = txt2; }
//调用放在后面!!! aaa = setInterval(paoMaDeng, 100); } </script> </head> <body> <p id="txt"> 北京欢迎你!</p> <input type = "button" value = "停止" id = "btn" onclick="clearInterval(aaa);"/> </body> </html>