简体   繁体   English

在内容可编辑的div中插入符号的位置远未达到

[英]Caret position is way off in contenteditable div

Chrome (and maybe other browsers) position the caret in a strange way within a contenteditable div. Chrome(可能还有其他浏览器)在可内容编辑的div中以一种奇怪的方式放置了插入符号。

Please consider the following snippet: 请考虑以下代码段:

<div contenteditable="true" style="width: 100%; height: 200px; border: 1px solid black; overflow: auto;">
    <p>
        <span contenteditable="false" style="width: 75%; height: 80px; display: inline-block; border: 1px solid red;">&nbsp;</span>.
    </p>
</div>

Also available in this JSFiddle . 在此JSFiddle中也可用。

If you click on the right-side of the period next to the red span and press backspace to delete the period, the caret suddenly shifts to the extreme right of the paragraph. 如果单击红色跨度旁边的句点的右侧,然后按Backspace键删除该句点,则插入符号突然移至该段落的最右端。 I would expect the caret to be positioned where the period used to be, next to the red span. 我希望插入符号可以定位在该时期以前的位置,在红色跨度旁边。

Why is it not positioned the way I expect, and is there a way to get the behavior I'm looking for? 为什么它没有按照我的期望进行定位,并且有办法获得我想要的行为?

This strange behavior is happening because of the p tag, the cause is possibly some conflict between widths, you can edit the css of the tag, instead of using display:block (default), use display:inline . 由于p标签而发生这种奇怪的行为,原因可能是宽度之间存在一些冲突,您可以编辑标签的css,而不是使用display:block (默认),使用display:inline

I created this fiddle: JsFiddle , with display:inline , was the closest I could get from the display:block . 我创建了这个小提琴:具有display:inline JsFiddle是我可以从display:block获得的最接近的提琴。

I tried the align attribute but I did not succeed. 我尝试了align属性,但没有成功。

I'm fairly confident that it's the span causing it, because the moment you remove it or even display: none it, the problem goes away. 我相当有信心这是造成它的范围,因为在您删除甚至display: none它的那一刻display: none ,问题就消失了。 I got really curious about this myself and did some searching, this person seems to have the same problem as you. 我自己对此很好奇,并做了一些搜索,这个人似乎和您有同样的问题。

Why Is My Contenteditable Cursor Jumping to the End in Chrome? 为什么我的Contenteditable光标在Chrome中跳到结尾?

Non-breaking spaces are just what they sound like — spaces where a line break will not occur. 不间断空格就是它们听起来像的样子,即不会发生换行的空格。 You should not use them to add space between words, sentences, or elements. 您不应使用它们在单词,句子或元素之间添加空格。 Especially not elements. 特别是没有元素。

Remove & nbsp; 删除 (with space or will actually make space :D) and everything's good. (使用空格或将实际生成空格:D),一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM