简体   繁体   中英

How to make editable textarea text non-selectable

I'm trying to create a textarea that is not read-only (users can type), but they cannot select and drag.

All that I found either turns the textarea into readonly, or disables my ability to focus.

Appreciate any help.

In jQuery 1.8, this can be done as follows:

$('textarea')
    .attr('unselectable', 'on')
    .css('-webkit-user-select', 'none')
    .css('-moz-user-select', 'none')
    .css("-ms-user-select","none")
    .css("-o-user-select","none")
    .css("user-select",'none')
    .on('selectstart', false)
    .on('mousedown', false);

or by just using css,

#yourtextarea
{
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

You can use the CSS style user-select: none; to keep text from being selectable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM