How can I prevent selecting text in Google Chrome?

后端 未结 2 855
迷失自我
迷失自我 2021-02-05 09:51

Doesn\'t oEvent.preventDefault(); work in GC? I need to prevent selecting text when the onmove event is triggered.

EDIT: It turns out to be very easy...

2条回答
  •  既然无缘
    2021-02-05 10:19

    To do it using JavaScript:

    var el = document.getElementById("myElement"), s = el.style;
    s.userSelect = "none";
    s.webkitUserSelect = "none";
    s.MozUserSelect = "none";
    el.setAttribute("unselectable", "on"); // For IE and Opera
    

    Note that for IE and Opera, the unselectable attribute isn't inherited by an element's children, so any child elements of el will also need unselectable to be set to "on".

提交回复
热议问题