[英]Place caret before / after image in contenteditable
I have a contenteditable div for a chat. 我有一个内容可编辑的div聊天。 I added a function for adding emoji to the text.
我添加了将表情符号添加到文本的功能。 When I'm clicking on the emoji in the contenteditable, I want that the caret is placed before or after the image.
当我单击contenteditable中的表情符号时,我希望将插入号放置在图像之前或之后。
.chat-text-box img { width: 100%; height: 100%; } .chat-text-box emoji { width: 18px; height: 18px; display: inline-block; }
<div class="chat-text-box" contenteditable="true">This is a emoji: <emoji class="emoji-icon" contenteditable="false" data-emoji="&#x1F605;"><img draggable="false" src="https://twemoji.maxcdn.com/2/svg/1f605.svg"></emoji> </div>
You can create that caret with a pseudo-element ::after
when the text-box has :focus
当文本框具有
:focus
时,可以使用伪元素::after
来创建插入符号。
More about pseudo-elements MDN 有关伪元素MDN的更多信息
.chat-text-box img { width: 100%; height: 100%; } .chat-text-box emoji { width: 18px; height: 18px; display: inline-block; /* added this so you can use position: absolute on the ::after */ position: relative; } .chat-text-box:focus emoji::after { content: '>'; display: block; width: 1rem; height: 1rem; position: absolute; bottom: 0; right: -1.5rem; }
<div class="chat-text-box" contenteditable="true">This is a emoji: <emoji class="emoji-icon" contenteditable="false" data-emoji="&#x1F605;"><img draggable="false" src="https://twemoji.maxcdn.com/2/svg/1f605.svg"></emoji> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.