disable text selection and add exceptions

2019-06-01 10:11发布

if i use this style for the <body>

onmousedown='return false;' onselectstart='return false;'

is it possible to give the exceptions by using javascript for inputs textareas and selects? and if yes then can u show me how?

2条回答
太酷不给撩
2楼-- · 2019-06-01 10:30

Actually, you can disable text selection without needing heavy-handed JavaScript event handlers. See my answer here: How to disable text selection highlighting using CSS?

Summary: use CSS and the unselectable expando in IE.

CSS:

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   -o-user-select: none;
   user-select: none;
}

HTML:

<div id="foo" unselectable="on" class="unselectable">...</div>
查看更多
干净又极端
3楼-- · 2019-06-01 10:41

You need to prevent event bubbling for textboxes and selects.

You can use :input selector to select all input, textarea, select and button elements.

Something like

$(":input").bind("mousedown", function(e){
    e.stopPropagation();
});

See a working demo

查看更多
登录 后发表回答