这是适用于所有情况的 Fiddle 示例,不使用固定的元素大小,它取决于集合中每个元素的大小
var text = ['ta', 't', 'a', 'be', 'ru'];
var offset = 0;
var index = 0;
var happy = ['http://www.vendian.org/howbig/UnstableURLs/letter_a_poster.png',
'http://chineseknotting.org/projects/knotty/T-carrick.jpg',
'http://etc.usf.edu/presentations/extras/letters/varsity_letters/38/17/E-400.png',
'http://chineseknotting.org/projects/knotty/T-carrick.jpg',
'http://etc.usf.edu/presentations/extras/letters/varsity_letters/38/17/E-400.png'];
var grumpy = $('img').map(function(){ return this.src; }).get(); //gets the original images from the HTML
$("#test").on('keyup', function() {
var typed = this.value;
if (typed.substr(offset, text[index].length) == text[index]) {
$('li', 'ul').eq(index).find('img').attr('src', happy[index]);
offset += text[index].length;
index++;
}else{
$('li', 'ul').eq(index).find('img').attr('src', grumpy[index]);
}
});
我已经更新了脚本的逻辑,更新的版本可以在这里找到(感谢 Internal Server Error)</p>