如何强制
Firefox ContentEditable 中的换行符
我有一个在网站上运行的 JavaScript WYSIWYG 编辑器(与 CKEditor 不同)。
它有一个设置,当您在编辑器中按 Enter 时,IE 会创建
换行符。
这很好用,但不幸的是,Firefox(我已经用 5 和 7 进行了测试)仍然会生成
元素,并且仅在以下情况下生成
:您使用Shift + Enter。
有没有办法让 Firefox 始终在 contentEditable 中生成
元素?
I have a JavaScript WYSIWYG editor (not unlike CKEditor) running on a site.
It has a setting that makes IE create <br>
line breaks when you press Enter in the editor.
That works great, but unfortunately, Firefox (I've tested with 5 and 7) will still generate <p>
elements, and generate <br>
s only if you use Shift + Enter.
Is there a way to make Firefox always generate <br>
elements in a contentEditable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从标准来看,这个动作似乎是应该被处理的。当回车键上没有设置修饰符时,块会被破坏,并且当按下 Shift 时,会使用
。 [来源]。也就是说,这并不能解决您的问题,所以让我们解决这个问题。此行应该执行
Set 操作,以便当您按回车键时,它只会放入
标记。这是仅受 FF 支持,因此它不会影响 IE 上的任何内容。不过不知道其他浏览器怎么样。注意:如果用户再次按 Enter 且未输入任何内容,则无论如何都会创建一个新的段落标记。为了防止这种情况,您可以使用
继续活动(我的建议)。
keypress
事件捕获 Enter 并停止它,或者您可以在之前插入对于此注释来说,最困难的部分是检查
按键
上元素的状态。懒惰的解决方案(也是我推荐的解决方案,除非不这样做很重要)是在每个 Enter 键之前插入它。如果您想以其他方式执行此操作,我将检查元素的.innerHTML
并查看最后几个字符(不修剪内容)是否为< /code> (所以可能是
/\
上的正则表达式匹配)。$/
From looking at the standards, it looks like this action is the way it is supposed to be handled. Blocks are broken when no modifier is set on an enter key, and
<br>
's are used when shift is pressed. [Source].That said, that does not solve your problem, so let's work on that. This line should do
Set's it so that when you just hit return, it only puts in a
<br>
tag. This is only supported by FF, so it shouldn't affect anything on IE. Don't know about other browsers though.Note: If the user hits Enter a second time, without typing anything, it creates a new paragraph tag no matter what. To prevent this, you can catch the Enter using the
before you continue with the event (what I'd recommend).
keypress
event and stop it, or you could insert in aThe tough part for you with this note is checking on the state of the element on
keypress
. The lazy solution (and the one I would recommend unless it's important to not do this) is to just insert it before every Enter key. If you want to do it the other way, I would check the.innerHTML
for the element and see if the last few characters (without trimming the content) are<br/>
(so maybe a regex match on/\<br\s?\/?\>$/
).考虑一下:
HTML:
CSS:
JavaScript:
现场演示: http://jsfiddle.net/FhEf6/3/
我使用
'\n'
字符而不是 BR 元素(DIV 设置了white-space:pre
)。因此,当按 ENTER 时,不会将 BR 或 P 元素添加到 DIV 中。 DIV 内始终只有一个 TextNode,所有换行符均由'\n'
字符表示。Consider this:
HTML:
CSS:
JavaScript:
Live demo: http://jsfiddle.net/FhEf6/3/
I use
'\n'
characters instead of BR elements (the DIV haswhite-space:pre
set). As a result, no BR, or P elements are added to the DIV when ENTER is pressed. There is only one TextNode inside the DIV at all times and all new-lines are represented by'\n'
characters.// 不完全是你所问的,但当你想强制时这个函数可能会很有趣
// 其他浏览器 Opera、Chrome、Internet Explorer 在输入时插入常规 BR。
// 可以写得更好,但目前为止有效
// hf
// not exactly what you asked but this function may be interesting when you want to force
// the other browsers opera,chrome,internet explorer to insert a regular BR on enter.
// could be better written but works so far
// hf
我的解决方案在 Firefox 中完美运行,尽管它的出现非常出乎意料!我还在 Edge 和 Chrome 中测试了它。它基于 @Ktash 的答案,用三个消失的节点替换 nbsp:一个空的跨度(带有一个类),中间的一个
和一个包含& 的跨度。 nbsp;
。JS:
CSS:
My solution works perfectly in Firefox, although it came around quite unexpectedly! I've also tested it in Edge and Chrome. It's based on the answer of @Ktash, replacing the nbsp with three disappearing nodes: an empty span (with a class), a
.
<br>
in the middle and a span containingJS:
CSS: