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
css 的设置方法是禁止用户选中文字,不管是不是在拖拽效果下;html
js 的设置方法是为了保证在拖拽时,禁止文字选中;web
代码示例以下:浏览器
document.onmousemove = function () { window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); }
这样设置后,当鼠标在拖拽的时候就不会选中文字了spa