jquery逐字符显示字符串

标签 jquery string recursion character

我想创建一个 jquery 脚本,它可以在 div 中逐个字符地写下字符串,就好像有人在用户查看页面时键入它一样。

我认为这可以与使用 settimeout 的递归函数一起使用。

请帮我解决这个问题。谢谢。

最佳答案

你可以自己写一个。

  • 利用setInterval()
  • 在数组中存储一条消息,并逐一弹出单词/字符
  • 完成后清除间隔

DEMO

jQuery:

// Consturct a message, transform it to an array using .split() and reverse it
// If you want to print out one word at a time instead of a character at a time
// Change .split('') to .split(' ')
var $message = 'This is a message to be rendered'.split('').reverse();

// Set the frequency of your 'pops'
var $timeout = 1000;

var outputSlowly = setInterval(function() {

    // Add text to the target element
    $('#target').append($message.pop());

    // No more characters - exit
    if ($message.length === 0) {            
        clearInterval(outputSlowly);   
    }

}, $timeout);

HTML:

<p id="target"></p>

关于jquery逐字符显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24308108/

相关文章:

php - 如何知道网址是音频还是视频?

javascript - 查询 : Get div defined in same table row

Javascript:无法获取多行表上的元素 ID

PHP - 字符串 - 删除具有特定类的 HTML 标记,包括其内容

java - 坚持递归算法

performance - 递归与手动堆栈 - 在哪种情况下首选?

javascript - DOM 准备好后检查元素是否存在

java - 有效数字字符串的 NumberFormatException

string - 在PowerShell中将字符串转换为DateTime

java - 使用递归和通用接口(interface)