話不多說,請看實例代碼:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta name="keyword" content=""><meta name="description" content=""></head><body><div class="autotype" id="autotype"> <p>一場雨把我困在這里</p> <br/> <p>你溫柔的表情</p> <p>會讓我傷心</p> <br/> <p>六月的雨,只是無情的你~</p></div><script src="http://file2.ci123.com/ast/js/jquery_172.js"></script><script> $.fn.autotype = function(){ var $text = $(this); console.log('this',this); var str = $text.html();//返回被選 元素的內容 var index = 0; var x = $text.html(''); //$text.html()和$(this).html('')有區別 var timer = setInterval(function(){ //substr(index, 1) 方法在字符串中抽取從index下標開始的一個的字符 var current = str.substr(index, 1); if(current == '<'){ //indexOf() 方法返回">"在字符串中首次出現的位置。 index = str.indexOf('>', index) + 1; }else{ index ++ ; } //console.log(["0到index下標下的字符",str.substring(0, index)],["符號",index & 1 ? '_': '']); //substring() 方法用于提取字符串中介于兩個指定下標之間的字符 $text.html(str.substring(0, index) + (index & 1 ? '_': '')); if(index >= str.length){ clearInterval(timer); } },100); }; $("#autotype").autotype();</script></body></html>
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答