阻止contentEditable中的行/段落 [英] Prevent line/paragraph breaks in contentEditable

查看:142
本文介绍了阻止contentEditable中的行/段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox中使用contentEditable时,有没有办法阻止用户通过按Enter或Shift + Enter来插入段落或换行符?

解决方案

您可以将事件处理程序附加到contentEditable字段的keydown或keypress事件,如果键码自身标识为enter(或shift + enter),则可以取消该事件。

如果焦点位于contentEditable字段,则会禁用enter / shift +完全输入。



如果使用jQuery,如下所示:

  $(#idContentEditable).presspress(function(e){return e.which!= 13;}); 

...将在返回时返回false并取消按键事件。


When using contentEditable in Firefox, is there a way to prevent the user from inserting paragraph or line breaks by pressing enter or shift+enter?

解决方案

You can attach an event handler to the keydown or keypress event for the contentEditable field and cancel the event if the keycode identifies itself as enter (or shift+enter).

This will disable enter/shift+enter completely when focus is in the contentEditable field.

If using jQuery, something like:

$("#idContentEditable").keypress(function(e){ return e.which != 13; });

...which will return false and cancel the keypress event on enter.

这篇关于阻止contentEditable中的行/段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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