Evitar la selección de texto después de hacer doble clic

Resuelto David asked hace 15 años • 15 respuestas

Estoy manejando el dblclickevento en un lapso en mi aplicación web. Un efecto secundario de un doble clic es que selecciona texto en la página. ¿Cómo puedo evitar que ocurra esta selección?

David avatar May 19 '09 07:05 David
Aceptado
function clearSelection() {
    if(document.selection && document.selection.empty) {
        document.selection.empty();
    } else if(window.getSelection) {
        var sel = window.getSelection();
        sel.removeAllRanges();
    }
}

También puede aplicar estos estilos al intervalo para todos los navegadores que no sean IE e IE10:

span.no_selection {
    user-select: none; /* standard syntax */
    -webkit-user-select: none; /* webkit (safari, chrome) browsers */
    -moz-user-select: none; /* mozilla browsers */
    -khtml-user-select: none; /* webkit (konqueror) browsers */
    -ms-user-select: none; /* IE10+ */
}
Paolo Bergantino avatar May 19 '2009 00:05 Paolo Bergantino

Para evitar la selección de texto SÓLO después de un doble clic:

Podrías usar MouseEvent#detailla propiedad. Para eventos mousedowno mouseup, es 1 más el recuento de clics actual.

document.addEventListener('mousedown', function(event) {
  if (event.detail > 1) {
    event.preventDefault();
    // of course, you still do not know what you prevent here...
    // You could also check event.ctrlKey/event.shiftKey/event.altKey
    // to not prevent something useful.
  }
}, false);
Some dummy text
Expandir fragmento

Consulte https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail

4esn0k avatar Apr 10 '2017 11:04 4esn0k