实现ChatGPT的文字输出效果 酷酷的马: 加了一个JQ的写法:function typeText() { const outputElement = $("#output"); const cursorElement = $("#cursor"); const givenTextElement = $("#givenText"); const givenText = givenTextElement.html(); let currentIndex = 0.1; let currentHTML = ""; function animateTyping() { if (currentIndex < givenText.length) { const currentChar = givenText.charAt(currentIndex); if (currentChar === "", currentIndex); currentHTML += givenText.slice(currentIndex, closingTagIndex + 1); currentIndex = closingTagIndex + 1; } else { currentHTML += currentChar; currentIndex++; } outputElement.html(currentHTML); setTimeout(animateTyping, 100); // 设置打字速度,单位为毫秒 } else { // 当打印完成时,移除光标的闪烁效果 cursorElement.removeClass("cursor"); } } animateTyping(); } $(document).ready(function() { typeText(); }); |