Jquery文本更改事件 [英] Jquery text change event

查看:128
本文介绍了Jquery文本更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文本框的内容发生变化时随时触发事件。

I need to fire an event anytime the content of a textbox has changed.

我无法使用 keyup 也无法使用按键

I cant use keyup nor can I use keypress.

按住按键时,按键和按键不起作用。

Keyup and keydown doesn't work if you hold down on the key.

Keypress在文本实际发生变化之前触发。它无法识别退格或删除。

Keypress triggers before the text has actually changed. It doesn't recognize backspace or delete either.

所以现在我假设我将不得不构建一些自定义逻辑或下载插件。那里有插件吗?或者如果我应该构建一个,我应该注意哪些约束?

So now I'm assuming I'm going to have to build some custom logic or download a plugin. Are there any plugins out there? Or if I should build one, what constraints should I look out for?

例如。 Facebook通过顶级搜索来实现这一目标。你可以按住。

For eg. Facebook does it with their search at the top. you can press and hold.

另一个例子是写一个stackoverflow问题。在编辑器的正下方,内容被实时复制,退格并且每个都可以正常复制。他们是怎么做到的?

another example is writing a stackoverflow question. Right below the editor, the contents are copied in real time, backspace and everythng works. How do they do it?

推荐答案

我刚刚看了一下SO的来源。看起来他们做了很多像这个的事情:

I just took a look at SO's source. It looks like they do something a lot like this:

function updatePreview(){
    $('div').text($('textarea').val());
}

$('textarea').bind('keypress', function(){
        setTimeout(updatePreview, 1);
    }
);​

他们做了一些额外的工作来制作粗体和斜体以及链接的HTML标签等等。他们计时如果生成HTML需要太长时间,它们会将延迟从1增加到更长。

They do some extra stuff to make HTML tags for bold and italics and links and such and they time it. They increase the delay from 1 to longer if it takes too long to generate the HTML.

这篇关于Jquery文本更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆