问题
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