Prevent line/paragraph breaks in contentEditable

题意:防止在 contentEditable 中出现换行/段落断开

问题背景:

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?

在 Firefox 中使用 contentEditable 时,是否有办法防止用户通过按 Enter 或 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).

你可以为 contentEditable 字段绑定一个 keydownkeypress 事件处理器,并在键码标识为 Enter(或 Shift+Enter)时取消该事件

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

当焦点位于 contentEditable 字段时,这将完全禁用 Enter/Shift+Enter

If using jQuery, something like:

如果使用jQuery,类似这样的代码:

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

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

……这将返回 false 并取消按下回车键时的按键事件。

猜你喜欢

转载自blog.csdn.net/suiusoar/article/details/143564292
今日推荐