Any way to prevent “deselection” of highlighted text?

前端 未结 6 891
南旧
南旧 2020-12-10 13:54

Highlight some text on this webpage, then click basically anywhere on the document. Your selection will disappear.

Is there a way to prevent this behavior when the

6条回答
  •  伪装坚强ぢ
    2020-12-10 14:14

    "return false" as well as "e.preventDefault()" in onmousedown works in FF and Safari, but not IE. The only solution for IE, as far as I can tell, is to throw an error.

    This works in all browsers (but causes an error in IE, since preventDefault is not a method):

    //clicking the 'test' element will not deselect text.
    var test = document.getElementById("test");
    test.onmousedown = function(e){
      e = e || window.event;
      e.preventDefault();
    }
    

    I'd still like to do this error-free in IE, if possible

    Thanks to Paolo Bergantino for the the "onmousedown" tip.

提交回复
热议问题