简体   繁体   English

试图将插入符号(跨浏览器)放在行尾

[英]Trying to put caret (cross-browser) at the end of line on focus

Consider the following contenteditable element 考虑以下contenteditable元素

<div contenteditable>
    Foo bar <span contenteditable="false"></span>x</div>

DEMO 演示

Now, the nice thing going on here is that whatever browser you take, the caret is always placed after the x if you click somewhere inside. 现在,这里发生的一件好事是,无论您使用哪种浏览器,如果单击内部的某个位置,插入号始终位于x后面。

But, because I don't want the x at the end of the line and you remove the x the behaviour of where the caret is placed is different (safari and Edge do strange things). 但是,因为我不想在行尾加上x ,而又删除了x ,所以插入标记放置位置的行为是不同的(野生动物园和Edge做奇怪的事情)。 So, I figured that when the last line of a contenteditable ends with a span which is not editable you will have problems, so I did 因此,我发现当contenteditable的最后一行以不可编辑的span结尾时,您会遇到问题,所以我做了

<div contenteditbale>Foo Bar <span con...="false"><span></span></div>

Which actually doesn't help at all, same behaviour. 相同的行为实际上根本没有帮助。 It seems that the caret will not show inside an empty span 看来插入符号不会显示在空白范围内

Any suggestions how to fix this? 任何建议如何解决此问题?

So are you saying the caret is going inside the span, or is it a separate element? 那么,您是说插入符号在跨度内部,还是单独的元素? You could create a CSS Pseudo Element to position the caret where you would like it. 您可以创建一个CSS伪元素 ,将插入符放置在所需的位置。 Just use: 只需使用:

div::after {
    content: url(caret.png);
}

Here's an updated DEMO 这是更新的演示

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

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