css 設置方法:
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit瀏覽器*/
-ms-user-select: none; /*IE10*/
-khtml-user-select: none; /*早期瀏覽器*/
user-select: none;
js 設置方法:
window.getSelection? window.getSelection().removeAllRanges():document.selection.empty();
對比不同:
css 的設置方法是禁止用戶選中文字,無論是否是在拖拽效果下;
js 的設置方法是為了保證在拖拽時,禁止文字選中;
代碼示例如下:
document.onmousemove = function () { window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); }
這樣設置后,當鼠標在拖拽的時候就不會選中文字了