js实现字符串一个一个依次显示

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>

 

一:
<textarea id="showStr"></textarea>
<div style="display:none" id="string">请在此输入您想咨询的问题
</div>
<script language="javascript">
var index=0;
var str=document.getElementById("string").innerHTML;
function type(){
if(index==str.length){index=0;}
    document.getElementById("showStr").innerText = str.substring(0,index++);
}
setInterval(type, 500);
</script>

 

二:

<p id="aa" class="string"></p>
<p style="display:none" id="w">请在此输入您想咨询的问题</p>

<script type="text/javascript">
window.onload = type;
var index = 0;
var word = $("#w").html();

function type(){
$("#aa").html(word.substring(0,index++));
if(index > word.length) {
return;
} else {
setTimeout(type,430);
};
}

</script>

 


</body>
</html>

 

 

原文链接:https://blog.csdn.net/liu__hua/article/details/38869615

posted @ 2018-04-25 15:17  Jweib  阅读(671)  评论(0编辑  收藏  举报