How to prevent double-clicking on a web page text with JavaScript?

My HTML page has an element with text on it. I have assigned an action that will happen when this item is clicked. When users click on an item many times in a row, sometimes a double click occurs, which causes the selection of text (selection), which leads to damage to the appearance. I tried to prevent it from using an event handler that prevents the default behavior of a double-click event (dblclick). However, this does not work. It seems that the text is selected and selected before my dummy event handler is even executed.

function doNothing(event) {
  alert('doNothing'); // Added for debugging. The alert is shown, meaning the event handler is invoked. When I get the alert, the text is already highlighted.
  if(!event) event = window.event;
  if(event.preventDefault) event.preventDefault();
  event.cancelBubble = true;
  if(event.stopPropagation) event.stopPropagation();
  return false;
}

myElem.onlick = doSomething; // This is the event for a single click. Works well.
myElem.ondblclick = doNothing; // The event handler is called, but the text is already highlighted.

Concrete questions:

1) -, ? ( , , , . , - , () .)

2) dblclick, ? ?

3) dblclick, - ? (, , preventDefault() cancelBubble = true stopPropagation() return false . , ?)

4) , ( )?

FF 11 ( - ).

!

+3
2
myElem.onmousedown = function(e) {
    e.preventDefault()
}

edit: return fasle e.preventDefault, click.

+4

event.preventDefault() dblclick, . , , - , , .

, - , , , :

  1. , : window.getSelection(). Empty(). , , Opera ("", "").
  2. e.PreventDefault() dblclick.
0

All Articles