Losing selected text from contenteditable on clicking div with css user-select: none;

此生再无相见时 提交于 2020-01-05 07:29:30

问题


I'm developing a JavaScript rich text editor. Instead of buttons, I'm using div with css property user-select: none. This works fine in Firefox but problem is with Google Chrome.

When I click on div with css user-select: none, I'm losing the selected text from contenteditable div. Html and css is given below

<div id="editor">
   <div id="controls">
      <div id="button-bold"></div>
   </div>
   <div id="rte" contenteditable></div>
</div>

CSS

#button-bold
{
    width: 20px;
    height: 20px;
    padding: 2px;
    float: left;
    border: 1px solid #E7E7E5;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
#rte {
    width: 100%;
    min-height: 400px;
    margin: 5px;
    outline: none;
}

回答1:


I have a similar setup, and the following solution works fine in Chrome: How to disable text selection using jQuery?

The answer is to add a handler for the 'selectstart' event.




回答2:


Use buttons, intead of div or span for making clickable links to change style.

And then use document.execcommand('forecolor',false,'#000000');



来源:https://stackoverflow.com/questions/12197580/losing-selected-text-from-contenteditable-on-clicking-div-with-css-user-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!