disable text selection and add exceptions

守給你的承諾、 提交于 2019-12-08 03:16:48

问题


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?


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/4712030/disable-text-selection-and-add-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!