用javascript實現自動輸出網頁文本,用了setTimeout(),遞歸和String.substring();兩個函數實現此功能,下面把實現代碼分享給大家。
做出的效果就像是有一個打字員在打字.
 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> </head> <body bgcolor="#ccc" onload="printer();">  <h2 align="center">文本自動輸出</h2>  <br>  <br>  <hr width="400" color="black">  <br> <form align="center">  <textarea cols="50" rows="30" id="text" style="background-color:#FF99CC; color: #330033; cursor: default; font-family: Arial; font-size: 18px" wrap=virtual></textarea> </form> </body> <script type="text/javascript">   //獲取textarea對象  var text=document.getElementById("text");  //要輸出的內容  var str=" 傳統的HTML語言不能開發交互式的動態網頁,而JavaScript卻能很好的做到這一點。JavaScript是一門相當簡單易學的網絡化編程語言,通過把她和HTML語言相互結合起來,能夠實現實時的動態網頁特效,這給網頁瀏覽者在瀏覽網頁的同時也提供了某些樂趣。";  var pos=0;  //利用遞歸和setTimeout()實現文字輸出  function printer(){   text.value=str.substring(0,pos)+"|";   //判斷是否到達結尾.如果是則一秒后再來一遍.   if(pos++>str.length){    pos=0;    setTimeout("printer()",1000);   }else{    setTimeout("printer()",50);   }  } </script> </html>以上代碼就是本文對用javascript實現自動輸出網頁文本的全部內容,希望大家喜歡。
| 
 
 | 
新聞熱點
疑難解答