I'm trying to achieve a typewriting effect and my content consists of a lot of HTML entities. The problem with using .html() is that since it writes out each letter at a time, it will type out &
then l
then t
then ;
and finally it would change to <
.
HTML
<p id="src">
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if lt IE 9]> <html class="no-js lt-ie9"> <![endif]-->
</p>
<p id="typed-paragraph">
<span id="target"></span>
<span id="typed-cursor">|</span>
</p>
CSS
#src {
display: none;
}
jQuery
(function(){
var srcText = $("#src").html();
i = 0;
result = srcText[i];
setInterval(function() {
if(i == srcText.length-1) {
clearInterval(this);
return;
};
i++;
result += srcText[i].replace("
", "<br />");
$("#target").html(result);
}, 150); // the period between every character and next one, in milliseonds.
})();
You can see the example of it here
http://jsfiddle.net/j9KF5/9/
But if I use .text() then I lose all the line breaks.
Ultimately, how I can either fix the entities problem or the line break problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…