【发布时间】:2017-04-18 13:12:15
【问题描述】:
单击带有图标的按钮后,我试图在我的 contenteditable div 中创建一个图标。问题是,在选择图标/按钮时,我丢失了文本中的插入符号。关闭按钮后,我希望这个插入符号回到原来的位置。
我做了一个 JS-Fiddle 来说明这个问题:JSFiddle
PS:这个问题尤其是关于插入符号的。是否插入图标对我来说并不重要。那部分应该很容易弄清楚:)。
html
<div id="write-mail">
<div style="position: relative;">
<div id="new-mail-content" contenteditable="true">
<div>How to return the caret where it was in the sentence after clicking the buttons?</div>
<div>Maybe prevent the caret from being removed after clicking the buttons?</div>
<div>I know I can store the current selection by using window.getSelection(). If I would be able to reactivate this selection, that would be great!</div>
</div>
<div id="emotepicker">
<button><span>✔</span></button>
<button><span>✯</span></button>
<button><span>✘</span></button>
</div>
</div>
<button class="add-icon">Add Icon</button>
</div>
css
#new-mail-content{
background-color: white;
min-height: 5em;
}
#write-mail button{
height: 30px;
}
#emotepicker{
display: none;
position: absolute;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(0,0,0,0.1);
text-align: center;
}
div{
padding: 2px;
}
jquery/javascript
$(".add-icon").click(function(){
$("#emotepicker").toggle();
});
【问题讨论】:
-
@Felix Häberle 你对我的 css 的更改应该有什么帮助?
-
看看stackoverflow.com/questions/3972014/… 开始编辑时保存插入符号位置,关闭表情面板时恢复。
-
我发布的示例对您有帮助吗?
-
随时为有帮助的答案投票。 ;)
标签: javascript jquery html contenteditable caret