简体   繁体   English

在内容可编辑的图像之前/之后放置插入号

[英]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:&nbsp; <emoji class="emoji-icon" contenteditable="false" data-emoji="&amp;#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:&nbsp; <emoji class="emoji-icon" contenteditable="false" data-emoji="&amp;#x1F605;"><img draggable="false" src="https://twemoji.maxcdn.com/2/svg/1f605.svg"></emoji> </div> 

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

相关问题 jQuery - contenteditable - 确定图像之前或之后的插入符号 - jQuery - contenteditable - Determine if caret before or after an image 如何在更换一个可疑的div的html之后将插入符号放在以前的位置? - How to place the caret where it previously was after replacing the html of a contenteditable div? 更改 contenteditable 元素的 innerHTML 后,将插入符号放回原来的位置 - Place caret back where it was after changing innerHTML of a contenteditable element 如何在contenteditable div中查找插入符号前后的字符长度? - How to find the length of characters before and after the caret in contenteditable div? 在contenteditable div中的插入位置之前和之后获取节点/元素 - Get node/element before and after caret position in contenteditable div 将插入符放置在ContentEditable Div的末尾不起作用 - Place Caret at end of ContentEditable Div does not work 内容可编辑,可防止在某些元素后插入符号 - Contenteditable prevent caret placement after a certain element 将插入符号设置为脱机后的内容可编辑div - Set caret to end in contenteditable div of after the dealine 在ContentEditable的focus()之后移动插入符号的位置<DIV> - Move caret position after focus() for ContentEditable <DIV> 如何在 contenteditable 中的 img 之后放置插入符号 - How to position caret to after an img in a contenteditable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM