Why is .disableSelection() deprecated?

让人想犯罪 __ 提交于 2019-12-02 19:54:21

You can use CSS to accomplish this in most browsers: http://jsfiddle.net/fQthQ/

* {
   -ms-user-select: none; /* IE 10+ */
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   user-select: none;
}

.selectable {
   -ms-user-select: auto;
   -moz-user-select: auto;
   -khtml-user-select: auto;
   -webkit-user-select: auto;
   user-select: auto;
}

Opera does not currently support this feature.

See the MDN page for more info: https://developer.mozilla.org/en-US/docs/CSS/user-select

A real-world case in which disabling text selection is useful is a <ul> element whose <li> have been programmed to be draggable (for sorting). The dragging works correctly except that text is getting selected while the user is dragging. Surely this is a legitimate use for disableSelection().

I know that it can be done without this method (otherwise it'd be impossible for the method to do it in the first place); but the reason I wanted to use disableSelection() is to concisely express my desire in a single place and have jQuery-UI handle my cross-browser compatibility issues rather than having to use a combination of several CSS rules and some JS (for IE7).

I believe this feature is deprecated because someone was annoyed at web sites that disable selection, and decided that this is not a practice they should encourage. It's analogous to a web programmer refusing to support popular legacy browsers because they don't want to encourage their use. Such a programmer would be fired, but you can get away with imposing your philosophy on others when you have power over a popular library. Deprecating this feature has the effect of making programmers who want to disable selection waste their time looking for an alternative method. By punishing evildoers, jQuery-UI will presumably make them change their ways, and our society will not be inundated with web sites that disable selection. So you see, there is a perfectly good rationalization, and whoever wrote "Disabling text selection is bad. Don't use this." in the official documentation can regard themselves as heroic. We're the bad guys here.

As an alternative to the CSS user-select: none; technique, I think simply preventing default behavior on double-click and mousedown via $(document).on('dblclick mousedown', '.prevent-select', false); has some advantages:

  1. Still allows text selection, should the user want to drag the cursor around the elements. Just prevents annoying text selection when the user is rapidly clicking.
  2. Better browser support.
  3. Not deprecated by jQuery team (no evil glares in code review.)

I could not agree more with @WesleyMurch, wish I could give you more +1s :) I also feel that, JS should not be the way forward, css may be better for this...,

If you need the selection to toggle (in some cases if dynamic) you can use jquery to toggle between classes, (old IE browsers) you may ask???

Well it's a good way (subtle tool) to discourage people to keep using OLD I.E. because of the things they are missing out.

http://caniuse.com/#search=user-select

(Although IE 8/9 [from number of users in % perspective] is a big number but it depends how much you really care)

I would use the CSS option as @Shmiddty explains but maybe, enabled to start with and disable as you go along, but that's just my opinion :)

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