我是这样写的:
$('[name=item-title]').live('keyup',function(k){
char_limit=parseInt($('[data-charlimit]').attr('data-charlimit'));
total = char_limit-parseInt($(this).val().length);
$('.charcount span').text(char_limit-parseInt($(this).val().length));
});
但在前几个单词之后,它开始变得非常慢,我将在其中输入,这些单词将在一毫秒后显示。如果我喜欢 250 个单词,它看起来会落后近 3/4 秒。任何想法为什么?对我来说,我写的东西似乎很少......
最佳答案
您正在对每个字符进行 DOM 遍历,但您不确定为什么它很慢?
// calculate upper limit once -- it won't change, presumably
var char_limit=parseInt($('[data-charlimit]').attr('data-charlimit'));
$('[name=item-title]').live('keyup',function(k){
// length is already numeric, no need to parse it
var remaining = char_limit - $(this).val().length;
$('.charcount span').text( remaining ); // use the result, note I renamed it
});
关于javascript - 字符计数器超慢 - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3144826/