FireFox 中内容可编辑的光标位置/样式
我在 FireFox 3 中使用 contentEditable 时遇到问题。我遇到的问题是,在单击 div 后,光标将出现在 div 上方或仅部分出现在 div 中(直到我开始输入,此时它的行为正确)。关于如何阻止这种情况发生有什么想法吗?
HTML:
<html> <head><title>Test Page</title></head> <body> <div id="editor" style="position:absolute; left:157px; top:230px; width:120px; height:30px"> <div id="input" style="width:100%; height:100%; border:1px solid black; outline:none" contentEditable="true"> </div> </div> </body> </html>
I'm having trouble using contentEditable in FireFox 3. I have a problem where the cursor will appear above or only partially in the div after I click in it (until I start typing at which time it behaves correctly). Any ideas on how I can stop this from happening?
HTML:
<html> <head><title>Test Page</title></head> <body> <div id="editor" style="position:absolute; left:157px; top:230px; width:120px; height:30px"> <div id="input" style="width:100%; height:100%; border:1px solid black; outline:none" contentEditable="true"> </div> </div> </body> </html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在 Firefox 37.0.2 上遇到了完全相同的问题。在 contenteditable 的 :before 伪元素中放置零宽度空格可以修复该问题:
该修复适用于所有现代浏览器,包括 IE11,它也有与 Firefox 非常相似的插入符位置问题。
I am having the exact same issue with Firefox 37.0.2. Putting a zero width space in the contenteditable's :before pseudo element fixes the issue:
The fix works in all modern browsers, including IE11, which also has a caret position issue quite similar to Firefox's.
您需要在您想要编辑的 DIV 之间放置某种内容或 HTML:
You need to put some sort of content or HTML between the DIV that you want editable:
我花了几个小时绞尽脑汁试图找到解决这个问题的方法。我想到的是将编辑器包装在默认情况下隐藏的 div 中。然后使用一些内联 JavaScript 来显示 div。这似乎使光标跳到正确的位置。虽然很脏,但是很管用!
I was rattling my brain for hours trying to find a way to hack around this. What I came up with was to wrap the editor in a div which is hidden by default. Then use a little inline javascript to display the div. This seems to make the cursor jump into the correct position. Its dirty, but it works!
我在最新版本的FF 22中也遇到了这个问题。就我而言,我的外部div(例如上面的“编辑器”)没有高度,并且我的光标位置位于contenteditable div下方。通过为外部 div 提供高度,例如
height: 1.5em;
,光标可以正确定位自身。I also encountered this problem in the latest version of FF 22. In my case, my outer div (e.g. "editor" as above) did not have a height, and my cursor position was below the contenteditable div. By providing a height to the outer div, e.g.
height: 1.5em;
, the cursor positioned itself correctly.这可以通过创建盲 div 并向其添加焦点来解决,然后您的浏览器不会专注于 contenteditable 元素,但用户应该单击该元素,在这种情况下,它会在正确的位置显示光标。
有一种方法,但它不能解决问题,只能让你变得聪明。
That can be solved through creating blind div and add focus to it, then your browser isn't focused on contenteditable element, but user should click on that and it that case it shows cursor in the right place.
There's one way, however it doesn't solve problem, only smarten up.
我等待并使用了只能在 Firefox 4 及更高版本中编辑的内容。我提交了这个错误以及 Mozilla 团队已为 FF 4 修复的其他一些错误。
I waited and used the content editable only in Firefox 4 and higher. I filed this and a few other bugs which the Mozilla team has fixed for FF 4.